-1

I have used system() to launch IE in my C++ code on a button click. The IE gets launched but a cmd window gets opened too and it gets hanged. I close the cmd window and then it works smoothly. Why is there an initial hang when i first launch the IE?

halfer
  • 19,824
  • 17
  • 99
  • 186
Harshit Singhvi
  • 104
  • 1
  • 7
  • [CreateProcess function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx) or [How do I call ::CreateProcess in c++ to launch a Windows executable?](http://stackoverflow.com/questions/42531/how-do-i-call-createprocess-in-c-to-launch-a-windows-executable) – 001 Apr 08 '16 at 12:59
  • @JohnnyMopp I dint got the function . Can you please tell the function call of Create process for launching an IE with "C:\\ProgramFiles\\Internet Explorer\\iexplorer.exe www.google.com" as url – Harshit Singhvi Apr 08 '16 at 13:15

2 Answers2

0

The cause is that the system() opens a console and calls the command from there. The console only closes when the Internet Explorer closes.

Either use CreateProcess or you can work-around it with system() as well by using the START command:

system("start \"Internet Explorer\" \"C:\\Program Files\\Internet Explorer\\iexplore.exe\" www.google.com");
EmDroid
  • 5,918
  • 18
  • 18
  • thanks @axalis i used the above system command and it worked smoothly. Can u please tell me how can i close the existing IE with a specified url using system command – Harshit Singhvi Apr 11 '16 at 06:21
  • That's actually not that easy, you would need to use the CreateProcess method to retrieve the process handle to be able to close it. But this might still not work as wanted if somebody has other windows open (terminating the process will close all the IE windows). To only close the particular window you would need to use EnumWindows and check all the windows to find the one you want to close, then send a WM_CLOSE message to that window (note however if the user is using tabs that will still close all the tabs in the window - at the moment I don't know how to enumerate/close a particular tab) – EmDroid Apr 11 '16 at 08:28
0

This code will launch IE using CreateProcess.

However, you may want to use ShellExecute (ShellExecute(NULL, "open", "www.google.com", NULL, NULL, SW_SHOWDEFAULT);) which will use the users default browser.

const char *pathToExplorer = "C:\\Program Files\\Internet Explorer\\iexplore.exe";
const char *webPage = "www.google.com";

char szCmdLine[1024];
sprintf(szCmdLine, "\"%s\" \"%s\"", pathToExplorer, webPage);

STARTUPINFO si = {0};
PROCESS_INFORMATION lp;
si.cb = sizeof(STARTUPINFO);
::CreateProcess( NULL,
            szCmdLine,
            NULL,
            NULL,
            FALSE,
            CREATE_DEFAULT_ERROR_MODE | NORMAL_PRIORITY_CLASS,
            NULL,
            NULL,
            &si,
            &lp);
001
  • 13,291
  • 5
  • 35
  • 66