Archeg
Archeg

Reputation: 8462

AccessViolation during PInvoke

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?

Upvotes: 0

Views: 331

Answers (1)

Marcin Deptuła
Marcin Deptuła

Reputation: 11957

rsuint32 should be expressed as uint, which is 4-byte wide, not ulong, 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:

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

Upvotes: 1

Related Questions