如何成功写入字节数?

时间:2014-03-27 03:33:47

标签: c#

我正在做一个将代码从C ++转换为C#for printer的项目。 我将C ++中的WriteFile()替换为C#中的SerialPort.Write()

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#中做同样的事情?

1 个答案:

答案 0 :(得分:2)

在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

相关问题