【问题标题】:Calling the function using pointer parameter of Dll from .NET使用 .NET 中的 Dll 的指针参数调用函数
【发布时间】:2019-12-13 01:37:40
【问题描述】:

我使用 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# Calling C++ DLL Function which returns a struct 我像这个例子一样使用来获取 x_dwSum,但不知道如何获取该结构。

【问题讨论】:

    标签: c# c struct dll out


    【解决方案1】:

    ????没有出来。它使用 IntPtr 获取结构的地址,然后使用 Marshal.PtrToStructure 解析为 c# 中重新定义的结构。 在 C# 中重新定义必须是这样的:

    public struct STRUCT_PARENT {
            DWORD           NumberOfGirl;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]                 
            STRUCT_SON[]      SonList;
        };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-11
      • 2013-03-31
      • 2021-09-17
      相关资源
      最近更新 更多