I am writing a tool today to write some data to the absolute sector of the disk.
But readfile is okay, but writefile always reports 87 errors (incorrect parameters ).
I checked it on Google for a while and found that there were a lot of people who encountered the problem. Unfortunately, there was no correct answer ....
No, but I still had to solve it by myself ~ Haha
Remember it so that you do not forget it...
In fact, it mainly refers to the size of the written data, which must be a multiple of bytesofsector size, which is generally 512 bytes, but readfile does not matter...
In addition, msdn also said that it is best to use writefile beforeFsctl_lock_volumeOrFsctl_dismount_volumeLock or detach the disk, but it seems that it has no effect after I try it...
CodeThe excerpt is as follows:
1 Uint cdevinfo: Write (lpvoid pdata, uint nsize)
2 {
3 If ( ! Pdata && Nsize <= 0 )
4 Return 0 ;
5
6 Tchar szdevname [ 30 ];
7 _ Stprintf (szdevname, _ T ( " \\\\. \ Physicaldrive % d " ), M_ndevnum); // devicenumber
8
9 Handle hdev = : Createfile (szdevname, generic_write, file_assist_read | File_into_write, null, open_existing, null, null );
10 If (Hdev = Invalid_handle_value)
11 Return 0 ;
12
13 DWORD dwcount;
14 If ( ! : Deviceiocontrol (hdev, fsctl_lock_volume, null, 0 , Null, 0 , & Dwcount, null ))
15 Return 0 ;
16
17 DWORD dwwritten ( 0 );
18 : Setfilepointer (hdev, sector_num * Getsectorsize (), null, file_begin );
19 If ( ! : Writefile (hdev, pdata, nsize, & Dwwritten, null ))
20 Dwwritten = 0 ;
21
22 : Deviceiocontrol (hdev, fsctl_unlock_volume, null, 0 , Null, 0 , & Dwcount, null );
23 : Closehandle (hdev );
24 Return Dwwritten;
25 }