Solved

EnumWindowsProc WinApi

Posted on 2009-05-12
7
462 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

757 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

24 Experts available now in Live!

Get 1:1 Help Now