P/Invoke problem
-
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!
-
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!
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 thatchar*,
then doMarshal.PtrToString
, then pray the person that wrote the DLL used the default malloc/free mechanism. PInvokefree()
passing thechar*
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! -
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 thatchar*,
then doMarshal.PtrToString
, then pray the person that wrote the DLL used the default malloc/free mechanism. PInvokefree()
passing thechar*
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!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...
-
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...
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!