I am a complete noob when it comes to c / C ++ and PInvoke, so please tell me about me.
I got this assembly from someone I would like to use in my C # application.
The title is as follows:
int __declspec(dllimport) s2o(WCHAR* filename, char** out, int* len);
I managed to partially process it using:
[DllImport("s2o.dll", EntryPoint = "?skn2obj@@YAHPA_WPAPADPAH@Z", CallingConvention = CallingConvention.Cdecl)]
public static extern int s2o(
[MarshalAs(UnmanagedType.LPWStr)]
string filename,
ref char[] @out,
ref int len
);
And then calling it like this:
char[] result = null;
int length = 0;
s2o("filepath", ref result, ref length);
This seems to work in part because the “length” actually gets the meaning. Unfortunately, the "result" remains zero.
What should I do to make this work?
Edit:
Well, I managed to work by replacing char [] with IntPtr and then calling "Marshal.PtrToStringAnsi", as Nick suggested:
string result = Marshal.PtrToStringAnsi(ptr);
However, due to comments in the same answer, I'm a little worried about memory usage. There are no other ways in the assembly, so how can I clear them?
Thank!