C代码
#include
<
windows.h
>
#include < stdio.h >
#pragma comment(lib, "user32.lib");
#pragma comment(lib, "kernel32.lib");
const UINT uSectorSize = 512 ;
const UINT uBegSector = 0 ;
const UINT uSectorNum = 1 ;
const UINT uReadSize = uSectorSize * uSectorNum ;
BYTE bBuffer[uReadSize] = ... {0} ;
const char pDiskPath[] = " //./PHYSICALDRIVE0 " ;
void ShowByteInform ( PBYTE pBuf, UINT uSize )
... {
for ( UINT i = 1 ; i <= uSize; i++ )
...{
printf ( " %02X", pBuf[i-1] ) ;
if ( i % 16 == 0 )
printf ( " " ) ;
else if ( i % 8 == 0 )
printf ( " " ) ;
}
}
int main()
... {
HANDLE hDisk ;
__try ...{
hDisk = CreateFile ( pDiskPath, GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 ) ;
if ( hDisk == INVALID_HANDLE_VALUE )
...{
MessageBox ( 0, "磁盘打开错误!", 0, 0 ) ;
return 0;
}
SetFilePointer ( hDisk, uBegSector * uSectorSize, 0, FILE_BEGIN ) ;
DWORD dwReadByte ;
ReadFile ( hDisk, (LPVOID)bBuffer, uReadSize, &dwReadByte, NULL ) ;
if ( dwReadByte == 0 )
...{
MessageBox ( 0, "磁盘读取错误!", 0, 0 ) ;
return 0;
}
ShowByteInform ( bBuffer, uReadSize ) ;
}
__finally ...{
CloseHandle ( hDisk ) ;
}
return 0;
}
#include < stdio.h >
#pragma comment(lib, "user32.lib");
#pragma comment(lib, "kernel32.lib");
const UINT uSectorSize = 512 ;
const UINT uBegSector = 0 ;
const UINT uSectorNum = 1 ;
const UINT uReadSize = uSectorSize * uSectorNum ;
BYTE bBuffer[uReadSize] = ... {0} ;
const char pDiskPath[] = " //./PHYSICALDRIVE0 " ;
void ShowByteInform ( PBYTE pBuf, UINT uSize )
... {
for ( UINT i = 1 ; i <= uSize; i++ )
...{
printf ( " %02X", pBuf[i-1] ) ;
if ( i % 16 == 0 )
printf ( " " ) ;
else if ( i % 8 == 0 )
printf ( " " ) ;
}
}
int main()
... {
HANDLE hDisk ;
__try ...{
hDisk = CreateFile ( pDiskPath, GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0 ) ;
if ( hDisk == INVALID_HANDLE_VALUE )
...{
MessageBox ( 0, "磁盘打开错误!", 0, 0 ) ;
return 0;
}
SetFilePointer ( hDisk, uBegSector * uSectorSize, 0, FILE_BEGIN ) ;
DWORD dwReadByte ;
ReadFile ( hDisk, (LPVOID)bBuffer, uReadSize, &dwReadByte, NULL ) ;
if ( dwReadByte == 0 )
...{
MessageBox ( 0, "磁盘读取错误!", 0, 0 ) ;
return 0;
}
ShowByteInform ( bBuffer, uReadSize ) ;
}
__finally ...{
CloseHandle ( hDisk ) ;
}
return 0;
}
Delphi代码
program Project1;
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
... {$R *.RES}
const
BytesPerSector = 512 ;
SectorCount = 1 ;
SectorStart = 0 ;
drive = ' /.PHYSICALDRIVE0 ' ;
var
str : string ;
p :pchar;
i :Cardinal;
hDeviceHandle :Thandle;
begin
hDeviceHandle : = CreateFile(drive, GENERIC_READ,
FILE_SHARE_READ OR FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0 , 0 );
if (hDeviceHandle <> INVALID_HANDLE_VALUE) then
begin
p: = allocmem(SectorCount * BytesPerSector);
FileSeek(hDevicehandle,SectorStart * BytesPerSector, 0 );
if FileRead(hDevicehandle,p[ 0 ],SectorCount * BytesPerSector) <> SectorCount * BytesPerSector then
raise exception.create( ' 读取错误 ' );
str: = '' ;
for i: = 0 to 512 - 1 do
begin
if i mod 16 = 0 then
str: = str + format( ' 0x%.8x ' ,[i]);
str: = str + format( ' %.2x ' ,[integer(p[i])]);
if i mod 16 = 15 then
str: = str + # 13 # 10 ;
end;
ShowMessage( str);
freemem(p,SectorCount * BytesPerSector);
closehandle(hDeviceHandle);
end;
end.
... {END}
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
... {$R *.RES}
const
BytesPerSector = 512 ;
SectorCount = 1 ;
SectorStart = 0 ;
drive = ' /.PHYSICALDRIVE0 ' ;
var
str : string ;
p :pchar;
i :Cardinal;
hDeviceHandle :Thandle;
begin
hDeviceHandle : = CreateFile(drive, GENERIC_READ,
FILE_SHARE_READ OR FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0 , 0 );
if (hDeviceHandle <> INVALID_HANDLE_VALUE) then
begin
p: = allocmem(SectorCount * BytesPerSector);
FileSeek(hDevicehandle,SectorStart * BytesPerSector, 0 );
if FileRead(hDevicehandle,p[ 0 ],SectorCount * BytesPerSector) <> SectorCount * BytesPerSector then
raise exception.create( ' 读取错误 ' );
str: = '' ;
for i: = 0 to 512 - 1 do
begin
if i mod 16 = 0 then
str: = str + format( ' 0x%.8x ' ,[i]);
str: = str + format( ' %.2x ' ,[integer(p[i])]);
if i mod 16 = 15 then
str: = str + # 13 # 10 ;
end;
ShowMessage( str);
freemem(p,SectorCount * BytesPerSector);
closehandle(hDeviceHandle);
end;
end.
... {END}