Hello List,
I have a c library that has the following signature:
If I have a C function that takes a 'const char* identifierString' as a parameter,
would the following be the correct way to handle this?
[DllImport(DLL_DIRECTORY_PATH)]
public static extern void MyExternalFunction(IntPtr identifierString);
and then to call it:
string identString = "My identifier string";
IntPtr ptrToString = Marshal.StringToHGlobalAnsi(identString);
MyExternalFunction(ptrToString);
Eric Carlson - 24 Dec 2004 16:57 GMT
This should work:
public static extern void MyExternalFunction(string identifierString);
And you may need to apply a marshas attribute to this parameter depending on
what type of string the C function is expecting, for example:
public static extern void
MyExternalFunction([MarshalAs(UnmanagedType.LPStr)] string identifierString);
usually you only need to use the intptr for more complex cases
> Hello List,
> I have a c library that has the following signature:
[quoted text clipped - 11 lines]
> IntPtr ptrToString = Marshal.StringToHGlobalAnsi(identString);
> MyExternalFunction(ptrToString);