[英]Importing C++ dll to C#
我是C#的新手,请在以下情况下为我提供帮助。
我正在尝试将C ++ dll导入我的C#代码,并且出现以下错误。
对PInvoke函数'SerialInterface!SerialInterface.Form1 :: ReadTagData'的调用已使堆栈不平衡。 这可能是因为托管PInvoke签名与非托管目标签名不匹配。 检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配。
下面是C#代码
const int buffSize = 33;
const int addr = 112;
const int readBytes = 8;
[DllImport(@"C:\Visual Studio 2010\Projects\SerialInterface\SerialInterface\bin\Debug\AP4600_SDK.dll")]
public static extern int ReadTagData(string tagID, string tagBuffer, Int32 szTagDataBuf, Int32 TagAddress, Int32 nBytes);
string asciiRead = "";
int s = ReadTagData(TagId, asciiRead, buffSize, addr, readBytes);
ReadTagData
中的功能ReadTagData
定义为
AP4600_SDK_API int ReadTagData(
const char *pTagId, /* TagId of tag to be read, from Identify */
char *pTagDataBuf, /* (Output) The data read (ASCII representation of HEX), min size is 2*nBytes+1 (33 for Allegro) */
size_t szTagDataBuf, /* Size of TagDataBuf, minimum is 2*nBytes+1 (33 for Allegro) */
int TagAddress, /* Address of first byte to read */
size_t nBytes /* Number of bytes to read (between 1 and 8, inclusive) */
); /* Returns zero for success, non zero failure */
这是我可以看到的错误:
AP4600_SDK_API
的定义方式,调用约定可能是错误的。 由于无法看到AP4600_SDK_API
评估方式,因此无法确定。 您可能需要在p / invoke中明确指定调用约定。 目前,它使用默认值CallingConvention.StdCall
。 StringBuilder
而不是`string。 size_t
参数应为指针大小。 使用UIntPtr
。 因此,假设调用约定为StdCall
则它将是:
[DllImport(@"...", CallingConvention.StdCall)] // or perhaps CallingConvention.Cdecl
public static extern int ReadTagData(
string tagID,
StringBuilder tagBuffer,
UIntPtr szTagDataBuf,
int TagAddress,
UIntPtr nBytes
);
这样称呼它:
StringBuilder tagBuffer = new StringBuilder(nBytes*2+1)
int retval = ReadTagData(tagId, tagBuffer, tagBuffer.Capacity, addr, nBytes);
if (retval != 0)
// handle error
// output can now be read with tagBuffer.ToString()
在收到的错误消息中明确指出了该问题。
char *pTagDataBuf
,c ++中的char *pTagDataBuf
与c#中的string pTagDataBuf
。 C#具有托管字符串。 char*
使用非托管指针。 您可能需要使用不安全的代码块来访问该功能。
如果您尝试将32位dll加载到64位项目或vica verca中,也会显示此错误消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.