Samer
Samer

Reputation: 1980

converting IntPtr as c# struct pointer

I have an unmanaged C++ function that reads like :int myfunction(LPVOID p1, LPVOID p2)

My wrapper in C# takes extern static int mywrapperFunction(IntPtr p1, IntPtr p2)

Within my wrapper function definition, i want to deference IntPtr to a structure.

In C++:

int myfunction(LPVOID p1, LPVOID p2)
{
    (MYFIRSTSTRUCTURE *)abc = (MYFIRSTSTRUCTURE *)p1;
    (MYSECONDSTRUCTURE *)efg = (MYSECONDSTRUCTURE *)p1;
    //rest of the operation involves this abc and efg
}

I need to do similar thing in C#:

int mywrapperFunction(IntPtr p1, IntPtr p2)
{
   //how to consume IntPtr p1 and IntPtr p2 for C# structure similar to MYFIRSTSTRUCTURE and //MYSECONDSTRUCTURE
}

Upvotes: 2

Views: 3268

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564433

The normal way to handle this is via Marshal.PtrToStructure.

Upvotes: 4

Related Questions