Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
WriteFile() Win32 call with input buffer size = 512 Fails., when i try to write to the disk that has bytes per sector = 4096.[3 TB disk]. Same WriteFile with input buffer size = 4096 works fine.,

Can any body explain this behavior.

Explanation

This what the call is.,

C++
res &= WriteFile(hFile, m_Footer,sizeof(VHDFooter), &dwNumBytes, NULL);

hFile - Handle of a VHD File.,

m_Footer - VHD File Footer.,

sizeof(VHDFooter) = 512 always .,

dwNumBytes = o/p buffer.,

My Create File looks like this:
C++
hFile = _CreateFile(szImageFile, GENERIC_WRITE | GENERIC_READ, 
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);



Problem:

C++
res &= WriteFile(hFile, m_Footer, sizeof(VHDFooter), &dwNumBytes, NULL);// Fails with last error  = 87

res &= WriteFile(hFile, m_Footer,4096, &dwNumBytes, NULL); // Success., if input buffer size= 4096


Disk property.,4096 bytes per sector.,

NOTE: For 512 bytes per sector., the first function call works well.,


Thanks,

Sajith
Posted
Updated 5-Mar-13 20:08pm
v2
Comments
Michael Haephrati 4-Mar-13 16:11pm    
What exactly is your error code?
gssajith87 6-Mar-13 2:09am    
question improved., 87 is the last error i get.,
jan.mach71 6-Mar-13 2:47am    
If it's decimal 87 then check this: http://support.microsoft.com/kb/110148 , but as your snippet does not have any overlapped, it seems you will have to investigate and find solution yourself. Just a note: you are quick at changing sector sizes:)

Hi Thanks all,

I was passing the handle to writeFile which was created by CREATEFILE with FILE_FLAG_NO_BUFFERING attribute, MSDN has clean link saying that, if i have a handle passed to writefile with FILE_FLAG_NO_BUFFERING , i should pass the input buffer equal or multiples of the Harddisk SECTOR Size, For Example, if my HDD sector size is 4096 BYTES, i will have to give input Buffer size of writeFile as (4096, 2*4096, 3*4096) etc.,

To make the writeFile work for input buffer size of lesser value than sector size or an imperfect multiple of sector-size say(512, 2048 etc.,), you will have to open the file handle using FILE_ATTRIBUTE_NORMAL attribute of CreateFile Function.

Hope this helps.,

Thanks,
Sajith
 
Share this answer
 
Everytime you use WriteFile, Clear its Buffer.
 
Share this answer
 
Comments
gssajith87 7-Mar-13 8:56am    
Thanks Desai.,
My question is why do i get an INVALID_PARAMETER = 87., Error when i call the writefile with correct parameters., and why does it not throw any error when i change the buffer size.,

-Sajith
Actually it is very very simple...
All Writes must be Sector sized.
4096 byte sector needs a 4096 byte buffer - and a 512 byte sector requires a 512 byte buffer to be written. It is truly as simple as that.

Which of course does cause problems but that is what happens when writting to the hardware directly.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900