Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a pointer to class into unmanaged c++ code from C#

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

like image 301
Denis Kildishev Avatar asked Nov 03 '22 13:11

Denis Kildishev


1 Answers

In C# we have classes and structs. All class types are reference but struct types are value types. This means when you have something like class MyStruct and you write MyStruct s it 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 main struct. According to this solution to your problem is to convert class to struct.

long and ulong in 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 is long you send extra 32 bit value that may override next parameter and cause it to be invalid:

Stack:
    32 bit: [first 32 bit of the first parameter]
    32 bit: [second 32 bit of the first parameter]
    32 bit: [address of your structure]

So when function called it will take an invalid parameter as address of struct. so just change your function definition to:

[DllImport("mydll.dll", EntryPoint = "#24")]
private static extern int _MyMethod(int pid, ref MyStruct struct);

An your struct to:

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
stuct MyStruct
{
    public byte nVersion;
    public byte nModuleType;
    public byte nMachine64;
    public byte nReserved;
    public uint data1;
    public uint data2;
    public uint data3;
}

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

like image 197
BigBoss Avatar answered Nov 15 '22 05:11

BigBoss