Krzysztof Kowalczyk Blog  •  Notes  •  My Software  •  My Documents

Windows programming tip: launching a browser

Imagine you want to launch a web browser from within your program and make it go to a specific site. You can do it the easy way:
void LaunchIE(char *site) {
  ShellExecute( GetDesktopWindow(), "", "iexplore", site, NULL, SW_SHOWNORMAL );
}
There's one problem with this code: it doesn't run on Windows 95 and 98 and those are still very popular. Fortunately, there is another way to do almost the same:
HANDLE JustCreateProcess(char *cmd, char *dir) {
  PROCESS_INFORMATION ProcInfo={0,};
  STARTUPINFO StartUp={0,};
  StartUp.cb=sizeof(StartUp);
  if (!CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, dir, ;&StartUp, &ProcInfo))
    return NULL;
  if (NULL != ProcInfo.hThread) CloseHandle( ProcInfo.hThread );
  return ProcInfo.hProcess;
}

void LaunchIE(char *site) {
  assert(site);
  std::string fullStr( "c:\Program Files\Internet Explorer\IEXPLORE.EXE" );
  fullStr.append( " " );
  fullStr.append( site );
  HANDLE hProc;
  hProc = JustCreateProcess( (char*) fullStr.c_str(), "c:\Program Files\Internet Explorer" );
  if (hProc) CloseHandle(hProc);
}
← newer • 637 of 680older →