0

I am trying to launch a browser window via CreateProcess.

Sometimes it is launched in the foreground. This is what I want.

Sometimes the new window is placed under all the other windows. This is not what I want.

I have followed the instructions at How to bring window on top of the process created through CreateProcess, it does not work; the results are still inconsistent.

Here is the code:

void launch(const string &url) {
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    memset(&si, 0, sizeof(si));
    memset(&pi, 0, sizeof(pi));
    
    si.cb = sizeof(si);

    string args = string(" ") + url;
    LPSTR args_win = const_cast<char*>(args.c_str());

    CreateProcess("C:/Program Files/Mozilla Firefox/firefox.exe", args_win, 0, 0, 0, 0, 0, 0, &si, &pi);
    
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
}
CaptainCodeman
  • 1,951
  • 2
  • 20
  • 33
  • 2
    You could pass [`-foreground`](https://wiki.mozilla.org/Firefox/CommandLineOptions#-foreground) to firefox and let it take care of that for you. –  Jun 02 '22 at 21:20
  • @Frank Thank you very much, that actually solves the problem for me, but I will leave the question open in case a general purpose solution also exists. – CaptainCodeman Jun 02 '22 at 21:23
  • 4
    @CapptainCodeman, Sure, but you should know that this could very well a Firefox-specific problem. Browsers are multi-process programs, and firefox.exe is probably not the process associated with the window, muddying the waters considerably. –  Jun 02 '22 at 21:26
  • @Frank That is a good point, however I will add that the previous version of the application was written in C# and used `System.Diagnostics.Process.Start` to launch Firefox and it always worked as expected for 2 years straight. It seems that my `CreateProcess` is doing something different. – CaptainCodeman Jun 02 '22 at 21:29
  • 1
    @CaptainCodeman Not really. Look at the [`Process.Start()` source code](https://referencesource.microsoft.com/#system/services/monitoring/system/diagnosticts/Process.cs,e9edeff01b1851af,references) for yourself. It is complex, but strip away the features you aren't using and it is very close to your code here. The differences are it: 1) calls `CreateProcess()` inside a lock, 2) doesn't use the `lpApplicationName` parameter at all, only the `lpCommandLine` parameter, 3) sets the `bInheritHandles` parameter to TRUE, and 4) sets the `lpCurrentDirectory` parameter to `Environment.CurrentDirectory` – Remy Lebeau Jun 02 '22 at 22:55
  • 1
    @CaptainCodeman none of that has anything to do with window z-ordering/focusing. – Remy Lebeau Jun 02 '22 at 22:58
  • 1
    This is not something you can control. It all revolves around foreground activation permission and [foreground activation permission is like love: You can’t steal it, it has to be given to you](https://devblogs.microsoft.com/oldnewthing/20090220-00/?p=19083). Consider a system with 3 processes: `M`ain, `C`hild, and `N`otepad. `M` launches `C`, and by default, the system moves `C` to the top of the Z order and makes it the foreground window. If in between launching `C` and showing its GUI the user switches to `N`, things start to fall apart. – IInspectable Jun 03 '22 at 07:23

0 Answers0