I can’t get the LPRECT structure data, what am I doing wrong?

It should work without problems:

#include <iostream> #define _WIN32_WINNT 0x501 #include <windows.h> using namespace std; int main() { HWND consoleWindow = GetConsoleWindow(); LPRECT lpRect; GetWindowRect(consoleWindow,lpRect); cout << lpRect.top <<endl; } 

but instead I get the following:

 error: request for member 'top' in 'lpRect', which is of non-class type 'LPRECT {aka tagRECT*}' 
+2
source share
3 answers

Your code is incorrect. Windows expects a valid Rect here. LPRECT is just a pointer, and you did not initialize it. Please change it like this.

 HWND consoleWindow = GetConsoleWindow(); RECT aRect; GetWindowRect(consoleWindow,&aRect); cout << aRect.top <<endl; 
+5
source

The LPRECT type is a pointer to a RECT . It is (unfortunately, in my opinion) common in the Win32 API that they play "hide an asterisk" on you. This creates additional confusion as the asterisk is important in C.

So, in any case, you need to use the actual RECT to store the result:

 RECT rect; /* An actual RECT, with space for holding a rectangle. */ /* The type of &rect is LPRECT. */ GetWindowRect(consoleWindow, &rect); 
+4
source

Do you want to:

 RECT Rect; GetWindowRect(consoleWindow, &Rect); cout << Rect.top <<endl; 

I would say that you are a C # guy because you tried to use types and API calls without understanding pointers. LPRECT is a derived type that says it is a pointer to a RECT . Having a variable of this type, you pointed to a pointer to nothing, to an invalid memory address (the pointer variable remains uninitialized). Your code is expected to crash the application.

Instead, you need to save the RECT variable and pass a pointer to it so that it is initialized / registered by the API.

+1
source

Source: https://habr.com/ru/post/1497605/


All Articles