Note that there are some explanatory texts on larger screens.

plurals
  1. POCall a C++ DLL method from a C# application
    text
    copied!<p>I'm trying to use a method from a C++ DLL in a C# project, but I'm, having problems calling it correctly. The method is this:</p> <p>As said in the SDK manual</p> <p><strike> DWORD WINAPI PrtRead (HANDLE hPrt, DWORD dwTimeout, DWORD *pdwType, LPDWORD pdwParArray, LPDWORD pdwArraySize, LPBYTE pbReadData, LPDWORD pdwReadDataLen) </strike></p> <p>As really defined on code</p> <pre><code>extern "C" __declspec(dllimport) DWORD PrtRead (HANDLE hPRT, DWORD dwTimeout, DWORD *pdwType, LPDWORD pdwParArray, LPDWORD pdwArraySize,LPBYTE pbReadData, LPDWORD pdwReadDataLen); </code></pre> <p>and in the SDK C++ sample they call it like this:</p> <pre><code>DWORD dwPar[2]; pdwParArray = &amp;dwPar[0]; dwPar[0] = 0; dwPar[1] = 0; DWORD dwRet = PrtRead(hPrinter, dwCurrentTimeout, &amp;dwType, pdwParArray, &amp;dwArraySize, NULL, &amp;dwReadDataLen); </code></pre> <p>My problem is getting the value LPDWORD pdwParArray.</p> <p>The DLL always returns one of the following values in position [0]: 1, 2 or 20 and in postion [1]: 1, 2 or 4, but I'm unbale to make it do this.</p> <p>I've tryed defining the import like this:</p> <pre><code>[DllImport("HPRDH.dll", CallingConvention = CallingConvention.Cdecl)] public static extern ulong PrtRead(IntPtr hPrt, ulong dwTimeout, ref ulong pdwType, XXXXXXXXX , ref ulong pdwArraySize, ref byte[] pbReadData, ref ulong pdwReadDataLen); </code></pre> <p>and varied XXXXXXXXX like this</p> <hr> <p>Method definition:</p> <pre><code>out ulong[] pdwParArray </code></pre> <p>Variable Initialization:</p> <pre><code>ulong[] pdwParArray; </code></pre> <p>Method returns:</p> <pre><code>pdwParArray = null </code></pre> <hr> <p>Method definition:</p> <pre><code>ref ulong[] pdwParArray </code></pre> <p>Variable Initialization:</p> <pre><code>ulong[] pdwParArray = new ulong[2]; pdwParArray[0] = 0; pdwParArray[1] = 0; </code></pre> <p>Method returns: pdwParArray[0] = 0; pdwParArray[1] = Out of bounds array index;</p> <hr> <p>Method definition: out ulong pdwParArray</p> <p>Variable Initialization: ulong[] pdwParArray = new ulong[2];</p> <p>Method returns: pdwParArray[0] = 0; pdwParArray[1] = 0;</p> <hr> <p>Method definition: ref ulong pdwParArray</p> <p>Variable Initialization: ulong[] pdwParArray = new ulong[2];</p> <p>Method returns: pdwParArray[0] = 0; pdwParArray[1] = 0;</p> <hr> <p>How should I define the LPDWORD pdwParArray in my C# import, how to initialize it and call it?</p> <p>Thanks in advance</p> <pre><code>_ _ _ _ </code></pre> <p><strong>EDIT:</strong> @TheMathemagician</p> <p>Tried the following with these results:</p> <p>Definition:</p> <p>[MarshalAs(UnManagedType.LPArray)] ulong[] pdwParArray</p> <p>Result:</p> <p>pdwParArray[0] = 0;</p> <p>pdwParArray[1] = 0;</p> <hr> <p>Definition:</p> <p>[MarshalAs(UnmanagedType.LPArray)] out ulong[] pdwParArray</p> <p>Result:</p> <p>pdwParArray[0] = null;</p> <p>pdwParArray[1] = null;</p> <hr> <p>Definition:</p> <p>[MarshalAs(UnmanagedType.LPArray)] ulong pdwParArray</p> <p>Result:</p> <p>Cannot marshal 'parameter #4': Invalid managed/unmanaged type combination (Int64/UInt64 must be paired with I8 or U8).</p> <hr> <p>Definition:</p> <p>[MarshalAs(UnmaDefinition:nagedType.LPArray)] out ulong pdwParArray</p> <p>Result:</p> <p>Cannot marshal 'parameter #4': Invalid managed/unmanaged type combination (Int64/UInt64 must be paired with I8 or U8).</p>
 

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