Reputation: 469
i am using c# to call a c code through a DLL , i got AccessViolationException when calling a method , here is the code from the source header
extern __declspec( dllexport )
int ReadCardSN( IN OUT unsigned char* CardSN );
in my c# code i am using
public static byte[] Data = new byte[4];
[DllImport("CardLib.dll")]
public static extern Int32 ReadCardSN(byte[] Data);
int resCode = ReadCardSN(Data);
what can be the problem ?
Upvotes: 0
Views: 98
Reputation: 613013
The error is because your buffer is too small. The example code shows the use of a buffer of length 10240. You supply a buffer of length 4.
As written it looks like the C code uses the default cdecl calling convention. Your C# code uses stdcall.
It would also be better to apply the [In, Out]
attribute to the argument. Because byte[]
is blittable that is not strictly necessary but it is semantically accurate.
Upvotes: 3