2012-02-06 74 views
0

期间我有这样的DLL调用:AccessViolation的PInvoke

[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。我应该在哪里寻找错误?

+0

ULONG在C#为8字节宽,而不是4个字节 – 2012-02-06 14:41:38

回答

1

rsuint32应表示为uint,它是4字节宽,而不是ulong,它在C#中是8字节长。此外,您可能希望确保,你的字符串编组proplery,通过指定字符集,这样的:

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

谢谢你,我的错,我迷茫长INT。对我感到羞耻:)但无论如何,这并没有帮助 - 我的代码抛出AccessViolationException。我也试过所有的字符集 - 没有任何帮助 – Archeg 2012-02-06 15:11:46

+0

发现问题。连同你指出的,我将其中一个字符串作为null传递。似乎他们不应该是空的。谢谢 – Archeg 2012-02-06 15:14:00