Monday, December 15, 2014

Your First C++ to List All Open Windows: Part I

To get a C++ compiler for Windows, see TDM-GCC at tdm-gcc.tdragon.net.

I googled "Get the Window Handle of an Externally Running Window" and went to stackoverflow.com to find the following piece of code:

#include "HelloWindows.h"
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <windows.h>
#include <iostream>
 
using namespace std;

BOOL CALLBACK FindWindowBySubstr(HWND hwnd, LPARAM substring)
{
    const DWORD TITLE_SIZE = 1024;
    TCHAR windowTitle[TITLE_SIZE];

    if (GetWindowText(hwnd, windowTitle, TITLE_SIZE))
    {
        _tprintf(TEXT("%s\n"), windowTitle);
        // Uncomment to print all windows being enumerated
        if (_tcsstr(windowTitle, LPCTSTR(substring)) != NULL)
        {
            // We found the window! Stop enumerating.
            return false;
        }
    }
    return true// Need to continue enumerating windows
}

int main()
{
 cout << "START" << endl;
    const TCHAR substring[] = TEXT("Substring");
    EnumWindows(FindWindowBySubstr, (LPARAM)substring);
}


I named the file HelloWindows.cpp and placed it at "C:\Users\Scott\CPrograms\HelloWindows.cpp".

I also created an empty file called HelloWindows.h in the same directory.

Then, I ran:



cd "C:\Users\Scott\CPrograms\"
"C:\MinGW64\bin\g++.exe" HelloWindows.cpp -o HelloWindows.exe"
HelloWindows.exe

This post was reposted from http://scottizu.wordpress.com/2013/08/20/your-first-c-to-list-all-open-windows-part-i/, originally written on August 20th, 2013.

No comments:

Post a Comment