Variable problem

Hi, for those of you who were reading the previous thread I made I eventually figured why I couldn't make use of EnumWindows and since fixed that, now however I now have a new problem.
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
BOOL CALLBACK appList3(HWND hwnd, LPARAM user) {
	int a = (int)user;
	wxString s1; wxString s2; wxString s3;
	s1.Printf(wxT("%i"), a);
	s2.Printf(wxT("exe"));
	s3.Printf(wxT("title"));
	WCHAR t1; LPTSTR t2 = NULL;
	LPDWORD pid = NULL;
	GetWindowThreadProcessId(hwnd, pid);
	s1.Printf(wxT("%i"), pid);
	s2 = getAppExe(pid); a++;
	int i1 = GetWindowTextLength(hwnd); //(hwnd, &t1, 50);
	int i2 = 0;
	if (i1 == 0) { i2 = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0); }
	if (i1 > 0) {
		GetWindowText(hwnd, &t1, i1);
		s3.Printf(wxT("%s"), t1);
	} else if (i2 > 0) {
		SendMessage(hwnd, WM_GETTEXTLENGTH, i2, (LPARAM)t2);
		s3.Printf(wxT("%s"), t2);
	}
		/*hexWin->gApp->AppendRows(1);
		hexWin->gApp->SetCellValue(appLen, 0, s1);
		hexWin->gApp->SetCellValue(appLen, 1, s2);
		//s.Printf(wxT("%i"), pe32.th32ProcessID);
		hexWin->gApp->SetRowLabelValue(appLen, s);*/
		//useCB = FALSE;
	frame->appLen = a;
	frame->addApp(s1, s2, s3);
	return TRUE;
}
1
2
3
4
5
6
7
8
#0 67B4A7CF	wxStringBase::AllocBeforeWrite() (E:\WIN32\Tools\CodeBlocks\wxWidgets2.8\lib\gcc_dll\wxmsw28u_gcc.dll:??)
#1 67B4A855	wxString::GetWriteBuf() (E:\WIN32\Tools\CodeBlocks\wxWidgets2.8\lib\gcc_dll\wxmsw28u_gcc.dll:??)
#2 67B4B460	wxString::PrintfV() (E:\WIN32\Tools\CodeBlocks\wxWidgets2.8\lib\gcc_dll\wxmsw28u_gcc.dll:??)
#3 67B4B4EF	wxString::Printf() (E:\WIN32\Tools\CodeBlocks\wxWidgets2.8\lib\gcc_dll\wxmsw28u_gcc.dll:??)
#4 00000000	0x00401ad3 in appList3() (??:??)
#5 7503946A	USER32!LockSetForegroundWindow() (C:\Windows\syswow64\user32.dll:??)
#6 00000000	0x000401b0 in ??() (??:??)
#7 00000000	0x00000000 in ??() (??:??) 
Try as I might I can't figure out what I'm doing wrong to get this callstack. I'll focus on window text later (assuming I'm not getting any text). I have to goto work now so it'll be about 5 to 8 hours before I check back.
Last edited on
I figured it out, turns out it had to do with the pointer I was giving to GetWindowText - &t1 - fixed it and now have a new problem I can't seem to get a hold of the EXE for the window, I'm going to continue trying to figure it out but would be helpful if someone could identify the reason for me.
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
wxString getAppExe(DWORD appID) {
	PROCESSENTRY32 pe = {0}; wxString s;
	pe.dwSize = sizeof(PROCESSENTRY32);
	Process32First(NULL, &pe);
	do {
		s.Printf(wxT("%s"), pe.szExeFile);
		if (pe.th32ProcessID == appID) { break; }
	} while(Process32Next(NULL, &pe));
	return s;
}
BOOL CALLBACK appList3(HWND hwnd, LPARAM null) {
	int a = AL;
	wxString s1; wxString s2; wxString s3;
	LPTSTR t = NULL; DWORD pid;
	GetWindowThreadProcessId(hwnd, &pid);
	s1.Printf(wxT("%i"), pid);
	s2 = getAppExe(pid); a++;
	int i1 = GetWindowTextLength(hwnd);
	int i2 = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0);
	if (i1 > 0 || i2 > 0) {
		if (GetWindowText(hwnd, t, i1) != TRUE) {
			//SendMessage(hwnd, WM_GETTEXT, i2, (LPARAM)t);
		} s3.Printf(wxT("%s"), t);
		if (s3 != wxT("")) {
			AL = a;
			frame->addApp(a, s1, s2, s3);
		}
	} return TRUE;
}
Edit: Corrected a couple of mistakes in appList3, SendMessage was giving me problems so I commented out for now.
Last edited on
You could use GetModuleFileNameEx() and OpenProcess() to get the executable name of another process, assuming you know the PID.
Thanks, I'll try after work.

Edit: Couldn't find any information on process handles so went back to original way, turns out not getting the next process when I call Process32Next, any ideas on why that would be? (Tried with snapshot handle to so that's not it)
Edit: never mind, it was the dwSize that was the cause, fixed and now have one more variable to acquire - which username is associated with the process
Edit: Finally achieved what I needed to which is get all open windows and their exe plus process id, here's my code for those who want to reference / learn from it. (Didn't need the username after all)
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
PROCESSENTRY32 getApp(DWORD appID) {
    PROCESSENTRY32 pe32;
    pe32.dwSize = sizeof(PROCESSENTRY32);
    HANDLE ah = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    Process32First(NULL, &pe32);
    do {
        if (pe32.th32ProcessID == appID) { break; }
    } while (Process32Next(ah, &pe32));
    CloseHandle(ah);
    return pe32;
}
wxString getAppExe(DWORD appID) {
	PROCESSENTRY32 pe32;
	wxString s;
	pe32.dwSize = sizeof(PROCESSENTRY32);
	pe32 = getApp(appID);
	s.Printf(wxT("%s"), pe32.szExeFile);
	return s;
}
BOOL CALLBACK appList3(HWND hwnd, LPARAM null) {
	if (IsWindowVisible(hwnd)) {
		int a = AL;
		wxString s1, s2, s3;
		TCHAR t[MAXCHAR], t2[MAXCHAR];
		DWORD pid;
		GetWindowThreadProcessId(hwnd, &pid);
		s1.Printf(wxT("%i"), pid);
		s2 = getAppExe(pid); a++;
		// This is in case have a problem with MAXBYTE as value
		// sizeof(t)/sizeof(TCHAR)
		SendMessage(hwnd, WM_GETTEXT, MAXBYTE, (LPARAM)(void*)t);
		s3.Printf(wxT("%s"), t);
		if (s3 != wxT("") && s3 != wxT("(null)")) {
			AL = a;
			frame->addApp(a, s1, s2, s3);
		}
	} return TRUE;
}
Edit: made improvements so updated the code here
Last edited on
Topic archived. No new replies allowed.