Click here to Skip to main content
11,922,416 members (30,975 online)
Rate this:
Please Sign up or sign in to vote.
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.


This what the call is.,

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:
hFile = _CreateFile(szImageFile, GENERIC_WRITE | GENERIC_READ, 


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.,


Posted 4-Mar-13 8:37am
Edited 5-Mar-13 21:08pm
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: , 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:)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.,

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Everytime you use WriteFile, Clear its Buffer.
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.,

Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.

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

  Print Answers RSS
0 OriginalGriff 453
1 Dave Kreskowiak 335
2 Sergey Alexandrovich Kryukov 263
3 Richard MacCutchan 225
4 ppolymorphe 175
0 OriginalGriff 7,231
1 KrunalRohit 4,530
2 Sergey Alexandrovich Kryukov 3,356
3 George Jonsson 2,865
4 Suvendu Shekhar Giri 2,216

Advertise | Privacy | Mobile
Web01 | 2.8.151120.1 | Last Updated 26 Aug 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100