C++ 使用DeviceIoControl功能读取MBR时出现问题
调用DeviceIoControl函数读取可移动设备的MBR时出错。错误代码是5。这意味着访问被拒绝!我使用的是WindowsXPSP2C++ 使用DeviceIoControl功能读取MBR时出现问题,c++,winapi,C++,Winapi,调用DeviceIoControl函数读取可移动设备的MBR时出错。错误代码是5。这意味着访问被拒绝!我使用的是WindowsXPSP2 #include "stdafx.h" #include <windows.h> #include <winioctl.h> #include <stdio.h> BOOL GetMBR(PARTITION_INFORMATION *pdg) { HANDLE hDevice;
#include "stdafx.h"
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>
BOOL GetMBR(PARTITION_INFORMATION *pdg)
{
HANDLE hDevice; // handle to the drive to be examined
BOOL bResult; // results flag
DWORD junk; // discard results
hDevice = CreateFile(TEXT("\\\\.\\G:"), // drive to open
0, // no access to the drive
FILE_SHARE_READ | // share mode
FILE_SHARE_WRITE,
NULL, // default security attributes
OPEN_EXISTING, // disposition
0, // file attributes
NULL // do not copy file attributes
);
if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
{
printf("CreateFile() failed!\n");
return (FALSE);
}
bResult = DeviceIoControl(
hDevice, // device to be queried
IOCTL_DISK_GET_PARTITION_INFO, // operation to perform
NULL, 0, // no input buffer
pdg, sizeof(*pdg), // output buffer
&junk, // # bytes returned
(LPOVERLAPPED) NULL // synchronous I/O
);
CloseHandle(hDevice);
return (bResult);
}
int _tmain(int argc, _TCHAR* argv[])
{
PARTITION_INFORMATION pdg; // disk drive geometry structure
BOOL bResult; // generic results flag
ULONGLONG DiskSize; // size of the drive, in bytes
bResult = GetMBR(&pdg);
if (bResult)
{
}
else
{
printf ("GetDriveGeometry() failed. Error %ld.\n", GetLastError ());
}
getchar();
return ((int)bResult);
}
#包括“stdafx.h”
#包括
#包括
#包括
BOOL GetMBR(分区信息*pdg)
{
HANDLE hDevice;//要检查的驱动器的句柄
BOOL-bResult;//结果标志
DWORD junk;//放弃结果
hDevice=CreateFile(文本(“\\.\\G:”),//要打开的驱动器
0,//无法访问该驱动器
文件_共享|读取|//共享模式
文件共享写入,
NULL,//默认安全属性
打开\u现有,//处置
0,//文件属性
NULL//不复制文件属性
);
如果(hDevice==无效的\u句柄\u值)//无法打开驱动器
{
printf(“CreateFile()失败!\n”);
返回(假);
}
bResult=设备控制(
hDevice,//要查询的设备
IOCTL\u DISK\u GET\u PARTITION\u INFO,//要执行的操作
NULL,0,//没有输入缓冲区
pdg,sizeof(*pdg),//输出缓冲区
&垃圾,返回的字节数为//#
(LPOVERLAPPED)空//同步I/O
);
闭合手柄(hDevice);
返回(bResult);
}
int _tmain(int argc,_TCHAR*argv[]
{
分区信息pdg;//磁盘驱动器几何结构
BOOL-bResult;//通用结果标志
ULONGLONG DiskSize;//驱动器的大小,以字节为单位
bResult=GetMBR(&pdg);
if(bResult)
{
}
其他的
{
printf(“GetDriveGeometry()失败。错误%ld。\n”,GetLastError());
}
getchar();
返回((int)bResult);
}
使用文件读取属性打开它。我很确定您需要某种类型的驱动器读取权限,很可能是文件读取属性。谢谢。现在没有错误了。@Luke把它作为一个答案贴出来,然后@MrFlint确认一下。@Luke:请这样做,谢谢。