I have a c++ exported function in dll:
int MyMethod(ulong pid, MyStruct* struct);
MyStruct is described as class:
class MyStruct
{
public:
uchar nVersion;
uchar nModuleType;
uchar nMachine64;
uchar nReserved;
ulong data1;
ulong data2;
ulong data3;
};
I’m trying to import this function to my C# code like this:
[DllImport("mydll.dll", EntryPoint = "#24")]
private static extern int _MyMethod(long pid, ref MyStruct struct);
Class in C#:
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
stuct MyStruct
{
public byte nVersion;
public byte nModuleType;
public byte nMachine64;
public byte nReserved;
public ulong data1;
public ulong data2;
public ulong data3;
}
And i’m getting System.AccessViolationException:
MyStruct struct = new MyStruct();
_MyMethod(4728, ref struct);
What’s wrong?
UPDATE:
System.Runtime.InteropServices.Marshal.SizeOf(struct) returns 32. Why? I thought it should be 4 * 1 + 8 * 3 = 28
In C# we have
classes andstructs. Allclasstypes are reference butstructtypes are value types. This means when you have something likeclass MyStructand you writeMyStruct sit is actually something like a pointer to base class, and when you pass it by reference you actually pass address of that pointer, so it has nothing to do with C++ that expect a pointer to mainstruct. According to this solution to your problem is to convertclasstostruct.longandulongin C# are 64 bit types while they are 32 bit in C++(MSVC at least), so when you declare your function such that its first parameter islongyou send extra 32 bit value that may override next parameter and cause it to be invalid:So when function called it will take an invalid parameter as address of struct. so just change your function definition to:
An your struct to:
may be source of your error is in first parameter of the function, because function expect a 32 bit value and you provide a 64 bit one and actually you provide 2, 32 bit value to the function that cause the function