Why don't we need to close the handle returned by ShellExecute?

If successful, ShellExecute returns a handle.

Does this handle need to be closed, and if so, how?

According to the examples published by my Microsoft, we do not need to close this handle. But the ShellExecute document itself is not negotiable. Can you confirm that we really do not need to close this handle?

But then, how can the descriptor be valid and does not need to be closed ??? Which of the following statements is true:

  • the descriptor is invalid and we cannot do anything with it;
  • the descriptor is never freed and there is (Microsoft memory leak) a memory leak (until the caller’s program ends);
  • the handle is automatically freed by the system for some time and is never reused (-> another type of resource leak). Only when we try to use it can we know if it all points to something.
  • what else?
+4
source share
3 answers

This property is a 16 bit thing , in win32, it is just a number> 32 for success and cannot be used for anything but an error code when a function fails. On the other hand, if you pass SEE_MASK_NOCLOSEPROCESS to the Ex version, you have a handle that you need to close.

+4
source

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 displayed as HINSTANCE for backward compatibility with 16-bit Windows applications. This however is not true HINSTANCE . This can only be attributed to int and compared to 32 or the following error codes below.

+4
source

I am a little versed in HINSTANCE and HMODULE . It is not HANDLE , but much more like a memory address (pointer). You can understand this by simply entering HINSTANCE in (IMAGE_DOS_HEADER *) and looking inside the loaded module. You can use VirtualQueryEx (GetCurrentProcess(),...) to get additional information (e.g. size) from the memory address.

Take a 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 see how you can get HINSTANCE from the memory address (__ImageBase).

So, if you are LoadLibrary , for example, you get HMODULE (this is the same as HINSTANCE ). You should not use FreeLibrary to "close the handle", but to unload the module from memory. For example, if you use GetModuleHandle , you also get the same address (you get the address specified as HMODULE ), but you should not call FreeLibrary to "close the handle."

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

0
source

Source: https://habr.com/ru/post/1308984/


All Articles