PInvoke 期间的访问冲突
我有这个 dll 调用:
[DllImport("FreqCntPcIntf.dll", CallingConvention = CallingConvention.StdCall)]
public static extern Intf_ErrorType FreqCntIntf_Init(
ref byte InstNo,
string InstName,
string PortServerName,
ulong UartComPort,
ulong MaxBlockTime);
enum Intf_ErrorType
{
...
}
C++ 声明是这样的:
FREQCNTINTF_API Intf_ErrorType STDCALL FreqCntIntf_Init(InstanceNoType* InstNo, const char* InstName, const char* PortServerName, rsuint32 UartComPort, rsuint32 MaxBlockTime);
其中:
typedef enum
{
....
} RSENUM8(Intf_ErrorType);
#define FREQCNTINTF_API __declspec(dllexport)
typedef rsuint8 InstanceNoType;
typedef unsigned char rsuint8;
#define RSENUM32(EnumName) Enum_##EnumName; typedef rsuint32 EnumName
我在调用期间收到 AccessViolation。我应该在哪里寻找错误?
I have this dll call:
[DllImport("FreqCntPcIntf.dll", CallingConvention = CallingConvention.StdCall)]
public static extern Intf_ErrorType FreqCntIntf_Init(
ref byte InstNo,
string InstName,
string PortServerName,
ulong UartComPort,
ulong MaxBlockTime);
enum Intf_ErrorType
{
...
}
And the C++ declaration is this:
FREQCNTINTF_API Intf_ErrorType STDCALL FreqCntIntf_Init(InstanceNoType* InstNo, const char* InstName, const char* PortServerName, rsuint32 UartComPort, rsuint32 MaxBlockTime);
where:
typedef enum
{
....
} RSENUM8(Intf_ErrorType);
#define FREQCNTINTF_API __declspec(dllexport)
typedef rsuint8 InstanceNoType;
typedef unsigned char rsuint8;
#define RSENUM32(EnumName) Enum_##EnumName; typedef rsuint32 EnumName
I receive AccessViolation during call. Where should I look to find the bug?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
rsuint32
应表示为uint
(4 字节宽),而不是ulong
(在 C# 中为 8 字节长)。另外,您可能想通过指定 CharSet 来确保您的字符串已正确编组,如下所示:rsuint32
should be expressed asuint
, which is 4-byte wide, notulong
, which in C# is 8 byte long. Also, you might want to make sure, that your strings are marshalled proplery, by specifying CharSet, like this: