Cause of error 87 When writefile is written to the disk sector

Source: Internet
Author: User

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 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.