将IntPtr转换为c#结构指针

本文关键字:结构 指针 IntPtr 转换 | 更新日期: 2023-09-27 18:13:19

我有一个非托管的c++函数,读起来像:int myfunction(LPVOID p1, LPVOID p2)

我的c#包装器使用extern static int mywrapperFunction(IntPtr p1, IntPtr p2)

在我的包装器函数定义中,我想把IntPtr看作一个结构体。

在c++:

int myfunction(LPVOID p1, LPVOID p2)
{
    (MYFIRSTSTRUCTURE *)abc = (MYFIRSTSTRUCTURE *)p1;
    (MYSECONDSTRUCTURE *)efg = (MYSECONDSTRUCTURE *)p1;
    //rest of the operation involves this abc and efg
}
我需要在c#中做类似的事情:
int mywrapperFunction(IntPtr p1, IntPtr p2)
{
   //how to consume IntPtr p1 and IntPtr p2 for C# structure similar to MYFIRSTSTRUCTURE and //MYSECONDSTRUCTURE
}

将IntPtr转换为c#结构指针

正常的处理方式是通过Marshal.PtrToStructure