aknouche
aknouche

Reputation: 1

Terminating ShellExecuteEx terminates "wrong" window

I use this code to start the webbrowser and then terminate it. However instead, after having started the webbrowser and making it the active window, it catches the window in the background (the application that starts the browser) and terminates it. So I want it to terminate the window in the forground (the webbrowser) instead.

SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = "C:\iexplore.exe";      
ShExecInfo.lpParameters = "http://www.google.se";   
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow =  SW_SHOWMAXIMIZED;
ShExecInfo.hInstApp = NULL; 
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess,10);
DWORD Pid = GetCurrentProcessId();
HANDLE h = OpenProcess(PROCESS_TERMINATE, false, Pid);
TerminateProcess(h, 1);
CloseHandle(h);

I guess the problem is that GetCurrentProcessId() gives me the id of the running application and not the newly opened webbrowser. Why is that?

Upvotes: 0

Views: 103

Answers (1)

David Heffernan
David Heffernan

Reputation: 613521

The entire purpose of GetCurrentProcessId is to return the PID of the process that calls the function.

To get the process handle of the created process read the hProcess member of the SHELLEXECUTEINFO struct. That said you would be better off calling CreateProcess in this instance since you already know the executable file that you wish to start.

Upvotes: 0

Related Questions