Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FindWindow does not find the a window

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.

like image 366
ginc0de Avatar asked May 13 '13 20:05

ginc0de


People also ask

How do I find the windows in VBA?

The FindWindow() function finds the first top-level window in the window list that satisfies the specified arguments. : Windows Resources « Windows API « VBA / Excel / Access / Word. The FindWindow() function finds the first top-level window in the window list that satisfies the specified arguments.

How does FindWindow work?

FindWindow works strictly by checking the windows in your declarations; it does not use the Agent to check the windows on the display. To determine which windows are actually open, use the Exist method. If specified, sTag must be a fully qualified tag.

What is FindWindow?

The FindWindow function retrieves the handle to the top-level window whose class name and window name match the specified strings. This function does not search child windows.

What is Hwnd C++?

A Windows window is identified by a "window handle" ( HWND ) and is created after the CWnd object is created by a call to the Create member function of class CWnd . The window may be destroyed either by a program call or by a user's action.


3 Answers

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;
}
like image 68
typ1232 Avatar answered Oct 29 '22 22:10

typ1232


 HWND Find = ::FindWindowEx(0, 0, "MozillaUIWindowClass", 0);
like image 24
David Brabant Avatar answered Oct 29 '22 21:10

David Brabant


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.
}
like image 22
123iamking Avatar answered Oct 29 '22 22:10

123iamking