I’m using a C++ dll containing a method with this signature:
int GetEnumerationString(int type, int value, const char** s ); /* ALLOCATES */
As mentioned in the header comment, the method allocates the pointer. I tried the following signature in C#:
[DllImport("thedll.dll", EntryPoint="GetEnumerationString")]
private static extern int GetEnumerationString(int type, int value, ref string s );
but when I run my program, I get an AccessViolationException.
What is the right signature for the method?
If an unmanaged function allocates a buffer, it must use the CLR allocator in order for the CLR to pick up the memory and free it when needed.
If that is not possible, you must return a pointer:
Then manually figure a string out of it and dispose the pointed to memory (for which you must know what the allocator was).