GetClientRect error and don't know why

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void app_size(int *pApp_win_tX, int *pApp_win_bX, int *pApp_win_tY, int *pApp_win_bY)
{
	#define Buffer 1024

	//DECLARE variables
	char WindowTitle [Buffer];
	RECT CurrentWindowSize;
	HWND CurrentConsoleHwnd;

	//gets the active window's hwnd
	if (!(CurrentConsoleHwnd = (HWND)GetConsoleTitle ( (LPWSTR)WindowTitle, Buffer)))
	{
		cout << "Error occured at get \"get console title\": " << GetLastError() << endl;
	}

	cout << "current console hwnd : " << CurrentConsoleHwnd << endl;

	if ( !(GetClientRect (CurrentConsoleHwnd, &CurrentWindowSize)) )
	{
		cout << "Error occured at get client window coordinates: " << GetLastError() << endl;
	}
	else
	{
		*pApp_win_tX = CurrentWindowSize.left;
		*pApp_win_bX = CurrentWindowSize.right;
		*pApp_win_tY = CurrentWindowSize.top;
		*pApp_win_bY = CurrentWindowSize.bottom;
	}

	return;
}


I keep on getting error when it reaches GetClientRect().

Any ideas why?

Thanks for your help
bad C casting is bad.

See GetConsoleTitle:

http://msdn.microsoft.com/en-us/library/ms683174(VS.85).aspx

GetConsoleTitle does not return an HWND. It returns a DWORD indiciating how many TCHARs were written to the passed array.

To get the HWND you can call GetConsoleWindow:

1
2
3
4
5
CurrentConsoleHwnd = GetConsoleWindow();
if(!CurrentConsoleHwnd)
{
  // error
}



------------------------
Also.. This bit is not all that important, but it's cuious nonetheless. This code won't cause any problems with your program but it's good to know this anyway:

1
2
3
4
char WindowTitle [Buffer];  // <-- type char* (LPSTR)

//..
if (!(CurrentConsoleHwnd = (HWND)GetConsoleTitle ( (LPWSTR)WindowTitle, Buffer))) // <-- casting to type LPWSTR 


I'd recommend doing one of the following:

1) use TCHAR instead of char for WindowTitle and don't cast WindowTitle at all (recommended)
or
2) cast WindowTitle to (LPSTR) instead of (LPWSTR)


I was going to write a big long post about what the differences are, but something like that is better suited for the Articles forum.
Hey thanks,

Just figured it out just before i came back to this page.

i m now using something similar to yours but its GetForegroundWindow()
also changed GetClientRect to GetWindowRect for my program's application.

so my code now looks like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void app_size(long *pApp_win_tX, long *pApp_win_bX, long *pApp_win_tY, long *pApp_win_bY)
{
	//DECLARE variables

	RECT CurrentWindowSize;
	HWND CurrentConsoleHwnd = GetForegroundWindow();

	if ( !(GetWindowRect (CurrentConsoleHwnd, &CurrentWindowSize)) )
	{
		cout << "Error occured at get client window coordinates: " << GetLastError() << endl;
	}
	else
	{
		*pApp_win_tX = CurrentWindowSize.left;
		*pApp_win_bX = CurrentWindowSize.right;
		*pApp_win_tY = CurrentWindowSize.top;
		*pApp_win_bY = CurrentWindowSize.bottom;

//check		printf ("screen top x coordinates: %d, screen bottom x coordinates: %d, screen top y coordinates: %d, screen bottom y coordinates: %d\n"
//						, *pApp_win_tX, *pApp_win_bX, *pApp_win_tY, *pApp_win_bY );
	}

	return;
}


Thanks for your detailed explaination!
Last edited on
GetForegroundWindow will only work if your program is in the foreground. You're better off with GetConsoleWindow.
i am trying to get the window position and size so i can do automatic scroll for my program.

its a final year project thing

thanks
Topic archived. No new replies allowed.