如何在c#中定义带有void *buffer的c++函数



我正在尝试使用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#有refout说明符,用于通过引用传递参数变量。您需要使用哪一个取决于参数是输入/输出(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);

最新更新