Solved

EnumWindowsProc WinApi

Posted on 2009-05-12
7
486 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

687 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