PInvoke期间的AccessViolation

时间:2012-02-06 14:34:03

标签: c# .net pinvoke dllimport

我有这个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。我应该在哪里找到这个bug?

1 个答案:

答案 0 :(得分:1)

rsuint32应表示为uint,宽度为4字节,而不是ulong,在C#中长度为8字节。此外,您可能希望通过指定CharSet来确保您的字符串是编组的proplery,如下所示:

[DllImport("FreqCntPcIntf.dll", CallingConvention = CallingConvention.StdCall, CharSet=CharSet.Ansi)]