简体   繁体   English

如何成功写入字节数?

[英]How to get number of bytes written successfully?

I'm doing a project that convert code from C++ to C# for printer. 我正在做一个项目,将代码从C ++转换为C#用于打印机。 I replace WriteFile() in C++ with SerialPort.Write() in C#. 我用C#中的SerialPort.Write()替换C ++中的WriteFile()

C# C#

public void Write(
    byte[] buffer,
    int offset,
    int count
)

C++ 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 . 在C ++中,我可以获得用lpNumberOfBytesWritten编写的字节数。 How to do the same thing in C#? 如何在C#中做同样的事情?

In c# We can use API call like below, 在c#中我们可以使用如下的API调用,

[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 有关更多信息,请参阅http://www.pinvoke.net/default.aspx/kernel32.writefile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用BinaryWriter获取写入字节的长度? - How to get the length of written bytes with BinaryWriter? 获取以字节为单位的大小,当写入文件时,该字符串将占用多少? - Get the size, in bytes, of how much a string will occupy when written to a file? 获取StreamWriter当前写入的文件大小(以字节为单位) - Get the size of the file(in bytes) currently written by the StreamWriter 奇怪的突然错误“要写入的字节数大于指定的ContentLength” - Strange Sudden Error “The number of bytes to be written is greater than the specified ContentLength” MonoDevelop:“要写入的字节数大于指定的ContentLength” - MonoDevelop : “The number of bytes to be written is greater than the specified ContentLength” 如何将数字插入4个字节 - How to insert number into 4 bytes 如何使用Xamarin Android获得可用的字节数 - How can I get the number of bytes availables with Xamarin Android 只写流-使用DataContractSerializer获取写入的字节数 - Write only stream - get written bytes count with DataContractSerializer 如何查找文件中的字节数? - how to find the number of bytes in a file? 找出已经写入文件流的字节数 - find out how many bytes have been written to a filestream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM