I'm trying to call a function in a C dll, from C# code.
The C function :
#ifdef NT2000
__declspec(dllexport)
#endif
void MyFunction
( long *Code,
long Number,
unsigned char *Reference,
unsigned char *Result ) ;
And my C# call is :
[DllImport("mydll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void MyFunction(ref IntPtr Code,
int Number,
string Reference,
ref IntPtr Result);
This function is supposed to return "Code" and "Result"
The "Code" value that I get seems to be ok, but "Result" seems not ok, so I'm wondering if I'm getting the string value that I expect or the memory address ?
Thanks for your help.
CodePudding user response:
long
in C maps to int
in C#. And char
is ANSI, so you need to specify UnmanagedType.LPStr
. The Result
appears to be an out
parameter, for which you will need to pre-assign a buffer (size unknown?) and pass it as StringBuilder
[DllImport("mydll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void MyFunction(
ref int Code,
int Number,
[MarshalAs(UnmanagedType.LPStr)]
string Reference,
[MarshalAs(UnmanagedType.LPStr), Out]
StringBuilder Result);
Use it like this
var buffer = new StringBuilder(1024); // or some other size
MyFunction(ref myCode, myNumber, myReference, buffer);