问题描述
我正在尝试编写代码,使用CreateFile和WriteFile将文件写入网络共享.
I am trying to write a code which writes a file to a network share, using CreateFile and WriteFile.
我已经成功完成了此操作,但是在进行测试时,我注意到该程序在写入文件时未检测到网络断开连接.
I have succeeded on doing so, but while I was testing I noticed that the program does not detect network disconnection while file is being written.
我在各个论坛中进行了搜索,发现了一个相关主题:
I searched through the forums and found one related topic:
http://social.msdn.microsoft.com/Forums/en/vcgeneral/thread/a5e9514b-53e0-410c-a975-41a5ca91e4be
http://social.msdn.microsoft.com/Forums/en/vcgeneral/thread/a5e9514b-53e0-410c-a975-41a5ca91e4be
---
我发现,使用GENERIC_WRITE | GENERIC_READ选项,WriteFile可以工作.由于它不返回GetLastError ERROR_IO_PENDING,因此它似乎始终在同步模式下工作.
一旦网络中断,WriteFile始终返回true,因此无法向调用方指示错误.
实际上,异步模式正在工作,因为GetOverlappedResult返回了良好的值.
但是与WriteFile一样,GetOverlappedResult返回,但没有错误指示.
如果使用GENERIC_WRITE创建文件,则WriteFile以异步模式工作(GetLastError = ERROR_IO_PENDING),并且WriteFile在网络断开时正确地向调用方指示错误.
因此,通过将CreateFile访问权限从GENERIC_WRITE | GENERIC_READ更改为GENERIC_WRITE,可以使程序正常运行.无论如何,在此应用中无需读取文件.
What I have found is that with the GENERIC_WRITE|GENERIC_READ options, the WriteFile works, It appears to work always in sync mode as it does not return GetLastError ERROR_IO_PENDING.
Once the network goes down, WriteFile always returns with true, and thus cannot indicate an error to the caller.
Actually Async mode is working, as GetOverlappedResult returns good values.
But likewise to WriteFile, GetOverlappedResult returns, but with no error indication.
If the file is created with GENERIC_WRITE, WriteFile works in async mode (GetLastError = ERROR_IO_PENDING) and WriteFile correctly indicates an error to the caller on network disconnect.
So I can make my program operate correctly by changing the CreateFile access from GENERIC_WRITE|GENERIC_READ to GENERIC_WRITE. In this app read of the file is not needed anyway.
---
此人通过将访问权限从GENERIC_WRITE | GENERIC_READ更改为GENERIC_WRITE来解决此问题,但是由于我必须在写入之前从文件中读取标头,因此我也需要读取访问权限.
This person worked around the problem by changing the access from GENERIC_WRITE|GENERIC_READ to GENERIC_WRITE but since I have to read a header from the file before writing, I need read access as well in my case.
我什至不确定这是否是CreateFile/WriteFile中的错误,或者是否有任何方法可以在CreateFile中设置参数,以便WriteFile可以正确检测到网络断开连接.
I am not even sure if this is a bug in CreateFile/WriteFile, or if there is any way to set parameters in CreateFile so that it WriteFile will properly detect network disconnection.
对此有什么解决办法吗?
Is there any solution to this?
推荐答案
请您向我们提供有关此问题的更多信息,例如系统环境以及有关此问题的整个代码,包括createfile和writefile函数
Would you please provide us more information about this issue, like system environment, and the whole codes about this issue, include createfile and writefile function.
最好的问候,
Jesse
这篇关于写入网络共享时,WriteFile无法检测到网络连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!