Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. P/Invoke problem

P/Invoke problem

Scheduled Pinned Locked Moved C#
csharpasp-nethelpquestionworkspace
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mark Tutt
    wrote on last edited by
    #1

    After a day of scratching my head, I think I've found the problem with my latest project, however I'm not sure what to do about it. I've wrapped a legacy DLL with a .NET remoting solution that allows the functions to be accessed easily from ASP.NET. The DLL is used for reporting. On my test system, with demo configuration and minimal data, everything worked fine. At the clients site, where the reports are larger, the remoting service returns a null reference exception when the reports are over 4k. Here is functions P/Invoke signature: [DllImport("opsrpts.dll", CallingConvention=CallingConvention.Cdecl)] static extern String DisplayReport(int rvc, int wsobj, int rptnum, int period, int scope, int reset, int range, int start, int end, int suppress); and the C declaration: extern "C" DLLIMP_DBI char* __cdecl DisplayReport(int rvc, int wsobj, int rptnum, int period, int scope, int reset, int range, int start, int end, int suppress) I'm assuming that the attempt to append more than 4k in a single string operation blows something up somewhere. At this point it's the only logical explaination I have. Is there any way other than modifying the report to get the larger reports back? Since there is no way to just take the char* and work with it, I can't think of a way other than changing the DLL to use multiple calls to pull the data back in chunks. Thanks in advance!

    L 1 Reply Last reply
    0
    • M Mark Tutt

      After a day of scratching my head, I think I've found the problem with my latest project, however I'm not sure what to do about it. I've wrapped a legacy DLL with a .NET remoting solution that allows the functions to be accessed easily from ASP.NET. The DLL is used for reporting. On my test system, with demo configuration and minimal data, everything worked fine. At the clients site, where the reports are larger, the remoting service returns a null reference exception when the reports are over 4k. Here is functions P/Invoke signature: [DllImport("opsrpts.dll", CallingConvention=CallingConvention.Cdecl)] static extern String DisplayReport(int rvc, int wsobj, int rptnum, int period, int scope, int reset, int range, int start, int end, int suppress); and the C declaration: extern "C" DLLIMP_DBI char* __cdecl DisplayReport(int rvc, int wsobj, int rptnum, int period, int scope, int reset, int range, int start, int end, int suppress) I'm assuming that the attempt to append more than 4k in a single string operation blows something up somewhere. At this point it's the only logical explaination I have. Is there any way other than modifying the report to get the larger reports back? Since there is no way to just take the char* and work with it, I can't think of a way other than changing the DLL to use multiple calls to pull the data back in chunks. Thanks in advance!

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      Mark Tutt wrote:

      char* __cdecl DisplayReport

      Shoot the person that wrote that function, shoot him dead. People like that should not be allowed behind a keyboard. Suggest to him in a nice way that he should join the police academy. Alternatively if you run out of bullets, or dont have any handy, try the StringBuilder class instead. The correct way would be to get the value for that char*, then do Marshal.PtrToString, then pray the person that wrote the DLL used the default malloc/free mechanism. PInvoke free() passing the char* to prevent memory leaks. xacc-ide 0.0.99-preview7 now with C#, C, C++, IL, XML, Nemerle, IronPython, Perl, Caml, SML, Ruby, Flex, Yacc, Java, Javascript, Lua, Prolog and Boo highlighting support!

      M 1 Reply Last reply
      0
      • L leppie

        Mark Tutt wrote:

        char* __cdecl DisplayReport

        Shoot the person that wrote that function, shoot him dead. People like that should not be allowed behind a keyboard. Suggest to him in a nice way that he should join the police academy. Alternatively if you run out of bullets, or dont have any handy, try the StringBuilder class instead. The correct way would be to get the value for that char*, then do Marshal.PtrToString, then pray the person that wrote the DLL used the default malloc/free mechanism. PInvoke free() passing the char* to prevent memory leaks. xacc-ide 0.0.99-preview7 now with C#, C, C++, IL, XML, Nemerle, IronPython, Perl, Caml, SML, Ruby, Flex, Yacc, Java, Javascript, Lua, Prolog and Boo highlighting support!

        M Offline
        M Offline
        Mark Tutt
        wrote on last edited by
        #3

        leppie wrote:

        Shoot the person that wrote that function, shoot him dead. People like that should not be allowed behind a keyboard. Suggest to him in a nice way that he should join the police academy. Alternatively if you run out of bullets, or dont have any handy, try the StringBuilder class instead.

        Now now, the DLL in question is part of a legacy codebase that goes back nearly 15 years, and is pretty much exclusively C/ASM. Gotta give a little respect to the people who wrote the stuff that the world runs on. I'll take a look at Marshal.PtrToString...

        L 1 Reply Last reply
        0
        • M Mark Tutt

          leppie wrote:

          Shoot the person that wrote that function, shoot him dead. People like that should not be allowed behind a keyboard. Suggest to him in a nice way that he should join the police academy. Alternatively if you run out of bullets, or dont have any handy, try the StringBuilder class instead.

          Now now, the DLL in question is part of a legacy codebase that goes back nearly 15 years, and is pretty much exclusively C/ASM. Gotta give a little respect to the people who wrote the stuff that the world runs on. I'll take a look at Marshal.PtrToString...

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          Mark Tutt wrote:

          pretty much exclusively C/ASM.

          Even for C that is bad.

          Mark Tutt wrote:

          Gotta give a little respect to the people who wrote the stuff that the world runs on.

          Now I'm getting more worried... :sigh: xacc-ide 0.0.99-preview7 now with C#, C, C++, IL, XML, Nemerle, IronPython, Perl, Caml, SML, Ruby, Flex, Yacc, Java, Javascript, Lua, Prolog and Boo highlighting support!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups