how come we need not close the handle returned by 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. 开发者_JS百科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 ?
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.
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.
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
.
精彩评论