getclassname and getwindow text help

closed account (zwA4jE8b)
I am trying to get the open window class names and caption enumerated.
Right now (line 27) is outputting numbers. How do I get a string (the class name)? I am also having trouble with getwindow text function. What datatype do I use to store the text? I am using LPWSTR but I keep getting an uninitialized error.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <Windows.h>
#include <iostream>
#include <vector>
#include <string>
using namespace std;

void boolproc(vector<HWND>&);

int main()
{
	wchar_t* _classbuf = new wchar_t[255];
	LPWSTR _capbuf;

	vector<HWND> _Vhwnd;
	vector<HWND>::iterator iterH;
	vector<LPWSTR> _Vclass;
	vector<LPWSTR>::iterator iterC;
	vector<LPWSTR> _Vcaption;
	vector<LPWSTR>::iterator iterS;

	boolproc(_Vhwnd);
	for (iterH = _Vhwnd.begin(); iterH != _Vhwnd.end(); ++iterH)
	{
		GetClassName(*iterH, _classbuf, 255);
		_Vclass.push_back(_classbuf);
		cout << *_classbuf << " ";
		//GetWindowText(*iterH, _capbuf, 255);
		//_Vcaption.push_back(_capbuf);
		//cout << _capbuf << endl;
		//::SendMessage(*iterV, WM_CLOSE, 0, 0);
	}
	cin.get();
	return 0;
}

void boolproc(vector<HWND>& _hV)
{

	HWND hwnd;
	_hV.push_back(GetDesktopWindow());
	if ((hwnd = FindWindowA("IEFrame", 0)) != NULL)
		_hV.push_back(hwnd);
	if ((hwnd = FindWindowA(NULL, "Untitled - Notepad")) != NULL)
		_hV.push_back(hwnd);
	if ((hwnd = FindWindowA(NULL, "Steam")) != NULL)
		_hV.push_back(hwnd);
	if ((hwnd = FindWindowA(NULL, "Windows Task Manager")) != NULL)
		_hV.push_back(hwnd);

}
Last edited on
Sorry to say, but there are so many wrong things here that I might miss one or two. :-(

First of all, it appears that you have no previous experience or knowledge on how to use the Windows SDK. You see, the Windows SDK can be used to produce both Unicode and ANSI builds. For some reason you decided to use the ANSI version of the function FindWindow (FindWindowA, yes, that A is for ANSI) but in main() you use the "generic" versions of GetClassName() and GetWindowText(), to name a couple.

So first thing's first: Learn to use the SDK. I googled the topic for 5 or so minutes and it seems that a direct page explaining the SDK's mechanics is not as readily available as I thought. I then recommend that you read a good book on the subject. See if you can get "Programming Windows", 5th edition by Charles Petzold.

To satisfy your curiosity: Most function names like FindWindow() are macro definitions. They look similar to this:

1
2
3
4
5
#ifdef UNICODE
#define FindWindow(className, windowName) FindWindowW(className, windowName)
#else
#define FindWindow(className, windowName) FindWindowA(className, windowName)
#endif 


Visual Studio automatically defines the UNICODE macro definition when you change your project settings to a Unicode build.

When compiling with the SDK, you must also enclose all string literals with the TEXT() macro. Also _T() works. This would be line #41 above with these two corrections:

if ((hwnd = FindWindow(TEXT("IEFrame"), 0)) != NULL)

Finally for now, you don't use directly LPWSTR or LPSTR, you use LPTSTR that "morphs" into one of the former ones depending on the Unicode setting.
closed account (zwA4jE8b)
Thank you, and you are correct, I do not know anything about windows programming. I thought it would be fun to do some experiments while I begin to learn it.
I found this to be a good place to start if for one reason or another you can't grab a book: http://msdn.microsoft.com/en-us/library/ff381398(VS.85).aspx
closed account (zwA4jE8b)
cool, thanks, i will check it out.
Topic archived. No new replies allowed.