11

I have a plan for make a simple trainer console with C++ but first step I've got problem with FindWindow()

#include <stdio.h>
#include <cstdlib>
#include <windows.h>
#include <winuser.h>
#include <conio.h>

LPCTSTR WindowName = "Mozilla Firefox";
HWND Find = FindWindow(NULL,WindowName);
int main(){
    if(Find)
    {
        printf("FOUND\n");
        getch();
    }
    else{
        printf("NOT FOUND");
        getch();
    }
}

The above code I use to try whether the command FindWindow() but when I execute the output always show

NOT FOUND

I've replaced Character Set on property Project from

Use Unicode Character Set

to

Use Multi-Byte Character Set

and

LPCTSTR

to

LPCSTR

or

LPCWSTR

but the result always the same, I hope anyone can help me.

peterh
  • 11,875
  • 18
  • 85
  • 108
ginc0de
  • 151
  • 1
  • 2
  • 12
  • 4
    Are you sure the window title is actually `Mozilla Firefox`? Isn't it the page title? Also that should probably be `_T("Mozilla Firefox")` – mwerschy May 13 '13 at 20:51
  • 1
    Use Spy++ or something and make sure it's really got that title. – chris May 13 '13 at 20:52
  • 2
    Obvious question, but is there *actually* a window with that *exact* title? I launched Firefox, used Spy++ and didn't see such a window. Besides, searching for windows by title seems, at best, fragile. – Nik Bougalis May 13 '13 at 20:53
  • I don't know exactly the title window but when cursor hover that show Mozilla Firefox – ginc0de May 14 '13 at 10:45

4 Answers4

17

FindWindow only finds the window if it has the exact specified title, not just a substring.

Alternatively you can:


search for the window class name:

HWND hWnd = FindWindow("MozillaWindowClass", 0);

enumerate all windows and perform custom pattern searches on the titles:

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
    char buffer[128];
    int written = GetWindowTextA(hwnd, buffer, 128);
    if (written && strstr(buffer,"Mozilla Firefox") != NULL) {
        *(HWND*)lParam = hwnd;
        return FALSE;
    }
    return TRUE;
}

HWND GetFirefoxHwnd()
{
    HWND hWnd = NULL;
    EnumWindows(EnumWindowsProc, &hWnd);
    return hWnd;
}
typ1232
  • 5,535
  • 6
  • 35
  • 51
9
 HWND Find = ::FindWindowEx(0, 0, "MozillaUIWindowClass", 0);
David Brabant
  • 41,623
  • 16
  • 83
  • 111
  • What is the difference FindWindow() with FindWindowEx()?? I'm new in C++ and this is assignment from campus – ginc0de May 14 '13 at 10:51
  • 2
    FindWindowEx also searches child windows, beginning with the parent window you optionally specified (first param). If this first param is null, the function uses the desktop window as the parent window. The function searches among windows that are child windows of the desktop. – David Brabant May 14 '13 at 11:21
6

According to MSDN

lpWindowName [in, optional]

Type: LPCTSTR

The window name (the window's title). If this parameter is NULL, all window names match.

Thus your WindowName can't be "Mozilla Firefox", because the Firefox window's title is never "Mozilla Firefox" but it could be "Mozilla Firefox Start Page - Mozilla Firefox" or something depends on the web page's name. Here is the example picture Firefox's real tiltle

Thus your code should be like this, (the code below only work - only work if you have the exact window's title name: "Mozilla Firefox Start Page - Mozilla Firefox" like the image above. I have tested on Windows 8.1 and it worked)

void CaptureWindow()
{


RECT rc;
HWND hwnd = ::FindWindow(0, _T("Mozilla Firefox Start Page - Mozilla Firefox"));//::FindWindow(0,_T("ScreenCapture (Running) - Microsoft Visual Studio"));//::FindWindow(0, _T("Calculator"));//= FindWindow("Notepad", NULL);    //You get the ideal?
if (hwnd == NULL)
{
    return;
}
GetClientRect(hwnd, &rc);

//create
HDC hdcScreen = GetDC(NULL);
HDC hdc = CreateCompatibleDC(hdcScreen);
HBITMAP hbmp = CreateCompatibleBitmap(hdcScreen,
    rc.right - rc.left, rc.bottom - rc.top);
SelectObject(hdc, hbmp);

//Print to memory hdc
PrintWindow(hwnd, hdc, PW_CLIENTONLY);

//copy to clipboard
OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_BITMAP, hbmp);
CloseClipboard();

//release
DeleteDC(hdc);
DeleteObject(hbmp);
ReleaseDC(NULL, hdcScreen);

//Play(TEXT("photoclick.wav"));//This is just a function to play a sound, you can write it yourself, but it doesn't matter in this example so I comment it out.
}
123iamking
  • 2,387
  • 4
  • 36
  • 56
2

You need to use the full name of the application (as seen in Windows Task Manager -> Application tab)

Example:

Google - Mozilla Firefox

(after opening a Google tab in Firefox)

Simon MᶜKenzie
  • 8,344
  • 13
  • 50
  • 77
tungnguyen
  • 29
  • 5