Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Windows Win32 disk os VC++
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.,
 
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, 
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
 

Problem:
 
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 4-Mar-13 8:37am
Edited 5-Mar-13 21:08pm
v2
Comments
Michael Haephrati at 4-Mar-13 16:11pm
   
What exactly is your error code?
gssajith87 at 6-Mar-13 2:09am
   
question improved., 87 is the last error i get.,
jan.mach71 at 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:)
Rate this: bad
good
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.,
 
Thanks,
Sajith
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Everytime you use WriteFile, Clear its Buffer.
  Permalink  
Comments
gssajith87 at 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
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 499
1 Maciej Los 325
2 Sergey Alexandrovich Kryukov 234
3 DamithSL 233
4 Mathew Soji 195
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 26 Aug 2013
Copyright © CodeProject, 1999-2014
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