Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling C/C++ dll functions from C#
    text
    copied!<p>I am trying to call some functions from a compiled C/C++ dll in a C# program. I have successfully called several of the functions but am having trouble with a couple.</p> <p>Compliled c/c++ function call:</p> <pre><code>DECLDIR int ADS1292R_Get_Version(unsigned char ADS129x_Version[]) { unsigned char Wbuf[10]; unsigned char Rbuf[10], RetVal =1; Wbuf[0] = START_DATA_HEADER; // PACKET Start header Wbuf[1] = FIRMWARE_VERSION_REQ; // Get firmware version info command Wbuf[2] = 0x00; // Not used Wbuf[3] = 0x00; // Not used Wbuf[4] = 0x00; // Not used Wbuf[5] = END_DATA_HEADER; // Packet End header Wbuf[6] = '\n'; pComPort-&gt;Write(Wbuf, 7); // Send command to firmware Sleep(5); memset(Rbuf,0,7); pComPort-&gt;Read(Rbuf, 7); // Receive response from Frimwate if ((Rbuf[0] == START_DATA_HEADER) &amp;&amp; (Rbuf[1] == FIRMWARE_VERSION_PACKET) &amp;&amp; (Rbuf[5] == END_DATA_HEADER)) { ADS129x_Version[0]= Rbuf[2]; // Get Major Number ADS129x_Version[1]= Rbuf[3]; // Get Minor number RetVal = 0; // Set return val as su } return RetVal; </code></pre> <p>}</p> <p>C# implementation (along with a wrapper function for the class):</p> <pre><code> [DllImport("ADS1292R_USB_lib.dll", EntryPoint = "ADS1292R_Get_Version")] public static extern int ADS1292R_Get_Version(byte[] x); public int getVersion() { byte[] dataTemp = new byte[3]; int mydata = ADS1292R_Get_Version(dataTemp); if (mydata == 0) { MessageBox.Show("1:" + dataTemp[0].ToString() + " 2:" + dataTemp[1].ToString() + " 3:" + dataTemp[2].ToString()); } return 0; } </code></pre> <p>At run-time, the below error is being raised. Is there something that I am missing?</p> <p>Updated with error text:</p> <p>"PInvokeStackImbalance was detected" "A call to PInvoke function 'DLLTalk!DLLTalk.DLLClass::ADS1292R_Get_Version' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature."</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