I am trying to call a method in a dll for which I am using the C ++ header. I am calling a dll from C #. The input is a string, and the output is binary data. Any of the following 3 methods will probably work, I just don't know how to get any of them to work fully. C # declaration made by me, so they may be wrong
1: I can get hGlobal, but I don't know how to get data from the descriptor.
//CMBT_LL_WINAPI INT DLLPROC LlConvertStringToHGLOBALW(LPCWSTR pszText, _PHGLOBAL phMemory); [DllImport("cmll15.dll", EntryPoint = "LlConvertStringToHGLOBALW", CharSet = CharSet.Unicode, ExactSpelling = true)] private static extern int _LlConvertStringToHGlobal32(string text, ref IntPtr handle);
2:
[DllImport("cmll15.dll", EntryPoint = "LlConvertStringToBLOBW", CharSet = CharSet.Unicode, ExactSpelling = true)]
3:
[DllImport("cmll15.dll", EntryPoint = "LlConvertStringToStreamW", CharSet = CharSet.Unicode, ExactSpelling = true)]
Updated, here is the code that I think will end up.
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)] private static extern UIntPtr GlobalSize(IntPtr hMem); [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)] public static extern IntPtr GlobalLock(IntPtr handle); [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)] public static extern IntPtr GlobalUnlock(IntPtr handle); [DllImport("cmll15.dll", EntryPoint = "LlConvertStringToHGLOBALW", CharSet = CharSet.Unicode, ExactSpelling = true)] private static extern int _LlConvertStringToHGlobal32(string text, ref IntPtr handle); private static void Main(string[] args) { IntPtr dataHandle = IntPtr.Zero; _LlConvertStringToHGlobal32(Contents, ref dataHandle); try { var size = (uint) GlobalSize(dataHandle); var array = new byte[size]; IntPtr dataPtr = GlobalLock(dataHandle); try { Marshal.Copy(dataPtr, array, 0, array.Length); } finally { GlobalUnlock(dataPtr); } using (var fs = new FileStream("c:\\file.dat", FileMode.Create)) { fs.Write(array, 0, array.Length); } } finally { Marshal.FreeHGlobal(dataHandle); } }