为什么我们不需要关闭 ShellExecute 返回的句柄?

发布于 2024-08-31 11:48:56 字数 367 浏览 6 评论 0原文

成功后,ShellExecute 返回一个句柄。

我们是否需要关闭此句柄?如果需要,如何关闭?

根据我的 Microsoft 发布的示例,我们不需要关闭此句柄。但 ShellExecute 本身的文档对此主题保持沉默。您能否确认我们确实不需要关闭此句柄?

但是,句柄如何才能有效并且不需要关闭???以下哪些说法是正确的:

  1. 句柄无效,我们无法用它做任何事情;
  2. 句柄永远不会被释放,并且存在(微软赞助的)内存泄漏(直到调用程序结束);
  3. 该句柄会在某个时候被系统自动释放,并且之后不再重用(->另一种资源泄漏)。只有尝试使用它,我们才能知道它是否仍然指向某个东西。
  4. 还有什么?

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:

  1. the handle is invalid and we can't do anything with it;
  2. the handle is never freed and there is a (Microsoft-sponsored) memory leak (until the caller program ends);
  3. 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.
  4. what else ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清欢 2024-09-07 11:48:56

该实例是 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.

撩人痒 2024-09-07 11:48:56

摘自:http://msdn.microsoft.com /en-us/library/bb762153%28VS.85%29.aspx

如果函数成功,则返回一个
值大于 32。如果函数
失败,它返回一个错误值
指示失败的原因。
返回值被转换为
HINSTANCE 用于向后兼容
与 16 位 Windows 应用程序。
然而,这并不是真正的 HINSTANCE。它
只能转换为 int 且
与 32 或以下相比
错误代码如下。


Taken from: http://msdn.microsoft.com/en-us/library/bb762153%28VS.85%29.aspx

If the function succeeds, it returns a
value greater than 32. If the function
fails, it returns an error value that
indicates the cause of the failure.
The return value is cast as an
HINSTANCE for backward compatibility
with 16-bit Windows applications. It
is not a true HINSTANCE
, however. It
can be cast only to an int and
compared to either 32 or the following
error codes below.

給妳壹絲溫柔 2024-09-07 11:48:56

我稍微清楚了什么是HINSTANCEHMODULE。这不是一个HANDLE,而是一个内存地址(指针)。如果您只需将 hInstance 转换为 (IMAGE_DOS_HEADER *) 并查看加载的模块内部,您就可以理解这一点。您可以使用 VirtualQueryEx (GetCurrentProcess(),...) 从内存地址接收更多信息(例如大小)。

查看 http://blogs.msdn.com/oldnewthing/ archive/2004/10/25/247180.aspxhttp://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 来“关闭手柄”。

如果您了解什么是 HINSTANCEHMODULE 以及如何使用它们,您将知道如何使用从 ShellExecute 返回的 HINSTANCE

I clear a little what is HINSTANCE and HMODULE. This are not a HANDLE, but much more as a memory address (pointer). You can understand this if you just cast a hInstance to (IMAGE_DOS_HEADER *) and look inside of the loaded module. You can use VirtualQueryEx (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 a HMODULE (it's the same as HINSTANCE). You should use FreeLibrary not to "close handle", but to unload module from memory. If you use GetModuleHandle for example, you receive also the same address (you receive address casted as HMODULE), but you should NOT call FreeLibrary to "close the handle".

If you understand what is HINSTANCE and HMODULE and how they should be used, you will be know how to use HINSTANCE returned from ShellExecute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文