打开闪存驱动器的句柄给我一个“访问被拒绝”的消息错误代码
我想知道为什么当我尝试创建 USB 闪存驱动器的句柄时,我收到路径未找到错误。
HANDLE aFile = CreateFile(_T("\\\\.\\F:\\"), GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (aFile == INVALID_HANDLE_VALUE)
{
printf("\n");
printf("Bad handle value. Error %d \n", GetLastError());
}
从那里我想将 512 字节的流(引导扇区)读取到 .bin 文件,但我似乎无法首先完成句柄创建。 Windows 是否会阻止应用程序打开可移动驱动器的句柄?
I would like to know why when I try to create a handle to a USB flash drive, I receive a path not found error.
HANDLE aFile = CreateFile(_T("\\\\.\\F:\\"), GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (aFile == INVALID_HANDLE_VALUE)
{
printf("\n");
printf("Bad handle value. Error %d \n", GetLastError());
}
From there I want to read a stream of 512 bytes (the boot sector) to a .bin file, but I can't seem to get past the handle creation first. Does Windows prevent applications from opening a handle to removable drives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该代码有两个问题。首先,路径。您实际上是在指定驱动器的根文件夹;你真正需要的是音量。从路径中删除结尾的反斜杠;即 _T("\\\\.\\F:")。其次,您需要指定 FILE_SHARE_READ |文件共享写入;您正尝试以独占模式打开它,这将会失败。有关详细信息,请参阅 CreateFile 的 MSDN 文档。
That code has two problems. First, the path. You are actually specifying the root folder of the drive; what you really need is the volume. Remove the trailing backslash from the path; i.e. _T("\\\\.\\F:"). Secondly, you need to specify FILE_SHARE_READ | FILE_SHARE_WRITE; you are trying to open it in exclusive mode, and this will fail. See MSDN documentation for CreateFile for more information.