3

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
}
localhost
  • 375
  • 1
  • 3
  • 15
Samer
  • 1,923
  • 3
  • 34
  • 54
  • 1
    There was no good reason in the C++ code to declare these arguments void*, they should have been typed arguments. There's even less of a good reason to do the same in C#. Declare it the way it should be, it is int mywrapper(ref MyFirstStructure p1, ref MySecondStructore p2) – Hans Passant Jul 16 '14 at 22:35
  • This is part of a big code which I didn't write and at this point I can't change it, I am just doing the wrapper – Samer Jul 16 '14 at 23:58

1 Answers1

4

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

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373