0

I need to find all the open Window with graphic interfaces and their processes and I don't really know how to do it. I've written some code but I just succeeded in finding open windows:

HWND hwnd = GetForegroundWindow(); // get handle of currently active window
GetWindowText(hwnd, wnd_title, sizeof(wnd_title));
cout << "Window with focus: " << wnd_title << endl << endl;

EnumWindows(EnumWindowsProc, 0); 

EnumWindowsProc is defined like this:

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
    char class_name[80];
    char title[80];

    if (IsWindowVisible(hwnd)) {
        GetClassName(hwnd, class_name, sizeof(class_name));
        GetWindowText(hwnd, title, sizeof(title));
        cout << "Window title: " << title << endl;
        cout << "Class name: " << class_name << endl << endl;
    }   
    return TRUE;
}

Someone can help me?

ica
  • 11
  • 2

1 Answers1

1

I would recommend you not to check if IsWindowVisible because of

If the specified window, its parent window, its parent's parent window, and so forth, have the WS_VISIBLE style, the return value is nonzero. Otherwise, the return value is zero.
Because the return value specifies whether the window has the WS_VISIBLE style, it may be nonzero even if the window is totally obscured by other windows.

While enumerating windows you can use DWORD WINAPI GetWindowThreadProcessId(_In_ HWND hWnd, _Out_opt_ LPDWORD lpdwProcessId); to retrieve process id correlated with that particular HWND.

example :

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
    char class_name[80];
    char title[80];
    DWORD dwProcessId;
    GetClassName(hwnd,class_name, sizeof(class_name));
    GetWindowText(hwnd,title,sizeof(title));

    // get process id based on hwnd
    GetWindowThreadProcessId(hwnd, &dwProcessId);

    std::cout << "Window title: "<< title << std::endl;
    std::cout << "Class name: "<< class_name << std::endl
    // display process id based on hwnd
    std::cout << "Process Id: " << dwProcessId  << std::endl;

    return TRUE;
}
mrogal.ski
  • 5,828
  • 1
  • 21
  • 30
  • And is it possible to find the application that corrispond to every window? – ica Sep 08 '17 at 12:59
  • @ica Yes, This should do exactly what you need but I'm not sure if it wont display duplicated process ids since one process can have multiple windows. – mrogal.ski Sep 08 '17 at 13:10
  • because i need also to find the icon associated to the open application and i don't know if i can find it from windows – ica Sep 08 '17 at 13:22
  • @ica Getting an icon is fairly easy, all you have to do is to call `GetClassLong(hwnd, GCL_HICON);` – mrogal.ski Sep 08 '17 at 13:23
  • that function returns a handle... i need to send a the icon to a client... is it possible with a handle? – ica Sep 08 '17 at 13:37
  • @ica Just cast this handle to `HICON` and use [this solution](https://stackoverflow.com/questions/7375003/how-to-convert-hicon-to-hbitmap-in-vc) – mrogal.ski Sep 08 '17 at 13:41
  • Thank you very much – ica Sep 08 '17 at 13:44