c# - 使用 .NET 中的 Dll 的指针参数调用函数
问题描述
我已经使用 C 编写了一个函数并将其构建到一个 DLL 文件中。然后从 C# 中,我像这个示例一样从 Dll 调用我的函数。 https://blogs.msdn.microsoft.com/jonathanswift/2006/10/03/dynamically-calling-an-unmanaged-dll-from-net-c/ 有这样的功能:在C中:
typedef struct {
DWORD Old;
}STRUCT_SON;
typedef struct {
DWORD NumberOfGirl;
STRUCT_SON SonList[8];
}STRUCT_PARENT;
int getStructExample(STRUCT_PARENT* x_lstExample, DWORD* x_dwSum)
{
....
}
在 C# 中:
private delegate int getStructExampleDelegate(out ?????,out int iSum);
IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pDll,
"getStructExample");
getStructExampleDelegate getStructExample =
(getStructExampleDelegate)Marshal.GetDelegateForFunctionPointer(
pAddressOfFunctionToCall,typeof(getStructExampleDelegate));
int iSum;
int theResult = getStructExample(out ??????, out iSum);
所以我的问题是如何获取结构数据类型?也许使用 Marshal.PtrToStructure。但是什么是“??????” 获取数组 STRUCT_SON。 C# 调用 C++ DLL 函数,它返回一个结构, 我使用这个例子来获取 x_dwSum,但不知道如何获取该结构。
解决方案
???没有出来。它使用 IntPtr 获取结构的地址,然后使用 Marshal.PtrToStructure 解析为 c# 中重新定义的结构。在 C# 中重新定义必须是这样的:
public struct STRUCT_PARENT {
DWORD NumberOfGirl;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
STRUCT_SON[] SonList;
};
推荐阅读
- r - 如何将徽标保留在闪亮应用程序的窗口标题中
- sphinx - Sphinx 在索引完成之前过早执行 sql_query_post_index 查询
- sql - 获取所有行以及sql中的重复列数
- java - Jenkins windows slave不断失去连接并出现错误:ClosedChannelException
- java - Vaadin 设置背景不起作用
- css - 在移动设备上以纵向格式显示背景图像时出现 CSS 问题
- ios - 以编程方式创建 ARReferenceImage
- primefaces - p:textEditor 标记无法正常工作
- javascript - this.$root.$emit 在 Vue 中不起作用
- ibm-cloud - Watson Assistant 根据实体值处理逻辑的最佳方式