我正在尝试使用c++ DLL S6145usb.dll集成照片打印机CHC-S6145。这里有一个函数定义如下。我想知道c# DLL导入的映射是什么
功能名称- chcusb_getPrinterInfo
格式BOOL APIENTRY chcusb_getPrinterInfo (WORD tagNumber, void *rBuffer, DWORD *rLen);
功能细节根据标签标识获取指定打印机信息
BOOL
在Win32中为32位整数。PInvoke有一个Boolean
类型,可以通过MarshalAs
封送成一个32位整数。
WORD
是一个16位无符号整数。c#有一个Uint16
类型。
void*
是一个原始指针。c#使用(U)IntPtr
类型。
DWORD
是一个32位无符号整数。c#有一个UInt32
类型。
另一方面,DWORD*
是指向DWORD
的指针。c#有ref
和out
说明符,用于通过引用传递参数变量。您需要使用哪一个取决于参数是输入/输出(ref
)还是仅输出(out
)。
试试这样写:
[DLLImport("S6145usb.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern Boolean chcusb_getPrinterInfo(UInt16 tagNumber, IntPtr rBuffer, ref UInt32 rLen);
...
UInt32 bufLen = ...;
IntPtr buffer = Marshal.AllocHGlobal((int)bufLen);
chcusb_getPrinterInfo(..., buffer, ref bufLen);
// use buffer as needed...
Marshal.FreeHGlobal(buffer);