从非托管DLL调用函数
本文关键字:调用 函数 DLL | 更新日期: 2023-09-27 18:10:24
我有一个具有以下功能的非托管DLL:
ReadLatch( HANDLE cyHandle,
LPWORD lpLatch);
WriteLatch(HANDLE cyHandle,
WORD mask,
WORD latch);
GetPartNumber(HANDLE cyHandle,
LPBYTE lpbPartNum);
GetDeviceProductString(HANDLE cyHandle,
LPVOID lpProduct,
LPBYTE lpbLength,
BOOL bConvertToASCII = TRUE
);
GetDeviceSerialNumber(HANDLE cyHandle,
LPVOID lpSerialNumber,
LPBYTE lpbLength,
BOOL bConvertToASCII = TRUE
);
GetDeviceInterfaceString(HANDLE cyHandle,
LPVOID lpInterfaceString,
LPBYTE lpbLength,
BOOL bConvertToASCII);
我正在尝试导入这些函数,但没有找到正确的数据类型:
[DllImportAttribute("runtime.dll", EntryPoint = "ReadLatch", CallingConvention = CallingConvention.Cdecl)]
static extern int ReadLatch(HANDLE cyHandle, [MarshalAs(UnmanagedType. ??????)] ?????? lpLatch);
[DllImportAttribute("runtime.dll", EntryPoint = "WriteLatch", CallingConvention = CallingConvention.Cdecl)]
static extern int WriteLatch(HANDLE cyHandle,
WORD mask,
WORD latch);
[DllImportAttribute("runtime.dll", EntryPoint = "GetPartNumber", CallingConvention = CallingConvention.Cdecl)]
static extern int GetPartNumber(HANDLE cyHandle,
LPBYTE lpbPartNum);
[DllImportAttribute("runtime.dll", EntryPoint = "GetDeviceProductString", CallingConvention = CallingConvention.Cdecl)]
static extern int GetDeviceProductString(HANDLE cyHandle,
LPVOID lpProduct,
LPBYTE lpbLength,
BOOL bConvertToASCII = TRUE
);
[DllImportAttribute("runtime.dll", EntryPoint = "GetDeviceSerialNumber", CallingConvention = CallingConvention.Cdecl)]
static extern int GetDeviceSerialNumber(HANDLE cyHandle,
LPVOID lpSerialNumber,
LPBYTE lpbLength,
BOOL bConvertToASCII = TRUE
);
[DllImportAttribute("runtime.dll", EntryPoint = "GetDeviceInterfaceString", CallingConvention = CallingConvention.Cdecl)]
static extern int GetDeviceInterfaceString(HANDLE cyHandle,
LPVOID lpInterfaceString,
LPBYTE lpbLength,
BOOL bConvertToASCII);
我在哪里可以找到关于如何表示HANDLE、LPWORD和其他函数的信息,以便我可以调用这些函数?
非托管类型及其托管对应类型:
-
HANDLE
通常用IntPtr
表示。 -
WORD
-UInt16
对于其他的,我们可能需要更多地了解它们是如何使用的。
希望你的API有一些附带的文档来解释参数的作用,因为其中一些不是很明显。
对于这个函数,我们可以做一些假设:ReadLatch(HANDLE cyHandle, LPWORD lpLatch);
假设lpLatch
实际上是一个"out"参数(并且您的返回类型是int
):
[DllImportAttribute("runtime.dll", EntryPoint = "ReadLatch", CallingConvention = CallingConvention.Cdecl)]
static extern int ReadLatch(IntPtr cyHandle, out UInt16 lpLatch);