If this function allocates memory and makes the operator responsible for freeing it, Iām afraid that you will have to manage it manually: declare the parameter as ref IntPtr and use the methods of the Marshal class to get a String with a copy of the pointed data.
Then call the appropriate function to free memory (as Dirk said, we can no longer talk about it without additional information about the function).
If it really needs to be highlighted before the call, it should look something like this:
[DllImport("yourfile.dll", CharSet = CharSet.Ansi)] public static extern sbyte service_GetParameter ( String parameter, Int32 length, ref IntPtr val); public static string ServiceGetParameter(string parameter, int maxLength) { string ret = null; IntPtr buf = Marshal.AllocCoTaskMem(maxLength+1); try { Marshal.WriteByte(buf, maxLength, 0);
source share