如何解决C#/C++ 传递结构得到访问冲突异常
早上好, 我正在尝试实现 pos 设备。本机 api 是一个 C++ 库,我必须在我的 C# 项目中使用它。 我已经导入了几乎所有需要的功能(工作正常),但其中有一个我无论如何都做不到。
这是C++库中的函数:
__declspec(dllexport) void setHost(char* id,Host* host,char* trx,char* flag);
这是在 C++ 库中声明的结构:
typedef struct {
int hostConnectionType;
char ipAddress[15 + 1];
int tcpPort;
int hostTransportProtocol;
char gtId[5 + 1];
char gprsAPN[64 + 1];
char gprsUserName[15 + 1];
char gprsPassword[8 + 1];
int tlsCertificateId;
char personalizationId[10 + 1];
int gtIndex;
} Host;
这是C++演示项目中的使用示例:
Host test_host;
test_host.hostConnectionType = 3;
test_host.hostTransportProtocol = 2;
test_host.gtIndex = 9;
strcpy(test_host.ipAddress,"199.188.177.166");
test_host.tcpPort = 54321;
strcpy(test_host.gtId,"12345");
test_host.tlsCertificateId = 21985;
strcpy(test_host.personalizationId,"001");
MobilePosAdapter_dll("00000123",&test_host,"0000000001","1000");
我在 C# 项目中所做的是声明 Host 结构:
[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Ansi)]
public struct Host
{
public int hostConnectionType;
public int hostTransportProtocol;
public int tcpPort;
public int tlsCertificateId;
public int gtIndex;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst = 16)]
public string ipAddress;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst = 6)]
public string gtId;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst = 65)]
public string gprsAPN;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst = 16)]
public string gprsUserName;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst = 9)]
public string gprsPassword;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst = 11)]
public string personalizationId;
}
然后,声明函数、属性和符号:
[UnmanagedFunctionPointer(CallingConvention.Cdecl,CharSet = CharSet.Ansi)]
private delegate void cdecl__setHost([MarshalAs(UnmanagedType.LPStr)] string id,Host Host,[MarshalAs(UnmanagedType.LPStr)] string trx,[MarshalAs(UnmanagedType.LPStr)] string flags);
private static cdecl__setHost _setHost = (cdecl__setHost)Marshal.GetDelegateForFunctionPointer(NativeMethods.GetProcAddress(DllPtr,"setHost"),typeof(cdecl__setHost));
最后测试一下:
Host host = new Host()
{
gtId = 12345,gtIndex = 0,hostConnectionType = 3,hostTransportProtocol = 2,ipAddress = "199.188.177.166",personalizationId = "001",tcpPort = 54321,tlsCertificateId = 21985
};
_setHost("00000123",host,"1000");
我收到一个 System.AccessViolationException。
我错了什么?
这是整个 API 中唯一一个以结构体作为参数的函数。其他函数有 struct 作为值返回,它们都可以正常工作。
解决方法
随便猜一猜
在非托管方面,参数类型是 Host*
,而在 C++ 中,您传递的是 &test_host
。
但是,在 C# 方面,您有一个 struct(默认情况下是通过副本传递的东西,因此是通过 val)并且您只传递 {{1}到具有声明参数的函数:
host
请注意,您尚未在 private delegate
void cdecl __setHost(
[MarshalAs(UnmanagedType.LPStr)] string id,Host host,// <--------- HERE
[MarshalAs(UnmanagedType.LPStr)] string trx,[MarshalAs(UnmanagedType.LPStr)] string flags
);
参数中添加任何 MarshalAs,因此我认为它会通过副本传递,并且会发生一些损坏,因为接收函数期望获得一个指针(几个字节)不是整个结构(多几次)。
我会尝试的第一件事是添加任何您想将其作为指针传递的提示,例如:
host
并称之为
private delegate
void cdecl __setHost(
[MarshalAs(UnmanagedType.LPStr)] string id,ref Host host,[MarshalAs(UnmanagedType.LPStr)] string flags
);
此外,请务必仔细检查 Ackdari 在评论中所说的内容 - 如果 C# 提供的字段顺序与预期的本机代码之间的字段顺序不同,也可能导致相同的异常..
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。