原作者姓名 N/A
文章原始出處: http://www.codeguru.com/system/ReadSector.html
正文
聞怡洋譯
轉載請與作者聯系
在NT和2000下,通過CreateFile來打開需要讀寫的驅動器,ReadFile、WriteFile來進行磁盤讀寫。
下面的代碼演示了,如何讀寫A驅
/* -----------------------------------------------------
Read Floppy Disk Sector for win NT/2000
reads [numsec] sectors from [head] [track] [sector]
-------------------------------------------------------*/
char* ReadSectors(int head, int sector, int track, int numsec)
{
// getting logical sector from absolute head/track/sector ...
//計算扇區位置
int LogicalSector = (sector-1) +
(head*SECTORSPERTRACK) +
(track*SECTORSPERTRACK*NUMOFHEADS) ;
char *buffer ;
HANDLE hDevice ;
HANDLE hDevice;
char* buffer = (char*)malloc (512*numsec);
strset ( buffer , ' ');
DWORD bytesread ;
// getting a handle to the drive a: using
// CreateFile () function ....
//打開驅動器 \\.\A:
hDevice = CreateFile("\\\\.\\A:",
GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (hDevice == NULL)
{
MessageBox ("Failed !");
return NULL;
}
// setting the file pointer to the start of the
// sector we want to read .
//移動文件指針到需要讀取位置
SetFilePointer (hDevice,
(LogicalSector*512),
NULL,
FILE_BEGIN);
// reading sector(s) ...
//讀數據
if (!ReadFile ( hDevice,
buffer,
512*numsec,
&bytesread,
NULL) )
{
/*
int err;
char error[10];
err=GetLastError ();
itoa (err, error, 10);
MessageBox (error, "Reading sectors ...Failed ");
return NULL ;
*/
}
//關閉
CloseHandle(hDevice);
return buffer ;
}