WriteFile失败,错误代码87,每扇区磁盘4096字节 [英] WriteFile Failure with error code 87 in 4096 Bytes per sector Disk

查看:220
本文介绍了WriteFile失败,错误代码87,每扇区磁盘4096字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WriteFile()输入缓冲区大小= 512的Win32调用失败。当我尝试写入每扇区有字节数= 4096的磁盘时。[3 TB磁盘]。输入缓冲区大小= 4096的相同WriteFile工作正常。



任何机构都可以解释这种行为。



说明



这就是电话。



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



hFile - 处理VHD文件。,



m_Footer - VHD文件页脚。,



sizeof(VHDFooter)= 512总是。,



dwNumBytes = o / p buffer。,



我的创建文件如下所示:

 hFile = _CreateFile(szImageFile,GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,
CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);







问题:



 res& = WriteFile(hFile,m_Footer, sizeof (VHDFo oter),& dwNumBytes,NULL);  //  失败,最后一次错误= 87  

res& = WriteFile(hFile,m_Footer, 4096 ,& dwNumBytes,NULL); // 成功。,如果输入缓冲区大小= 4096





磁盘属性。,每扇区4096字节。



注意:对于每扇区512字节,第一个函数调用效果很好。





谢谢,



Sajith

解决方案

您好,谢谢大家,



我正在将句柄传递给由CREATEFILE创建的带有FILE_FLAG_NO_BUFFERING属性的writeFile,MSDN有一个干净的链接,如果我有一个传递给带有FILE_FLAG_NO_BUFFERING的writefile的句柄,我应该将输入缓冲区传递给Harddisk SECTOR Size的等于或倍数,例如,如果我的硬盘扇区大小是4096 BYTES,我将不得不输入writeFile的缓冲区大小为(4096,2 * 4096,3 * 4096)等,



使writeFile适用于输入缓冲区大小小于扇区大小或不完美倍数的值如果是扇区大小(512,2048等),则必须使用CreateFile函数的FILE_ATTRIBUTE_NORMAL属性打开文件句柄。



希望这会有所帮助。,



谢谢,

Sajith


每次使用WriteFile时,清除它的缓冲区。

实际上它非常简单......

所有写入必须是扇区大小。

4096字节扇区需要4096字节缓冲区 - 和512字节扇区需要写入512字节缓冲区。这真的很简单。



这当然会导致问题,但这就是直接写入硬件时会发生的情况。


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

解决方案

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


Everytime you use WriteFile, Clear its Buffer.


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.


这篇关于WriteFile失败,错误代码87,每扇区磁盘4096字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆