Solved

EnumWindowsProc WinApi

Posted on 2009-05-12
7
469 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ Language error 28 189
Clean Uninstall of VS Professional 2015 5 51
Problem to packaging 1 81
Issues with C++ Class 19 80
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now