【问题标题】:converting IntPtr as c# struct pointer将 IntPtr 转换为 c# 结构指针
【发布时间】:2014-09-07 15:20:00
【问题描述】:

我有一个非托管 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
}

【问题讨论】:

  • C++ 代码中没有充分的理由将这些参数声明为 void*,它们应该是类型化参数。在 C# 中做同样的事情的充分理由就更少了。以应有的方式声明它,它是 int mywrapper(ref MyFirstStructure p1, ref MySecondStructure p2)
  • 这是一个我没有写的大代码的一部分,此时我无法更改它,我只是在做包装器

标签: c# c++ pointers intptr


【解决方案1】:

处理这个问题的正常方法是通过Marshal.PtrToStructure

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    • 1970-01-01
    • 2016-11-10
    相关资源
    最近更新 更多