【问题标题】:Handling pointers to structure in C#在 C# 中处理指向结构的指针
【发布时间】:2017-03-22 13:49:05
【问题描述】:

我正在开发一个项目,其中包括用 C++ 编写的 DLL 和 C# 代码。假设那个 DLL 有一个函数:

MyStruct* GetPointer(); // returns pointer to MyStruct structure

MyStruct 结构如下所示:

struct MyStruct
{
    OtherStruct1 *data1;
    OtherStruct2 *data2;
};

OtherStruct1 和 OtherStruct2 结构如下所示:

struct OtherStruct1
{
public:
    double x;
    char y;
};

struct OtherStruct2
{
public:
    float a;
    float b;
    float c;
    float d;
};

我的问题是 - 在 C# 代码中处理所有这些指针的最佳方式是什么? “处理”是指读取和写入内存的操作。 C# 中的结构不能简单地包含指针变量。我应该做些什么?什么是最优雅的方式?

【问题讨论】:

  • 您是否期望在 C# 代码中被修改的对象也会更新正在查看相同指针的 c++ 代码?如果您必须在指针对象更新之前拨打Marshal.StructureToPtr 之类的电话,可以吗?
  • 我希望有一种方法可以直接在内存中更改 DLL 给出的对象的值,因此 C++ 代码可以继续处理由 C# 代码更改的这些对象。就像 DLL 提供指针,C# 用初始数据填充它们,DLL 对其进行处理。然后C#代码不断读取DLL修改的数据并在canvas上绘制结果。
  • 是否可以使GetPointer 成为一种C# 方法,以便托管代码是创建共享内存空间的代码?和/或您可以使用C++/Cli shim 来帮助弥合差距吗?
  • C++ 库已经编写好了,所以如果有一个解决方案假设通过 DLL 分配共享内存空间并在 C# 中访问它,那就更好了。

标签: c# c++ pointers memory dll


【解决方案1】:

您可以使用 Microsoft(现已开源)PInvoke Interop Assistant 工具将您的 C/C++ 代码转换为 C# 或 VB。运行您的示例代码给出:

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct MyStruct {

    /// OtherStruct1*
    public System.IntPtr data1;

    /// OtherStruct2*
    public System.IntPtr data2;
}

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct OtherStruct1 {

    /// double
    public double x;

    /// char
    public byte y;
}

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct OtherStruct2 {

    /// float
    public float a;

    /// float
    public float b;

    /// float
    public float c;

    /// float
    public float d;
}

public partial class NativeMethods {

    /// Return Type: MyStruct*
    [System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="GetPointer")]
public static extern  System.IntPtr GetPointer() ;

}

将 DllImportAttribute 中的“Unknown”替换为您的 dll 名称,并确保它在项目中被引用。您现在应该能够在托管代码中访问您的结构。

然后要读取/写入内存,您将需要使用 System.Runtime.InteropServices.Marshal 命名空间中的方法。以下代码 sn-p 显示了如何使用 GetPointer 函数将非托管结构转换为托管结构:

IntPtr myPtr = NativeMethods.GetPointer(); // Call native code to retrieve pointer to unmanaged memory where the struct lives
MyStruct myStruct = new MyStruct(); // Create a new managed struct
myStruct = Marshal.PtrToStructure<MyStruct>(myPtr);

以下是将托管结构传递给非托管方法的方法:

MyStruct myStruct = new MyStruct(); // Create the managed struct
myStruct.data1 = new OtherStruct1(); // Create a managed OtherStruct1
myStruct.data2 = new OtherStruct2(); // Create a managed OtherStruct2
IntPtr myStructPtr = Marshal.AllocHGlobal(Marshal.SizeOf<MyStruct>()); // Allocate unmanaged memory for the struct
Marshal.StructureToPtr<MyStruct>(myStruct, myStructPtr, false);
try
{
    NativeMethodThatUsesMyStructPtr(myStructPtr);
}
finally
{
    Marshal.FreeHGlobal(myStructPtr); // *** You have to free unmanaged memory manually, or you will get a memory leak! ***
}

【讨论】:

  • 谢谢,这是更优雅的方式,不使用“不安全”块:)
  • 我进行了编辑以展示如何在完成后释放非托管内存。这是必须的,否则会导致内存泄漏!
  • 使用 DLL 的 Clear() 函数可以释放共享内存 :) 但无论如何,谢谢,这是对未来非常有用的知识 :)
【解决方案2】:

好的,我重新设计了一个方法。

[StructLayout(LayoutKind.Sequential)]
struct MyStruct
{
    OtherStruct1 *data1;
    OtherStruct2 *data2;
};

[StructLayout(LayoutKind.Sequential)]
struct OtherStruct1
{
public:
    double x;
    char y;
};

[StructLayout(LayoutKind.Sequential)]
struct OtherStruct2
{
public:
    float a;
    float b;
    float c;
    float d;
};

然后:

unsafe
{
    MyStruct *tmp = (MyStruct*) GetPointer();
    tmp->data2[0].a = 1.0F;
}


[DllImport(DLL_PATH, CallingConvention = CallingConvention.Cdecl)]
    unsafe public static extern MyStruct* GetPointer();

像魅力一样工作。 :)

【讨论】:

    猜你喜欢
    • 2021-07-14
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多