Thursday, April 27, 2017

C# Note

One of our customers had an issue integrating our C-callable DLL into a Windows C# app. The first call to mhdrg1() worked but subsequent calls fails with memory corruption. After some back and forth, during which we suspected memory allocation (which turned not to be the culprit), our client resolved the issue themselves and were kind enough to follow up with a note which we reproduce here in case it is helpful to future C# integrators:

After further tests, I concluded that the form of the data being PASSED IN as StringBuilders is the correct/best way. PInvoke marshalling handles the nitty-gritty.
Ref:  and

Declaring the function (actually a delegate in its current test state) as returning "void" and calling it results in no problems/corruptions/errors. Thus corruption was occurring when trying to get a return value. So, I changed the return type to IntPtr on each function delegate and used marshaling to retrieve an ANSI string from the pointer. Viola! It works perfectly.

No comments:

Post a Comment