2

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
}
2
  • 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) Commented Jul 16, 2014 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 Commented Jul 16, 2014 at 23:58

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.