user3466924
user3466924

Reputation: 31

How to get number of bytes written successfully?

I'm doing a project that convert code from C++ to C# for printer. I replace WriteFile() in C++ with SerialPort.Write() in C#.

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
);

In C++ i can get number of bytes written in lpNumberOfBytesWritten. How to do the same thing in C#?

Upvotes: 3

Views: 369

Answers (1)

Vikram Bose
Vikram Bose

Reputation: 3365

In c# We can use API call like below,

[DllImport("kernel32.dll")]
static extern bool WriteFile(IntPtr hFile, byte [] lpBuffer,
uint nNumberOfBytesToWrite, out uint lpNumberOfBytesWritten,
[In] ref System.Threading.NativeOverlapped lpOverlapped);

For more information see http://www.pinvoke.net/default.aspx/kernel32.writefile

Upvotes: 2

Related Questions