PolGraphic
PolGraphic

Reputation: 3364

How to check if any web browser is running, with C++ and WinAPI?

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.

Upvotes: 2

Views: 3496

Answers (1)

Jerry Coffin
Jerry Coffin

Reputation: 490338

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.

Upvotes: 5

Related Questions