关于ReadFile函数求助

qq_24510399 2014-12-14 09:34:05
本人新手,想写一个直接读写设备物理扇区内容的程序,最后卡在ReadFile函数上,返回错误87,求大神帮忙
int CSectorDlg::ReadSector()
{

char devName[] = "\\\\.\\i:";
byte buffer[8] ;
DWORD bytesPerSector = 10;
HANDLE hFile = CreateFile(devName, GENERIC_READ, FILE_SHARE_READ , NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
if(hFile == INVALID_HANDLE_VALUE)
{
cout<<"CreateFile error: "<<GetLastError()<<std::endl;
return 0;
}
DWORD byteRead ;
SetFilePointer(hFile, 1, NULL, FILE_BEGIN);
ReadFile(hFile, buffer, 10, &byteRead, NULL);
if (byteRead == 0)
{
//<<"ReadFile error:"<<GetLastError()<<endl;
TCHAR str[20];
DWORD dw = GetLastError();
wsprintf(str, "%u", dw);
MessageBox(str, "Show a DWORD", MB_OK);
return -1;
}
CloseHandle(hFile);
return 0;
}
...全文
673 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2014-12-24
  • 打赏
  • 举报
回复
试试这个吧:
TCHAR devName[256] = { 0 };
GetVolumeNameForVolumeMountPoint(TEXT("I:\"), devName, 256);
HANDLE hFile = CreateFile(devName, GENERIC_READ, FILE_SHARE_READ , NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
jacksonfan 2014-12-16
  • 打赏
  • 举报
回复
引用 6 楼 qq_24510399 的回复:
[quote=引用 5 楼 jacksonfan 的回复:] [quote=引用 3 楼 qq_24510399 的回复:] [quote=引用 1 楼 jacksonfan 的回复:] 87 The parameter is incorrect. ERROR_INVALID_PARAMETER 查看MSDN,该函数的参数说明吧
看过MSDN该函数说明,函数类型,使用应该没错。从网上看到的其他人的样例也都是这样的。[/quote] 提示是参数错误,肯定是有问题的 lpOverlapped Pointer to an OVERLAPPED structure. This structure is required if hFile was created with FILE_FLAG_OVERLAPPED. If hFile was not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the read operation starts at the current file position and ReadFile does not return until the operation has been completed. If hFile is not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the read operation starts at the offset specified in the OVERLAPPED structure. ReadFile does not return until the read operation has been completed. [/quote] 那我该怎么改程序呢?本人新手,怎么能确定lpOverlapped is or is not NULL呢?[/quote] Create的时候加上FILE_FLAG_OVERLAPPED标记
「已注销」 2014-12-15
  • 打赏
  • 举报
回复
buffer 大小为 8 字节你读取 10 字节即时成功也会有问题吧
qq_24510399 2014-12-15
  • 打赏
  • 举报
回复
引用 5 楼 jacksonfan 的回复:
[quote=引用 3 楼 qq_24510399 的回复:] [quote=引用 1 楼 jacksonfan 的回复:] 87 The parameter is incorrect. ERROR_INVALID_PARAMETER 查看MSDN,该函数的参数说明吧
看过MSDN该函数说明,函数类型,使用应该没错。从网上看到的其他人的样例也都是这样的。[/quote] 提示是参数错误,肯定是有问题的 lpOverlapped Pointer to an OVERLAPPED structure. This structure is required if hFile was created with FILE_FLAG_OVERLAPPED. If hFile was not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the read operation starts at the current file position and ReadFile does not return until the operation has been completed. If hFile is not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the read operation starts at the offset specified in the OVERLAPPED structure. ReadFile does not return until the read operation has been completed. [/quote] 那我该怎么改程序呢?本人新手,怎么能确定lpOverlapped is or is not NULL呢?
jacksonfan 2014-12-15
  • 打赏
  • 举报
回复
87 The parameter is incorrect. ERROR_INVALID_PARAMETER 查看MSDN,该函数的参数说明吧
jacksonfan 2014-12-15
  • 打赏
  • 举报
回复
引用 3 楼 qq_24510399 的回复:
[quote=引用 1 楼 jacksonfan 的回复:] 87 The parameter is incorrect. ERROR_INVALID_PARAMETER 查看MSDN,该函数的参数说明吧
看过MSDN该函数说明,函数类型,使用应该没错。从网上看到的其他人的样例也都是这样的。[/quote] 提示是参数错误,肯定是有问题的 lpOverlapped Pointer to an OVERLAPPED structure. This structure is required if hFile was created with FILE_FLAG_OVERLAPPED. If hFile was not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the read operation starts at the current file position and ReadFile does not return until the operation has been completed. If hFile is not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the read operation starts at the offset specified in the OVERLAPPED structure. ReadFile does not return until the read operation has been completed.
qq_24510399 2014-12-15
  • 打赏
  • 举报
回复
引用 2 楼 SXJIAKE 的回复:
buffer 大小为 8 字节你读取 10 字节即时成功也会有问题吧
这地方可能会出问题,但是之前buffer设置100个字节也不行呀
qq_24510399 2014-12-15
  • 打赏
  • 举报
回复
引用 1 楼 jacksonfan 的回复:
87 The parameter is incorrect. ERROR_INVALID_PARAMETER 查看MSDN,该函数的参数说明吧
看过MSDN该函数说明,函数类型,使用应该没错。从网上看到的其他人的样例也都是这样的。

2,640

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 硬件/系统
社区管理员
  • 硬件/系统社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧