user1934608
user1934608

Reputation: 415

compiler error when calling GetWindowRect

When using GetWindowRect the same way in the MSDN example I get the following error, "cannot convert 'RECT' to 'tagRECT*' for argument '2' to 'BOOL GetWindowRect(HWND__, tagRECT)'" below is code that reproduces the error:

#include <windows.h>
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance,
                LPSTR lpszArgument, int nFunsterStil)
{
 HWND hwnd;               
 MSG messages;            
 WNDCLASSEX wincl;        

 wincl.hInstance = hThisInstance;
 wincl.lpszClassName = szClassName;
 wincl.lpfnWndProc = WindowProcedure;     
 wincl.style = CS_DBLCLKS;                 
 wincl.cbSize = sizeof (WNDCLASSEX);

 wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
 wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
 wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
 wincl.lpszMenuName = NULL;                 
 wincl.cbClsExtra = 0;                      
 wincl.cbWndExtra = 0;                       
 wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

 if (!RegisterClassEx (&wincl))
    return 0;

 hwnd = CreateWindowEx (0, szClassName, "Windows App", WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 544, 375, HWND_DESKTOP, NULL, 
        hThisInstance, NULL);

 RECT blah;
 GetWindowRect (hwnd, blah);

 ShowWindow (hwnd, nFunsterStil);

 while (GetMessage (&messages, NULL, 0, 0))
 {
     TranslateMessage(&messages);
     DispatchMessage(&messages);
 }

 return messages.wParam;
 }


 LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam,
 LPARAM, lParam)
 {
 switch (message)                  
 {
     case WM_DESTROY:
         PostQuitMessage (0);       
         break;
     default:                      
         return DefWindowProc (hwnd, message, wParam, lParam);
 }
 return 0;
}

Upvotes: 1

Views: 594

Answers (1)

Qaz
Qaz

Reputation: 61910

You need to pass the address of your RECT so that GetWindowRect can change it. The function takes a pointer to a RECT (RECT *), but you're passing a RECT.

Fix it like this:

RECT blah;
GetWindowRect(hwnd, &blah);
                    ^

If the function took a RECT, any changes made to it would be discarded because it would be passed by value (that is, a copy of your object would be passed in). What happens with &blah is that the pointer, not the object, gets passed by value, but that value is still the same address of the object, so changing what's at that address persists. Thus, it's one notion of passing the object by reference, the other being with an actual reference. Since the Windows API was made primarily in C, however, it uses the more C-ish method of taking a pointer, rather than a reference, which is usually more preferable in C++.

Upvotes: 4

Related Questions