简体   繁体   中英

C++ Window Scanner

Need help with the loop. My Idea was scann for windows and when a window found check its procces Id so if the window was already found dont try to find it again. A piece from my code is like that:

if (Selection == 1)
{
    cout << endl << "================================= Scan Result =================================" << endl;
    cout << endl << "Scan Log:                                                        Times Found: " << TimesFound << endl;
    cout << "   - Scanning windows..." << endl;

    while(PressedKey != 27)
    {
        if (kbhit())
        {
            PressedKey = getch();
        }

        HWND WindowFound = FindWindow(0, "Untitled - Notepad");

        if (WindowFound != 0) 
        {
                            // My Idea was compare the procces Id of the found window
                            // to learn the found window was already found
            DWORD ProcessId;
            GetWindowThreadProcessId(WindowFound, &ProcessId);

            if(ProcessId != OtherId)
            {
                TimesFound++;
                cout << "Window found ! Times found: " << TimesFound << endl;
            }
        }
    }
}

I hope you guys can help me. Cheers

Edit: New Code

BOOL CALLBACK EnumFunc(HWND hwnd, LPARAM lParam)
{
    wchar_t lpString[32];

    GetWindowText(hwnd, (LPSTR)lpString, _countof(lpString));

    if(wcscmp(lpString, L"Untitled - Notepad") == 0) 
    {
        *((int *)lParam)++;
    }

    return TRUE;
}

This part of *((int *)lParam)++; code not works. Error is: expression must be a modifiable lvalue @MikeKwan

Edit: Me with same question again @MikeKwan :)

    while(PressedKey != 27)
    {
        if (kbhit())
        {
            PressedKey = getch();
            printf("Times found: %d \n", TimesFound);
        }

        EnumWindows(EnumFunc, (LPARAM)&TimesFound);
    }

I use this code to detect window every time but It detects a window and it redetects the same window again and again so nothing is changed :/

In your case, FindWindow is not aware of your list of already seen windows so it will continue to return the same window (although this is implementation defined). Regardless, FindWindow does not have the capability to do what you are attempting (skip windows during its search).

If you want to enumerate all windows only once, you should use EnumWindows . I wrote some example C code for EnumWindows that does what I am guessing you want to achieve. You will need to convert it to C++ but your usage of C++ is not particularly of the language at the moment anyway.

BOOL CALLBACK EnumFunc(HWND hwnd, LPARAM lParam)
{
    /*
     * We don't care about getting the whole string,
     * just enough to do the comparison. GetWindowText
     * will truncate the string if we tell it to.
     */
    wchar_t lpString[32];

    GetWindowText(hwnd, lpString, _countof);

    if(wcscmp(lpString, L"Untitled - Notepad") == 0) {
        (*(int *)param)++;
    }

    return TRUE;
}

int main(void)
{
    int numFound = 0;

    EnumWindows(EnumFunc, (LPARAM)&numFound);
    return ERROR_SUCCESS;
}

(Just wrote this in the answer window so there might be small errors).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM