Reznicencu Bogdan
Reznicencu Bogdan

Reputation: 900

C++ Get all running processes and their titles

I am currently working on a project in C++ and what i want to do is to take all of the running available proceses and with them their titles , but currently I'm able only to track their handlers and count them and no to take their title.

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam);
int i;
string hwndTitle;
LPTSTR WindowTitle;
int length = 0;
int getHWND()
{
    std::cout << "Finding all open windows\n";
    if(EnumWindows(EnumWindowsProc, 0)) {
        std::cout << i << " windows are open\n"<<hwndTitle<<"\n"<<"Call was successful...\n" << std::endl;
        std::cin.get();
    } else {
        std::cout << "Call was unsuccessful...\n" << std::endl;
        std::cin.get();
    }

    return 0;
} 

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) {
    i++;
    HWND WindowHandle;
    WindowHandle = GetForegroundWindow();
    length = GetWindowTextLength (hWnd);
    hwndTitle = GetWindowText(hWnd , WindowTitle , length); 
    return true;
}

Upvotes: 3

Views: 7111

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595339

Your code is misusing GetWindowText(). You are not allocating any memory for it to fill in, and it does not return a std::string or even a char*/TCHAR*, like your code is assuming. And even if you were using it correctly, you are outputting only the last window title that is assigned to hwndTitle instead of concatenating multiple titles together.

Try something more like this instead:

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam);

int getHWND()
{
    std::cout << "Finding all open windows" << std::endl;
    int i = 0;
    if (EnumWindows(EnumWindowsProc, (LPARAM) &i)) {
        std::cout << i << " windows are open\n" << "Call was successful...\n" << std::endl;
    } else {
        std::cout << "Call was unsuccessful...\n" << std::endl;
    }
    std::cin.get();
    return 0;
} 

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
    int *i = (int*) lParam;
    ++(*i);
    int length = GetWindowTextLengthA(hWnd);
    std::vector<char> WindowTitle(length+1);
    length = GetWindowTextA(hWnd, &WindowTitle[0], length); 
    if (length > 0) {
        WindowTitle[length] = 0;
        std::cout << &WindowTitle[0] << std::endl;
    } else {
        std::cout << "(none)" << std::endl;
    }
    return TRUE;
}

Alternatively:

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam);

int getHWND()
{
    std::cout << "Finding all open windows" << std::endl;
    std::vector<std::string> WindowTitles;
    if (EnumWindows(EnumWindowsProc, (LPARAM) &WindowTitles)) {
        for (std::vector<std::string>::iterator i = WindowTitles.begin(); i != WindowTitles.end(); ++i) {
            if (!i->empty()) {
                std::cout << *i << std::endl;
            } else {
                std::cout << "(none)" << std::endl;
            }
        }
        std::cout << WindowTitles.size() << " windows are open\n" << "Call was successful...\n" << std::endl;
    } else {
        std::cout << "Call was unsuccessful...\n" << std::endl;
    }
    std::cin.get();
    return 0;
} 

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
    std::vector<std::string> *WindowTitles = (std::vector<std::string>*) lParam;
    int length = GetWindowTextLengthA(hWnd);
    std::string WindowTitle(length+1, '\0');
    length = GetWindowTextA(hWnd, &WindowTitle[0], length); 
    WindowTitle.resize(length);
    WindowTitles->push_back(WindowTitle);
    return TRUE;
}

Upvotes: 4

Related Questions