Tyler Nichols
Tyler Nichols

Reputation: 196

Why does the Windows API respond with error codes depending on my logging statements

I have some code to grab a window handle and then get its rectangle.

HWND hWnd = FindWindow("CalcFrame", NULL);

LPRECT rect;
int retval = GetWindowRect(hWnd, rect);

if (retval == 0) {
    DWORD error = GetLastError();
    std::cout << error << "\n";
} else {
    std::cout << "FindWindow/GetWindowRect Success" << "\n";
}

This code works fine, and the values are stored in rect when I have no logging statements. When I add this logging statement directly after...

std::cout << rect->left << "," << rect->top << "," << rect->right << "," << rect->bottom << "\n";

I get an error (error code 1400) from the GetLastError() winapi method, showing that we could not find the window handle and get the windows rectangle.

When I use this logging statement, I get no error.

std::cout << "Right: " << rect->right << "\n";
std::cout << "Bottom: " << rect->bottom << "\n";

What could possibly be the reason for this?

Upvotes: 0

Views: 185

Answers (1)

Alan Birtles
Alan Birtles

Reputation: 36379

The correct code is:

RECT rect;
int retval = GetWindowRect(hWnd, &rect);

GetWindowRect expects a pointer to an existing structure.

Upvotes: 3

Related Questions