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.
Compliled c/c++ function call:
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->Write(Wbuf, 7); // Send command to firmware
Sleep(5);
memset(Rbuf,0,7);
pComPort->Read(Rbuf, 7); // Receive response from Frimwate
if ((Rbuf[0] == START_DATA_HEADER)
&& (Rbuf[1] == FIRMWARE_VERSION_PACKET)
&& (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;
}
C# implementation (along with a wrapper function for the class):
[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;
}
At run-time, the below error is being raised. Is there something that I am missing?
Updated with error text:
"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."