1
votes

Recently, I decided to improve the error processing in a little app I had made; to test my functions, I tried introducing a basic error by giving a false window name to FindWindow. The error was successfully caught, but my error function told me the last error, given by GetLastError, was 0.

I tried introducing errors in other parts of the program, all of which were caught, and gave some sort of actual error code. When a real window name was passed to FindWindow, the function worked flawlessly.

I created an isolated test to see if something else in my program was causing problems, which you can see here;

#include <iostream>

#include <windows.h>

using namespace std;

int main() {
    wstring wstr = L"asdasdasd";

    wcout << wstr << endl;

    HWND hwnd = FindWindow(NULL, wstr.c_str());

    if (hwnd == NULL) {
        cout << "Window not found" << endl;
    }

    cout << GetLastError();

    return 0;
}

Obviously, there is no window in existence called "asdasdasd". And the test confirms so, running the code gives:

asdasdasd           //wstring created correctly
Window not found    //Output of FindWindow() is NULL
0                   //GetLastError() is 0

(Comments added post-test)

The windows API documentation for FindWindowA reads:

If the function succeeds, the return value is a handle to the window that has the specified class name and window name.

If the function fails, the return value is NULL. To get extended error information, call GetLastError."

So shouldn't I be getting an error code back from GetLastError? Is there an edge case I'm hitting where this doesn't occur?

Any help would be appreciated.

1
Comments are not for extended discussion; this conversation has been moved to chat.Samuel Liew

1 Answers

1
votes

The consensus from the comments seems to be that FindWindow simply doesn't set an error if no window is found (Though other errors do, which I tested). Personally, this seems like pretty misleading documentation, especially as other function in the windows api will set an error code if they fail to do their job, even if no actual errors occur, but I digress.