Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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 ?
like image 506
user192472 Avatar asked May 06 '10 10:05

user192472


2 Answers

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.

like image 133
Anders Avatar answered Sep 24 '22 22:09

Anders


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.

like image 23
Jens Granlund Avatar answered Sep 22 '22 22:09

Jens Granlund