Note that there are some explanatory texts on larger screens.

plurals
  1. POCall dll with C++ header from C#
    text
    copied!<p>I'm trying to call a method in a dll which I have the C++ header for. I'm calling the dll from C#. The input is a string and the output is binary data. Any of the following 3 methods would probably work, I just don't know how to make any of them work all the way. The C# declaration is made by me, so they might not be correct</p> <p>1: I'm able to get the hGlobal, but I don't know how to get data from the handle.</p> <pre><code>//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); </code></pre> <p>2: </p> <pre><code>[DllImport("cmll15.dll", EntryPoint = "LlConvertStringToBLOBW", CharSet = CharSet.Unicode, ExactSpelling = true)] //CMBT_LL_WINAPI INT DLLPROC LlConvertStringToBLOBW(LPCWSTR pszText, _PUINT8 pBytes, UINT nBytes); private static extern int _LlConvertStringToBLOBW(string text, ref IntPtr pBytes, UInt32 nBytes); </code></pre> <p>3:</p> <pre><code>[DllImport("cmll15.dll", EntryPoint = "LlConvertStringToStreamW", CharSet = CharSet.Unicode, ExactSpelling = true)] //CMBT_LL_WINAPI INT DLLPROC LlConvertStringToStreamW(LPCWSTR pszText, _PISTREAM pStream); private static extern int _LlConvertStringToStreamW(string text, ref IntPtr pStream); </code></pre> <p>Updated, here is the code I think I will end up with.</p> <pre><code> [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); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload