我正在尝试从 C# 程序中已编译的 C/C++ dll 调用一些函数。我已经成功调用了几个函数,但是遇到了几个问题。
编译的 c/c++ 函数调用:
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# 实现(以及类的包装函数):
[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;
}
在运行时,会引发以下错误。有什么我想念的吗?
更新了错误文本:
“检测到 PInvokeStackImbalance” “对 PInvoke 函数 'DLLTalk!DLLTalk.DLLClass::ADS1292R_Get_Version' 的调用使堆栈不平衡。这可能是因为托管 PInvoke 签名与非托管目标签名不匹配。检查调用约定和参数PInvoke 签名与目标非托管签名匹配。”