。NET中使用Dll的指针参数调用函数

问题描述 投票:2回答:1

我已经使用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); 

所以我的问题是如何获取struct数据类型?也许使用Marshal.PtrToStructure。但是什么是“ ????”获取数组STRUCT_SON。C# Calling C++ DLL Function which returns a struct我正在使用像这样的示例来获取x_dwSum,但不知道要获取该结构。

c# c struct dll out
1个回答
0
投票

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

public struct STRUCT_PARENT {
        DWORD           NumberOfGirl;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]                 
        STRUCT_SON[]      SonList;
    };
© www.soinside.com 2019 - 2024. All rights reserved.