简体   繁体   中英

How to use ReadFile() in c++ to read bytes of length that are not multiple of 512

I am trying to read raw bytes to a drive. But the function ReadFile() only allowing me to read bytes of length 512 or its multiple. I cannot read bytes of length 10, 180,1000 etc.

DWORD NumberOfBytesRead=0;

ReadFile(hDevice, nullbuffer, (DWORD)512, &NumberOfBytesRead, (LPOVERLAPPED)NULL); //works

ReadFile(hDevice, nullbuffer, (DWORD)1024, &NumberOfBytesRead, (LPOVERLAPPED)NULL); //works

ReadFile(hDevice, nullbuffer, (DWORD)1000, &NumberOfBytesRead, (LPOVERLAPPED)NULL); //error 87

ReadFile(hDevice, nullbuffer, (DWORD)300, &NumberOfBytesRead, (LPOVERLAPPED)NULL);  //error 87

Is there some way to overcome this?

The rules for direct access of the device mandate that you must read aligned blocks of data. That's not something that you can change. It's a hard rule that you must follow.

Given this rule, you must read into a buffer that is a multiple of the required block size. If you wish only to access some part of that buffer, you can pick that part out after you have read from the device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM