Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
");
        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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
183 views
Welcome To Ask or Share your Answers For Others

1 Answer

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...