有没有办法检查(文件)句柄是否有效?
有什么方法可以检查句柄(在我的例子中由 CreateFile 返回)是否有效?
我面临的问题是 CreateFile 返回的有效文件句柄(不是 INVALID_HANDLE_VALUE)稍后会导致 WriteFile 失败,并且 GetLastError 声称这是因为句柄无效。
Is there any way to check if a handle, in my case returned by CreateFile, is valid?
The problem I face is that a valid file handle returned by CreateFile (it is not INVALID_HANDLE_VALUE) later causes WriteFile to fail, and GetLastError claims that it is because of an invalid handle.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
由于您似乎在关闭后没有将句柄值设置为 INVALID_HANDLE_VALUE,所以我要做的是在
HANDLE
变量上设置一个读取观察点,这将导致调试器在访问HANDLE
值的每一行处中断。您将能够看到访问变量的顺序,包括何时读取变量以将其传递给CloseHandle
。请参阅:添加观察点(破坏当变量发生变化时)
Since it seems that you are not setting the handle value to
INVALID_HANDLE_VALUE
after closing it, what I would do is set a read watchpoint on theHANDLE
variable, which will cause the debugger to break at each line that accesses the value of theHANDLE
. You will be able to see the order in which the variable is accessed, including when the variable is read in order to pass it toCloseHandle
.See: Adding a watchpoint (breaking when a variable changes)
您的问题很可能是由以下两种情况之一引起的:
通常,分配
INVALID_HANDLE_VALUE
是一个很好的做法到每个句柄,只要它不应该包含任何有效的句柄值。简而言之 - 当您的变量被声明时 - 立即将其初始化为此值。并且在关闭文件句柄后立即将此值写入变量。
这将为您提供 (1) 的指示 - 尝试使用已关闭(或尚未打开)的文件句柄
Your problem is caused most probably by either of two things:
Generally it's a good practice to assign
INVALID_HANDLE_VALUE
to every handle as long as it's not supposed to contain any valid handle value.In simple words - when your variable is declared - immediately initialize it to this value. And also write this value into your variable immediately after you close the file handle.
This will give you an indication of (1) - attempt to use the file handle which is already closed (or hasn't been opened yet)
其他答案对于您的特定问题都很重要。
然而,如果你得到一个
HANDLE
并且只是想知道它是否确实是一个打开的文件句柄(而不是例如互斥体或GDI对象等的句柄),那么是 Windows API 函数 GetFileInformationByHandle< /a> 为此。根据您的句柄授予您的文件权限,您还可以尝试使用 ReadFile 或使用 WriteFile ,其中
nNumberOfBytesToWrite
设置为 0。The other answers are all important for your particular problem.
However, if you are given a
HANDLE
and simply want to find out whether it is indeed an open file handle (as opposed to, e.g., a handle to a mutex or a GDI object etc.), there is the Windows API function GetFileInformationByHandle for that.Depending on the permissions your handle grants you for the file, you can also try to read some data from it using ReadFile or perform a null write operation using WriteFile with
nNumberOfBytesToWrite
set to 0.打开文件作为数据结构保存在内核中,我认为没有官方方法来检测文件句柄是否有效,只需使用它并检查错误代码为 INVALID_HANDLE。您确定没有其他线程关闭该文件句柄吗?
Open-File are kept as a Data Structure in kernel, I don't think that has a official way to detect a file-handle is valid, just use it and check Error code as INVALID_HANDLE. Are you sure no others thread closed that file-handle?
检查手柄的有效性充其量只是一个创可贴。
您应该调试该过程 - 在设置句柄(文件打开)时设置一个断点,当您点击该代码并设置句柄后,设置第二个 条件断点在句柄值更改时触发。
这应该使您能够找出根本原因,而不是仅仅检查每次访问时句柄是否有效,这是不可靠的、成本高昂的,并且在给出正确逻辑的情况下是不必要的。
Checking the validity of the handle is a band-aid, at best.
You should debug the process - set a breakpoint at the point the handle is set up (file open) and when you hit that code and after the handle is set up, set a second conditional breakpoint to trigger when the handle value changes.
This should enable you to work out the underlying cause rather than just check the handle is valid on each access, which is unreliable, costly and not necessary given correct logic.
只是为了补充其他人所说的内容,请确保在调用
CreateFile
时检查返回值。 IIRC,失败时它将返回INVALID_HANDLE_VALUE
,此时您应该调用GetLastError
找出原因。Just to add to what everyone else is saying, make sure that you check the return value when you call
CreateFile
. IIRC, it will returnINVALID_HANDLE_VALUE
on failure, at which point you should callGetLastError
to find out why.