直接访问硬盘

问题描述:

我想使用下面的代码打印引导扇区,但有错误。直接访问硬盘

#include <windows.h> 
#include <stdio.h> 
#include <iostream> 
#include <math.h> 


using namespace std; 
short ReadSect 
    (const char * _dsk, // disk to access 
    char *&_buff,   // buffer where sector will be stored 
    unsigned int _nsect // sector number, starting with 0 
    ) 
{ 
DWORD dwRead; 
HANDLE 
hDisk=CreateFile(_dsk,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0); 
if(hDisk==INVALID_HANDLE_VALUE) // this may happen if another program is 
already reading from disk 
{ 
    CloseHandle(hDisk); 
    return 1; 
} 
SetFilePointer(hDisk,_nsect*512,0,FILE_BEGIN); // which sector to read 

ReadFile(hDisk,_buff,512,&dwRead,0); // read sector 
CloseHandle(hDisk); 
return 0; 
} 

int main() 
{ 
char * drv="\\\\.\\C:"; 
char *dsk=" \\\\.\\PhysicalDrive0"; 
int sector=0; 
int b = 1; 

char *buff=new char[512]; 
ReadSect(dsk,buff,sector); 
if((unsigned char)buff[510]==0x55 && (unsigned char)buff[511]==0xaa) cout 
<<"Disk is bootable!"<<endl; 
else printf("%02hhX\n",(unsigned int)(unsigned char)buff[511]); 

printf("\n"); 
while (b<513) 
{ 
    if (b%16==0) 
    printf(" %02hhX\n",(unsigned int)(unsigned char)buff[b-1]); 

    else 
    printf (" %02hhX ",(unsigned int)(unsigned char)buff[b-1]); 
    b++; 
} 
getchar(); 
} 

微软视觉工作室不是打印引导扇区的十六进制数字,而是打印出“CD”流。什么是错误以及如何解决这个问题?谁能帮忙?

Photo of output

+3

您应该检查是否所有的函数调用成功。例如,'ReadFile'可能会失败,在这种情况下'buf'只是未初始化的内存([MSVC初始化为0xCDCDCDCD ...或其他调试版本的值](https://stackoverflow.com/questions/370195 /时,和为什么,意志的-OS-初始化内存到0XCD-0xdd-等-上自由的新的malloc - ))。 – Cornstalks

+0

我在ReadFile中检查_buff的值,并在MSVC中使用调试器在ReadSect中缓冲,并显示未知符号流。在将其转换为十六进制值后,它将CDCDCDCD ...显示为输出,这意味着ReadFile或ReadSect不起作用... – Nimportequi

+2

这不是我确保所有函数调用都成功的意思。例如,['ReadFile'返回'BOOL',其中'TRUE'表示成功,'FALSE'表示失败](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365467(v = vs.85)的.aspx)。您应该检查所有函数调用的状态,以查看它们是否返回任何错误。 – Cornstalks

首先,启动它以管理员身份从

char *dsk=" \\\\.\\PhysicalDrive0"; 

删除空间必须

char *dsk="\\\\.\\PhysicalDrive0"; 

而且,如果你使用的char * DSK,使修改:

hDisk = CreateFile(_dsk,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);

必须是: CreateFileA的(......)

#include <windows.h> 
#include <stdio.h> 
#include <iostream> 
#include <math.h> 


using namespace std; 
short ReadSect 
(const char * _dsk, // disk to access 
    char *&_buff,   // buffer where sector will be stored 
    unsigned int _nsect // sector number, starting with 0 
) 
{ 
    DWORD dwRead; 
    HANDLE 
     hDisk = CreateFileA(_dsk, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0); 
    //CreateFile() 
    if (hDisk == INVALID_HANDLE_VALUE) // this may happen if another program is already reading from disk 
    { 
     CloseHandle(hDisk); 
    return 1; 
    } 
    SetFilePointer(hDisk, _nsect * 512, 0, FILE_BEGIN); // which sector to read 

    ReadFile(hDisk, _buff, 512, &dwRead, 0); // read sector 
    CloseHandle(hDisk); 
    return 0; 
} 

int main() 
{ 
    char * drv = "\\\\.\\C:"; 
    char *dsk = "\\\\.\\PhysicalDrive0"; 
    int sector = 0; 
    int b = 1; 

    char *buff = new char[512]; 
    ReadSect(dsk, buff, sector); 
    if ((unsigned char)buff[510] == 0x55 && (unsigned char)buff[511] == 0xaa) cout 
     << "Disk is bootable!" << endl; 
    else printf("%02hhX\n", (unsigned int)(unsigned char)buff[511]); 

    printf("\n"); 
    while (b<513) 
    { 
     if (b % 16 == 0) 
      printf(" %02hhX\n", (unsigned int)(unsigned char)buff[b - 1]); 

     else 
      printf(" %02hhX ", (unsigned int)(unsigned char)buff[b - 1]); 
     b++; 
    } 
    getchar(); 
} 

photo of output

WinAPI Unicode and ANSI functions

+0

它的工作原理!你帮了我很多!谢谢,Михайл! Спасибобольшое! – Nimportequi