使用 DeviceIoControl 函数读取 MBR 时出现问题

2024-02-18

我在调用 DeviceIoControl 函数读取可移动设备的 MBR 时遇到错误。错误代码是5。这意味着访问被拒绝!我使用的是Windows XP SP2。

#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);
}

使用 FILE_READ_ATTRIBUTES 打开它。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 DeviceIoControl 函数读取 MBR 时出现问题 的相关文章

随机推荐