Virus721
Virus721

Reputation: 8335

Replace call to AfxGetInstanceHandle() by Windows API function(s)

SHELLEXECUTEINFO info;

// Initializing struct
info.cbSize       = sizeof(info); 
info.fMask        = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI;
info.hwnd         = NULL;
info.lpVerb       = _T("open");
info.lpParameters = sParameters_p; 
info.lpDirectory  = _T("");
info.nShow        = SW_SHOW; 
info.hInstApp     = NULL;
info.lpFile       = sFileName_p;
// Problem here :
info.hInstApp     = AfxGetInstanceHandle(); 

ShellExecuteEx(&info);

Is there a function in the Windows API that i can use to get the HINSTANCE of the process so that i can avoid using AfxGetInstanceHandle();

Does GetModuleHandle(NULL); work ? It returns a HMODULE and not a HINSTANCE.

Upvotes: 1

Views: 1951

Answers (1)

Qaz
Qaz

Reputation: 61970

Yes, GetModuleHandle(NULL) works. Passing NULL causes it to return a handle to the calling process (which is your EXE file).

As for the HMODULE vs. HINSTANCE issue, HMODULE and HINSTANCE used to be different way back in 16-bit Windows, but they are now the same thing and can be used interchangeably.


But do note that you are not using this field correctly. You must initialize it with NULL.

Then after the process was started and ShellExecuteEx() returns, that field will contain the legacy Windows 3.x startup error code for the started process. The olden HINSTANCE value, but only if the process failed to start. And beware, the hProcess field will be set to the process handle for the process you started. You must call CloseHandle() on that handle when you're done using it. Failure to do so will cause a permanent handle leak.

Do make sure that this is what you intended to do. If you are not actually interested in obtaining the handle then omit the SEE_MASK_NOCLOSEPROCESS option.

Upvotes: 7

Related Questions