C++采用ring3读取MBR实例

时间:2022-03-09 10:28:42

本文实例讲述了C++采用ring3读取MBR的方法,分享给大家供大家参考。具体实现方法如下:

复制代码 代码如下:
DWORD dwBytesReturned = 0; 
    BYTE bytBuffer_1[512]; 
    BYTE bytBuffer_2[512]; 
    CHAR string[2048]; 
    HANDLE hDevice, hDriver; 
    BOOL bRet; 
 
    hDevice = CreateFile("\\\\.\\PhysicalDrive0", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); 
 
    if(hDevice == INVALID_HANDLE_VALUE) 
    { 
        printf("\nFailed - CreateFile - Open the PhysicalDrive0.\n"); 
        return 0; 
    } 
 
    bRet = ReadFile(hDevice, (LPVOID)bytBuffer_1, 512, &dwBytesReturned, NULL); 
    if(bRet == FALSE) 
    { 
        printf("\nFailed - ReadFile - the first one.\n"); 
        return 0; 
    } 
    printf("aaaa"); 
    Sleep(1000*3); 
    printf("\nRead MBR using the ReadFile function...\n"); 
    printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - -"); 
 
    sprintf(string, "\n"); 
 
    for(DWORD i = 0; i < 512; i++) 
    { 
        sprintf(string, "%s %02X", string, bytBuffer_1[i]); 
 
        if(((i + 1) % 16) == 0) 
            sprintf(string, "%s\n", string); 
 
        if(((i + 1) % 16) == 8) 
            sprintf(string, "%s -", string); 
    } 
 
    printf("%s", string); 
 
    printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - -");

 

 

打印结果如下:

C++采用ring3读取MBR实例

希望本文所述对大家的C++程序设计有所帮助。