如何访问USB驱动器的特定地址

时间:2010-08-02 12:26:21

标签: winapi

我需要访问USB记忆棒的分区表并更改分区0的第一个字节以使其可引导。我还想将bin文件复制到该USB驱动器的特定地址。那么任何人都可以告诉我如何执行这些任务吗?

我非常需要一些例子......如果你能提供这类作品的任何链接,那就太好了。我在C工作。

我处于起步阶段。这是我的代码:

// DeviceIoControl.cpp : Defines the entry point for the console application.
//

#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("\\\\.\\H:"),     // drive to open
                    GENERIC_READ | GENERIC_WRITE, // 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)
    {
        printf ("BootIndicator          %lld\n", pdg.BootIndicator);
        printf ("HiddenSectors          %lld\n", pdg.HiddenSectors);
        printf ("PartitionLength        %u\n", pdg.PartitionLength);
        printf ("PartitionNumber        %u\n", pdg.PartitionNumber);
        printf ("PartitionType          %x\n", pdg.PartitionType);
        printf ("RecognizedPartition    %s\n", pdg.RecognizedPartition);
        printf ("RewritePartition       %ld.\n", pdg.RewritePartition);
        printf ("StartingOffset         %lld\n", pdg.StartingOffset);
    }

    else
    {
        printf ("PARTITION INFORMATION failed. Error %ld.\n", GetLastError ());
    }

    getchar();

    return ((int)bResult);
}

1 个答案:

答案 0 :(得分:1)

你必须在代码中这样做吗?您可以使用内置的diskpart工具通过this教程使USB驱动器可启动。