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
Solved

EnumWindowsProc WinApi

Posted on 2009-05-12
7
477 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

829 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