rsk82
rsk82

Reputation: 29437

I can't get hold of LPRECT structure data, what I'm doing wrong?

It should be working without issue:

#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 this:

error: request for member 'top' in 'lpRect', which is of non-class type 'LPRECT {aka tagRECT*}'

Upvotes: 5

Views: 8679

Answers (3)

Roman Ryltsov
Roman Ryltsov

Reputation: 69734

You want:

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

I would say you are a C# guy because you attempted to use the types and API call without understanding of pointers. LPRECT is a derived type which says it is a pointer to RECT. Having a variable of this type, you declared a pointer to nothing, to invalid memory address (the pointer variable is left uninitialized). Your code would expectedly crash the app.

You need to hold a RECT variable instead, and pass a pointer to it to have it initialized/filed by the API.

Upvotes: 1

PermanentGuest
PermanentGuest

Reputation: 5341

Your code is wrong. Windows expects a valid Rect here. LPRECT is just a pointer and you haven't initialized it. Please modify it like this.

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

Upvotes: 7

unwind
unwind

Reputation: 400139

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

So, anyway, you need to use an actual RECT to have somewhere to store the result:

RECT rect; /* An actual RECT, with space for holding a rectangle. */

/* The type of &rect is LPRECT. */
GetWindowRect(consoleWindow, &rect);

Upvotes: 5

Related Questions