如何成功写入字节数?
我正在做一个项目,将代码从C ++转换为C#用于打印机。 我用C#中的SerialPort.Write()
替换C ++中的WriteFile()
。
C#
public void Write( byte[] buffer, int offset, int count )
C ++
BOOL WINAPI WriteFile( _In_ HANDLE hFile, _In_ LPCVOID lpBuffer, _In_ DWORD nNumberOfBytesToWrite, _Out_opt_ LPDWORD lpNumberOfBytesWritten, _Inout_opt_ LPOVERLAPPED lpOverlapped );
在C ++中,我可以获得用lpNumberOfBytesWritten编写的字节数。 如何在C#中做同样的事情?
在c#中我们可以使用如下所示的API调用,
[DllImport("kernel32.dll")] static extern bool WriteFile(IntPtr hFile, byte [] lpBuffer, uint nNumberOfBytesToWrite, out uint lpNumberOfBytesWritten, [In] ref System.Threading.NativeOverlapped lpOverlapped);
有关更多信息,请参阅http://www.pinvoke.net/default.aspx/kernel32.writefile