Solved

EnumWindowsProc WinApi

Posted on 2009-05-12
7
473 Views
Last Modified: 2013-12-03
I'm trying to capture the title of all active windows. I call EnumWindows which calls EnumWindowsProc. GetWindowText only returns null strings however. It only needs to get the first 5 characters. Any suggestions?

bool EnumWindowsProc(void* hWnd, long lParam)
{
   char* win = "Title";
   GetWindowTextA(hWnd, win, 6);
    Write(win);
    return true;
}
0
Comment
Question by:secondeff
  • 3
  • 3
7 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 24365587
>> char* win = "Title";
This has no intrinsic storage, you are just defining a pointer. You're lucky it doesn't just crash :)

Try this

char win[6] = {0};
GetWindowTextA(hWnd, win, 6);
0
 
LVL 1

Author Comment

by:secondeff
ID: 24365923
I'm still getting all nulls. I looked over the assembly and it seems that my compiler treats both statements -  win = "Title" and win[6] = {0} similarly. It writes 7 bytes in the heap memory and passing that starting address to the GetWindowText function. My program crashed before when I just define char* win with no storage as you said.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24366041
The reason it didn't fail is because  you declared the type as char * pointing to a literal; however, this is deprecated in C++ since literals can be stored in read-only memory. When defining a pointer to a literal it should be const

char * win = "Test"; // This is deprecated in C++ since "Test" is actually a constant literal
char const * win = "Test";

You're probably better off using GetWIndowTextLength to find out the length of the string, allocate some heap to read into and then use strncmp() to compare the string with what you're looking for (don't forget to delete/free the heap you allocate -- NB, use new with auto_ptr and you won't need to worry about deleting).
http://msdn.microsoft.com/en-us/library/ms633521(VS.85).aspx
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Author Comment

by:secondeff
ID: 24366225
I just checked all of the lengths using GetWindowTextLength and each one (over 200) returned a zero. I know this isn't the case because I have six windows active all with different titles. I can't seem to figure this out.
0
 
LVL 40

Accepted Solution

by:
evilrix earned 250 total points
ID: 24366271
>> I just checked all of the lengths using GetWindowTextLength and each one (over 200) returned a zero. I know this isn't the case because I have six windows active all with different titles. I can't seem to figure this out.

Hmmmm... have you tried using the wide version? It really shouldn't make a difference though.

I only recently wrote some code to do this at work, without any such issues. I can't copy you the code, unfortunately, because it's copyrighted but it's not different from what you are doing here.

Can you post the exact code you're written just in case it's something obvious being overlooked?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
ID: 24366389
You need to keep in mind that a lot of windows simply do not have a title - they might not even be visible (check that with Spy++, it'll show you a lot more windows than you  can see). Also try the following, it works just as expected:
#include <windows.h>
#include <iostream>
using namespace std;
 
#pragma comment(lib,"user32.lib")
 
unsigned int unCount = 0;
 
BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
      char buf [7] = {0}; // 7 elements to include the NULL treminator!
      GetWindowText(hwnd,buf,6);
 
      cout << unCount++ << ": " << buf << endl;
 
      return true;
}
 
int main()
{
   EnumWindows(EnumWindowsProc,NULL);
 
   return 0;
}

Open in new window

0
 
LVL 1

Author Comment

by:secondeff
ID: 24371548
I found the problem. I didn't include the __stdcall in my EnumWindowsProc function. As soon as I added this everything worked fine. All that work for one line...
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question