首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

可选(opt)参数的一个有关问题

2013-12-26 
可选(opt)参数的一个问题HANDLE hFileBYTE szFile[4096] {}::ReadFile(hFile, szFile, sizeof(szFile)

可选(opt)参数的一个问题

HANDLE hFile;
BYTE szFile[4096] = {};

::ReadFile(hFile, szFile, sizeof(szFile), NULL, NULL);
这里hFile是一个有效的文件句柄,我发现ReadFile的第四个参数如果传入NULL,这个函数会失败,如果传一个DWORD*,则会成功。但它的第四个参数是可选的,为什么会有这个现象?
[解决办法]
这页 (http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467%28v=vs.85%29.aspx) 说,只有当第五个参数不是 null 的时候,第四个参数才能是 null.
[解决办法]
BOOL WINAPI ReadFile(
  _In_         HANDLE hFile,
  _Out_        LPVOID lpBuffer,
  _In_         DWORD nNumberOfBytesToRead,
  _Out_opt_    LPDWORD lpNumberOfBytesRead,
  _Inout_opt_  LPOVERLAPPED lpOverlapped
);

lpNumberOfBytesRead [out, optional]
A pointer to the variable that receives the number of bytes read when using a synchronous hFile parameter. ReadFile sets this value to zero before doing any work or error checking. Use NULL for this parameter if this is an asynchronous operation to avoid potentially erroneous results.

This parameter can be NULL only when the lpOverlapped parameter is not NULL.

热点排行