在NT和2000下,通过CreateFile来打开需要读写的驱动器,ReadFile、WriteFile来进行磁盘读写。</P><P>下面的代码演示了,如何读写A驱</P><P>/* -----------------------------------------------------</P><P>Read Floppy Disk Sector for win NT/2000<BR>reads [numsec] sectors from [head] [track] [sector]</P><P>-------------------------------------------------------*/</P><P><BR>char* ReadSectors(int head, int sector, int track, int numsec)<BR>{</P><P><BR>// getting logical sector from absolute head/track/sector ...<BR>//计算扇区位置<BR>int LogicalSector = (sector-1) +<BR>(head*SECTORSPERTRACK) +<BR>(track*SECTORSPERTRACK*NUMOFHEADS) ;</P><P>char *buffer ;<BR>HANDLE hDevice ;</P><P><BR>HANDLE hDevice;<BR>char* buffer = (char*)malloc (512*numsec);<BR>strset ( buffer , ' ');<BR>DWORD bytesread ;</P><P>// getting a handle to the drive a: using<BR>// CreateFile () function ....<BR>//打开驱动器 \\.\A:<BR>hDevice = CreateFile("\\\\.\\A:",<BR>GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,<BR>NULL, OPEN_EXISTING, 0, NULL);</P><P>if (hDevice == NULL)<BR>{<BR>MessageBox ("Failed !");<BR>return NULL;<BR>}</P><P><BR>// setting the file pointer to the start of the<BR>// sector we want to read .<BR>//移动文件指针到需要读取位置<BR>SetFilePointer (hDevice,<BR>(LogicalSector*512),<BR>NULL,<BR>FILE_BEGIN);</P><P>// reading sector(s) ...<BR>//读数据<BR>if (!ReadFile ( hDevice,<BR>buffer,<BR>512*numsec,<BR>&bytesread,<BR>NULL) )<BR>{<BR>/*<BR>int err;<BR>char error[10];<BR>err=GetLastError ();<BR>itoa (err, error, 10);<BR>MessageBox (error, "Reading sectors ...Failed ");<BR>return NULL ;<BR>*/<BR>}<BR>//关闭<BR>CloseHandle(hDevice);</P><P>return buffer ;<BR>} |