为什么我们不需要关闭 ShellExecute 返回的句柄?
成功后,ShellExecute 返回一个句柄。
我们是否需要关闭此句柄?如果需要,如何关闭?
根据我的 Microsoft 发布的示例,我们不需要关闭此句柄。但 ShellExecute 本身的文档对此主题保持沉默。您能否确认我们确实不需要关闭此句柄?
但是,句柄如何才能有效并且不需要关闭???以下哪些说法是正确的:
- 句柄无效,我们无法用它做任何事情;
- 句柄永远不会被释放,并且存在(微软赞助的)内存泄漏(直到调用程序结束);
- 该句柄会在某个时候被系统自动释放,并且之后不再重用(->另一种资源泄漏)。只有尝试使用它,我们才能知道它是否仍然指向某个东西。
- 还有什么?
On success, ShellExecute returns a handle.
Do we need to close this handle, and if so, how ?
According to examples published my Microsoft, we need not close this handle. But the doc of ShellExecute itself is mute on the subject. Can you confirm we indeed do not need to close this handle ?
But then, how can a handle be valid and in no need of being closed ??? Which of the following statements is/are true:
- the handle is invalid and we can't do anything with it;
- the handle is never freed and there is a (Microsoft-sponsored) memory leak (until the caller program ends);
- the handle is automatically freed by the system at some time and never reused afterwards (-> another kind of resource leak). Only on trying to use it can we know whether it still points to something.
- what else ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该实例是 win32 中的 16 位内容,这只是一个数字>成功时为 32,除了函数失败时作为错误代码外,不能用于任何其他用途。另一方面,如果将 SEE_MASK_NOCLOSEPROCESS 传递给 Ex 版本,则有一个需要关闭的句柄。
That hinstance is a 16 bit thing, in win32, it is just a number > 32 on success and can't be used for anything other than as an error code when the function fails. On the other hand, if you pass SEE_MASK_NOCLOSEPROCESS to the Ex version, you have a handle you need to close.
摘自:http://msdn.microsoft.com /en-us/library/bb762153%28VS.85%29.aspx
Taken from: http://msdn.microsoft.com/en-us/library/bb762153%28VS.85%29.aspx
我稍微清楚了什么是
HINSTANCE
和HMODULE
。这不是一个HANDLE
,而是一个内存地址(指针)。如果您只需将hInstance
转换为(IMAGE_DOS_HEADER *)
并查看加载的模块内部,您就可以理解这一点。您可以使用 VirtualQueryEx (GetCurrentProcess(),...) 从内存地址接收更多信息(例如大小)。查看 http://blogs.msdn.com/oldnewthing/ archive/2004/10/25/247180.aspx 和 http://www.apriorit.com/our-experience/articles/9-sd-articles/74-hmodule-hinstance-handle-from- static-library-in-c,您将看到如何从内存地址 (__ImageBase) 接收
HINSTANCE
。因此,如果您
LoadLibrary
,您会收到一个HMODULE
(它与HINSTANCE
相同)。您应该使用 FreeLibrary 不是为了“关闭句柄”,而是为了从内存中卸载模块。例如,如果您使用GetModuleHandle
,您也会收到相同的地址(您收到的地址被转换为HMODULE
),但您不应该调用FreeLibrary
来“关闭手柄”。如果您了解什么是
HINSTANCE
和HMODULE
以及如何使用它们,您将知道如何使用从ShellExecute 返回的
。HINSTANCE
I clear a little what is
HINSTANCE
andHMODULE
. This are not aHANDLE
, but much more as a memory address (pointer). You can understand this if you just cast ahInstance
to(IMAGE_DOS_HEADER *)
and look inside of the loaded module. You can useVirtualQueryEx (GetCurrentProcess(),...)
to receive more information (a size for example) from a memory address.Look at http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx and http://www.apriorit.com/our-experience/articles/9-sd-articles/74-hmodule-hinstance-handle-from-static-library-in-c and you will be see how you can receive a
HINSTANCE
from a memory address (__ImageBase).So if you
LoadLibrary
for example you receive aHMODULE
(it's the same asHINSTANCE
). You should useFreeLibrary
not to "close handle", but to unload module from memory. If you useGetModuleHandle
for example, you receive also the same address (you receive address casted asHMODULE
), but you should NOT callFreeLibrary
to "close the handle".If you understand what is
HINSTANCE
andHMODULE
and how they should be used, you will be know how to useHINSTANCE
returned fromShellExecute
.