【发布时间】:2021-12-06 09:38:27
【问题描述】:
我一直在尝试从我的 C# 应用程序调用 C++ dll 中的一个函数。这是我到目前为止所拥有的,但我不知道我是否走在正确的道路上,或者我做错了什么。由于函数参数是自定义类型的,我不知道该怎么做。
这是C++中的函数定义
int GetDeviceList(
PRINTER_LIST* pList
);
这些是参数
#define MAX_PRINTER 32
typedef struct {
WCHAR name[128]; // printer name
WCHAR id[64]; // printer ID
WCHAR dev[64]; // device connection
WCHAR desc[256]; // description
int pid; // USB product ID
} PRINTER_ITEM;
typedef struct {
int n;
PRINTER_ITEM item[MAX_PRINTER];
} PRINTER_LIST;
到目前为止,我能够转换参数
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct PrinterItem {
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
public string name;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
public string id;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
public string dev;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public string desc;
public int pid;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct PrinterList {
public int n;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public PrinterItem[] item;
}
我一直在尝试将它实现到我的程序中
public
class Program
{
[DllImport("dllName.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int GetDeviceList(_____);
static void Main(string[] args)
{
var deviceList = GetDeviceList(____);
}
}
【问题讨论】:
-
问题是什么?