windows 有没有办法检查(文件)句柄是否有效?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4602629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:54:51  来源:igfitidea点击:

Is there a way to check if a (file) handle is valid?

cwindowswinapihandle

提问by Mathias

Is there any way to check if a handle, in my case returned by CreateFile, is valid?

有没有办法检查句柄(在我的情况下由 CreateFile 返回)是否有效?

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.

我面临的问题是 CreateFile 返回的有效文件句柄(它不是 INVALID_HANDLE_VALUE)后来导致 WriteFile 失败,GetLastError 声称这是因为句柄无效。

回答by Daniel Trebbien

Since it seems that you are not setting the handle value to INVALID_HANDLE_VALUEafter closing it, what I would do is set a read watchpoint on the HANDLEvariable, which will cause the debugger to break at each line that accesses the value of the HANDLE. 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 to CloseHandle.

由于您似乎没有INVALID_HANDLE_VALUE在关闭后设置句柄值,因此我要做的是在HANDLE变量上设置一个读取观察点,这将导致调试器在访问HANDLE. 您将能够看到访问变量的顺序,包括读取变量以将其传递给CloseHandle.

See: Adding a watchpoint (breaking when a variable changes)

请参阅:添加观察点(在变量更改时中断)

回答by Felix Dombek

The other answers are all important for your particular problem.

其他答案对于您的特定问题都很重要。

However, if you are given a HANDLEand 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 GetFileInformationByHandlefor that.

但是,如果给你一个HANDLE,只是想找出它是否确实是一个打开的文件句柄(而不是,例如,一个手柄,一个互斥体或GDI对象等),还有就是Windows API函数GetFileInformationByHandle对于.

Depending on the permissions your handle grants you for the file, you can also try to read some data from it using ReadFileor perform a null write operation using WriteFilewith nNumberOfBytesToWriteset to 0.

根据你的手柄授予您该文件的权限,你也可以尝试使用从中读取一些数据的ReadFile或执行使用空写操作的WriteFilenNumberOfBytesToWrite设置为0。

回答by valdo

Your problem is caused most probably by either of two things:

您的问题很可能是由以下两种情况之一引起的:

  • You may close the file handle, nevertheless you still try to use it
  • File handle is overwritten due to a memory corruption
  • 您可以关闭文件句柄,但您仍然尝试使用它
  • 由于内存损坏,文件句柄被覆盖

Generally it's a good practice to assign INVALID_HANDLE_VALUEto 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.

一般来说INVALID_HANDLE_VALUE,只要不应该包含任何有效的句柄值,分配给每个句柄是一个很好的做法。简单来说——当你的变量被声明时——立即将它初始化为这个值。并在关闭文件句柄后立即将此值写入您的变量中。

This will give you an indication of (1) - attempt to use the file handle which is already closed (or hasn't been opened yet)

这将为您提供 (1) 的指示 - 尝试使用已关闭(或尚未打开)的文件句柄

回答by Steve Townsend

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 conditionalbreakpointto 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.

这应该使您能够找出根本原因,而不仅仅是检查每次访问时的句柄是否有效,这是不可靠的、成本高昂的,并且在正确的逻辑下是不必要的。

回答by Mike Caron

Just to add to what everyone else is saying, make sure that you check the return value when you call CreateFile. IIRC, it will return INVALID_HANDLE_VALUEon failure, at which point you should call GetLastErrorto find out why.

只是为了补充其他人所说的话,请确保在调用时检查返回值CreateFile。IIRC,它会INVALID_HANDLE_VALUE在失败时返回,此时你应该打电话GetLastError找出原因。

回答by secmask

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?

打开文件在内核中作为数据结构保留,我认为没有官方方法来检测文件句柄是否有效,只需使用它并检查错误代码为 INVALID_HANDLE。您确定没有其他线程关闭该文件句柄吗?