问题描述
我正在为大学的TFTP服务器程序,这需要它打开读取文件的独占访问。因此,它可以被配置,如果一个文件是由它等待文件变得解锁。另一进程锁定
I'm writing a TFTP server program for university, which needs exclusive access to the files it opens for reading. Thus it can be configured that if a file is locked by another process that it waits for the file to become unlocked.
有没有在Win32没有办法等待解除锁定一个文件,而不先为它创建一个手柄?
Is there any way on Win32 to wait for a file become unlocked without creating a handle for it first?
我想问的原因是,如果另一个进程调用的之一,我甚至不会能够得到一个文件句柄使用使用上的锁等待的。
The reason I ask, is that if another process calls CreateFile() with a dwShareMode that is incompatible to the one my process uses, I won't even be able to get a file handle to use for waiting on the lock using LockFileEx().
感谢您的帮助提前!
推荐答案
如果你看看堆栈溢出的问题,和,你会发现链接至code,它可用于枚举进程和每个正在运行的进程的所有打开的句柄。这些信息可以用来获得 HANDLE
到具有打开该文件,以及其 HANDLE
的过程文件。然后,您可以使用创建文件 HANDLE
的副本,但在TFTP进程的句柄表。重复的 HANDLE
然后可以使用的TFTP进程的。
If you take a look at the Stack Overflow questions What Win32 API can be used to find the process that has a given file open? and SYSTEM_HANDLE_INFORMATION structure, you will find links to code that can be used to enumerate processes and all open handles of each running process. This information can be used to obtain a HANDLE
to the process that has the file open as well as its HANDLE
for the file. You would then use DuplicateHandle()
to create a copy of the file HANDLE
, but in the TFTP process' handle table. The duplicated HANDLE
could then be used by the TFTP process with LockFileEx()
.
此解决方案依赖于一个内部函数, NtQuerySystemInformation()
,以及可用于枚举打开的句柄无证系统信息类值。需要注意的是的这一特点NtQuerySystemInformation()
可以改变或在未来的Windows版本中不可用。您可能需要使用href=\"http://msdn.microsoft.com/en-us/library/windows/desktop/ms680657%28v=vs.85%29.aspx\" rel=\"nofollow\"> SEH的处理程序,以防止访问冲突是这种情况发生。
This solution relies on an internal function, NtQuerySystemInformation()
, and an undocumented system information class value that can be used to enumerate open handles. Note that this feature of NtQuerySystemInformation()
"may be altered or unavailable in future versions of Windows". You might want to use an SEH handler to guard against access violations were that to happen.
这篇关于等待文件被解锁 - 视窗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!