尝试调用DLL,但该方法有一个复杂的结构类型,我无法在.Net中定义它

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

C ++中的struct定义是:

typedef struct _DEVICE_PATH_LIST_
{
    int NumDevice;  
    TCHAR *devicePath[DEVICE_PATH_LIST_SIZE];   
} DEVICE_PATH_LIST;

和C ++函数定义是:

 LandiWin_iGetDevicePathList( int iDeviceType, DEVICE_PATH_LIST *  devicePathList);

然后我想调用那个方法,所以我需要定义一个可以代表DEVICE_PATH_LIST的结构。这是我在C#中的定义:

[StructLayout(LayoutKind.Sequential)]
public unsafe struct DevicePathType
{
    public int NumDevice;
    [MarshalAs(UnmanagedType.ByValArray,SizeConst=32)]
    public char*[] devicePath;
}

但它不对,我也试过所有其他类型,但都失败了。请帮我定义一个正确的结构。

c# c++ dllimport
1个回答
0
投票

C ++的代码如下:

typedef struct _DEVICE_PATH_LIST_
{
 int NumDevice; 
 TCHAR *devicePath[DEVICE_PATH_LIST_SIZE];  
} DEVICE_PATH_LIST;

C#的代码需要像:

[StructLayout(LayoutKind.Sequential)]
public struct DevicePathType
{
    public int NumDevice;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
    public IntPtr[] devicePath;
}
© www.soinside.com 2019 - 2024. All rights reserved.