Use CreateFile, ReadFile, Writefile reads the absolute sector under Windows NT2000XP

xiaoxiao2021-03-06  122

http://blog.9cbs.net/apeman9cbs/archive/2004/08/16/76385.aspx

That is to open the file name when CreateFile is specified: "//./device" is OK.

Because the code is short, I don't know, I believe that everyone can understand the code.

In addition, it is read and written, and it is a floppy disk 0 sector. If you want to read other sectors, you can use the API setFilePointer.

The method of reading a sector (below the code presentation reads the data from the 0th sector of the A disk, writing to file boot.bin):

#include #include #include

Void main () {HFILE HFILE; HFILE = CreateFile (".// A:", generic_read, file_share_read, null, open_existing, 0, null; assert (Hfile && "CreateFile Failed!");

PBYTE PBUFFER = (PBYTE) Malloc (512); Assert (PBuffer && "Allocate Memory Failed!"); DWORD DWLEN; ReadFile (HFile, PBuffer, 512, & DWLEN, NULL); File * fp; fp = fopen ("boot. Bin "," WB "); Assert (FP &&" Open File Failed! "); FWRITE (PBuffer, 512, 1, FP); Fclose (FP);

CloseHandle (HFILE);

Free (pbuffer);

Then, the method of the write sector is like this (the following code demonstrates from the Boot.bin to read the data to the A disk 0 sector):

#include #include #include

Void main () {HFILE = CreateFile (".// A:", generic_write, file_share_write, null, open_existing, 0, null; assert (Hfile && "CreateFile Failed!");

PBYTE PBUFFER = (PBYTE) Malloc (512); Assert (PBuffer && "Allocate Memory Failed!"); File * fp; fp = fopen ("boot.bin", "rb"); assert (fp && "open file failed ! "); Fread (PBuffer, 512, 1, FP); Fclose (FP); DWORD DWLEN; Writefile (HFile, PBuffer, 512, & DWLEN, NULL); CloseHandle (HFILE);

Free (pbuffer);

转载请注明原文地址:https://www.9cbs.com/read-126586.html

New Post(0)