Winapi WriteFile函数写入一个字节

时间:2017-02-04 11:25:40

标签: c++ winapi

我尝试二进制文件写一个字节,然后失败......如何开始工作?

我的代码:

unsigned char b = 0x00; // equal to hex value = 00
/*
 Error: GetLastError()
 ERROR_INVALID_USER_BUFFER
 1784 (0x6F8)
 The supplied user buffer is not valid for the requested operation.
*/
WriteFile(file, ( char *)b, 1, &bytesWritten, NULL);

1 个答案:

答案 0 :(得分:3)

WriteFile定义是

BOOL WINAPI WriteFile(
  _In_        HANDLE       hFile,
  _In_        LPCVOID      lpBuffer,
  _In_        DWORD        nNumberOfBytesToWrite,
  _Out_opt_   LPDWORD      lpNumberOfBytesWritten,
  _Inout_opt_ LPOVERLAPPED lpOverlapped
);

其中 lpBuffer 是指向包含要写入数据的缓冲区的指针。

要获得指向 b 的指针,您需要address of运算符&而不是C-style cast来指示指针:

WriteFile(file, &b, 1, &bytesWritten, NULL);