C# P/Invoke:如何实现结构化参数字段的双重间接寻址
我正在从 C# 调用本机 dll。 对于所讨论的特定函数,我需要的参数之一是一个包含双重间接字段(指向指针的指针)的结构。
例如,采用以下 C 原型和结构:
int someFunc(SomeStruct* result);
struct SomeStruct
{
DWORD foo;
AnotherStruct** ppResultStruct;
}
struct AnotherStruct
{
DWORD bar;
}
以下 C# 代码仅为字段 AnotherStruct 提供单级间接,这是不正确的:
[DllImport("my.dll")]
public static extern int someFunc(SomeClass result);
[StructLayout(LayoutKind.Sequential)
public class SomeClass
{
int foo;
AnotherClass resultClass;
}
[StructLayout(LayoutKind.Sequential)]
public class AnotherClass
{
int bar;
}
如何实现此处所需的双重间接? 我知道如何在函数的双重间接参数的情况下执行此操作,但我对它嵌入在结构中的事实感到困惑。
也许我应该将 AnotherClass 字段声明为 IntPtr,然后使用不安全的代码为其分配适当的值。 这是一种有效的方法吗?还有其他/更好的选择吗?
I am calling into a native dll from C#. For the specific function in question, one of the parameters I need is a structure which contains a doubly-indirect field (pointer to a pointer).
For example, take the following C prototype and structs:
int someFunc(SomeStruct* result);
struct SomeStruct
{
DWORD foo;
AnotherStruct** ppResultStruct;
}
struct AnotherStruct
{
DWORD bar;
}
The following C# code provides only a single level of indirection for the field AnotherStruct, which is incorrect:
[DllImport("my.dll")]
public static extern int someFunc(SomeClass result);
[StructLayout(LayoutKind.Sequential)
public class SomeClass
{
int foo;
AnotherClass resultClass;
}
[StructLayout(LayoutKind.Sequential)]
public class AnotherClass
{
int bar;
}
How can I achieve the double-indirection that's required here? I know how to do it in the case of a doubly-indirect parameter to a function, but I'm confused by the fact that it's embedded in a struct.
Maybe I should declare the AnotherClass field as an IntPtr, and then resort to unsafe code to assign it an appropriate value. Is that a valid approach, and are there any other/better options?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有自定义 marashaler,您就无法构建既包含标准内联结构定义又包含双重间接寻址的类。 这种类型的编组需要一些魔法才能实现。
解决这个问题的最佳方法是将双重间接寻址视为实际的指针。 然后用漂亮的财产来驱除邪恶
Without a custom marashaler you cannot build a class that both contains a standard inline struct definition and double indirection. This type of marshaling requires a bit of magic to achieve.
The best way to approach this is to treat the double indirection as a pointer which it actual is. Then use a pretty property to take care of the evilness