2012-02-06 70 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