2010-08-02 52 views
0

我需要访问USB记忆棒的分区表,并更改分区0的第一个字节以使其可启动。而且我还想将bin文件复制到该USB驱动器的特定地址。那么,任何人都可以告诉我如何执行这些任务?如何访问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

你必须在代码中完成它吗?您可以使用内置的diskpart工具,通过以下this教程来启动USB驱动器。

+0

我害怕,但我必须在代码中做到这一点。 – FlintOff 2010-08-02 15:13:57

相关问题