WinApi:创建磁盘时拒绝访问

时间:2016-02-08 09:01:48

标签: c++ windows winapi disk-partitioning

我使用管理员权限运行以下代码:

HANDLE hDevice = INVALID_HANDLE_VALUE;  // handle to the drive to be examined 
BOOL bResult = FALSE;                 // results flag
DWORD junk = 0;                     // discard results

hDevice = CreateFileW(L"\\\\.\\PhysicalDrive0",          // 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
{
    std::cout << "invalid handle" << std::endl;
    return -1;
}
std::cout << "handle " << hDevice << std::endl;

DRIVE_LAYOUT_INFORMATION_EX        drv_layout_info_ex;
CREATE_DISK        disk;
INT                n;

ZeroMemory(&disk, sizeof(CREATE_DISK));
disk.PartitionStyle = PARTITION_STYLE_MBR;
disk.Mbr.Signature = 0x7B060725;
bResult = ::DeviceIoControl(hDevice, IOCTL_DISK_CREATE_DISK, &disk,
    sizeof(CREATE_DISK), NULL,
    0, &junk, NULL);
if (!bResult)
{
    std::cout << "IOCTL_DISK_CREATE_DISK failed due to " << GetLastError() << std::endl;
    return FALSE;
}

我得到了输出:

handle 00000048
IOCTL_DISK_CREATE_DISK failed due to 5

哪个访问被拒绝。有什么问题?

0 个答案:

没有答案