我试图从笔式驱动器“E:”读取原始字节,但在尝试使用 CreateFIle()
打开驱动器时给出错误代码 32 。我的代码如下:
wchar_t wszDrive[7];
wszDrive[0] = '\\';
wszDrive[1] = '\\';
wszDrive[2] = '.';
wszDrive[3] = '\\';
wszDrive[4] = 'e';
wszDrive[5] = ':';
wszDrive[6] = '\0';
hDevice = CreateFile(wszDrive, //drive name to open
GENERIC_READ | GENERIC_WRITE, ////must be opened with exclusive access(No Sharing)
0, // no access to the drive
NULL, // default security attributes
OPEN_EXISTING, // disposition i.e. if file already exist
0, // file attributes
NULL); // do not copy file attributes
if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
{
printf("CreateFile() failed! from read with error %d.\n", GetLastError());// Program prints this line. with error code 32.
return (FALSE);
}
else
cout << "\nCreateFile() successful! in read";
编辑:
CreateFile()
如果我使用 FILE_SHARE_READ | FILE_SHARE_WRITE
则运行正常,没有错误:
hDevice = CreateFile(wszDrive,
GENERIC_READ |
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
为什么我不能以独占访问权限运行?
最佳答案
错误 32 是 ERROR_SHARING_VIOLATION
.
The process cannot access the file because it is being used by another process.
这意味着该驱动器已经有一个打开的句柄,并且该句柄正在使用与您请求的访问/共享权限不兼容的访问/共享权限。
这就是为什么您无法打开驱动器以进行独占访问,但您可以打开它以进行读/写共享 - 该驱动器已在其他地方打开以进行读/写。
如果你想知 Prop 体在哪里,你可以使用像 SysInternals Process Explorer 这样的工具查看哪些进程打开了哪些文件/文件夹、设备等的句柄。
关于windows - 读取随身碟时 CreateFile() 失败,错误代码为 32,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60801485/