读取 pendrive 时 CreateFile() 失败,错误代码为 32

CreateFile() failed with error code 32 while reading pendrive

我正在尝试从笔式驱动器 'E:' 读取原始字节,但在尝试使用 CreateFIle() 打开驱动器时出现错误代码 32。我的代码如下:

wchar_t  wszDrive[7];
wszDrive[0] = '\';
wszDrive[1] = '\';
wszDrive[2] = '.';
wszDrive[3] = '\';
wszDrive[4] = 'e';
wszDrive[5] = ':';
wszDrive[6] = '[=10=]';

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.

这意味着已经有一个打开的驱动器句柄,并且该句柄正在使用与您请求的 access/sharing 权限不兼容的 access/sharing 权限。

这就是为什么您无法打开驱动器进行独占访问,但可以打开它进行 read/write 共享 - 驱动器已经在其他地方打开 reading/writing。

如果您想知道确切位置,可以使用 SysInternals Process Explorer 之类的工具来查看哪些进程打开了哪些 files/folders、设备等的句柄