2

My application has to do some stuff, but first it must know that all web browsers on the computer are shut down (not running).

How can I determine if they are currently running or not? I guess there is no common way for all of them, but the solution for all versions of that web browsers will be just fine:

  1. Internet Explorer
  2. Mozilla Firefox
  3. Google Chrome

Of course, after that check, I can show MessageBox "Please close all web browsers before continue", I don't have to close them by program (and I don't want to).

I prefer the solution that doesn't use any extra libraries etc., just a basic WinAPI/C++ if it's possible.

PolGraphic
  • 3,233
  • 11
  • 51
  • 108
  • 2
    http://stackoverflow.com/questions/13179410/check-whether-one-specific-process-is-running-on-windows-with-c check the 1st answer – Navin Nov 22 '13 at 09:20

1 Answers1

5

It seems like the obvious possibility would be to use FindWindow to find whether there's an open (main) window for any of them. For Chrome it looks like you want a class of "Chrome_WidgetWin_1" and for FireFox a class of "MozillaWindowClass" and for IE "IEFrame".

#include <iostream>
#include <windows.h>
#include <vector>
#include <string>

int main(){
    std::vector<std::string> names{
        "MozillaWindowClass", 
        "IEFrame",  
        "Chrome_WidgetWin_1" 
    };

    for (auto const &name : names) {
        if (NULL != FindWindow(name.c_str(), NULL)) {
            std::cout << "Please close all browsers to continue\n";
            break;
        }
    }
}

This should be somewhat more dependable than searching for the name of the executable--it's much easier for a user to rename an executable than change the class of main window it creates.

In theory, this (and probably almost anything similar) is open to a race condition: if the user starts up a browser immediately after you've checked for that browser, this could think there was no browser running when there actually was.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Won't work for IE. "Internet Explorer_Server" is not a top level window. For IE9, it's "IEFrame". – manuell Nov 22 '13 at 10:12
  • @manuell: Oops--corrected (and the same seems to work for IE 10 as well). – Jerry Coffin Nov 22 '13 at 10:15
  • The "not obvious" behaviour is that Chrome_WidgetWin_1 will be find if we run Opera, and MozillaWindowClass for Thunderbird. Except for that, IE/FF/Chrome checks indeed works. P.s. that strange initialisation of vector and for statement "(auto name : names)" are C++ code? – PolGraphic Nov 22 '13 at 10:29
  • @PolGraphic: Yes (both are new with C++11). – Jerry Coffin Nov 22 '13 at 10:47