Getting the Module (exe) Filename from an HWND

Environment: Windows 2000, Visual C++ 6

After searching for an article on how to find an executable name from an HWND and coming up dry, I decided to write one. First off I would like to say thanks to Jeff Kay for finding the PSAPI stuff on MSDN. :) Also, I would like to say that this code will only work with Windows NT/2000. I will be writing the Windows 98 version later on this week. Thanks for understanding.

In order to compile this project, you must link in PSAPI.lib which comes on the MSDN CDs (it also is included in the project zip file, not the source zip file). If you use this in any projects, PSAPI.dll will need to be supplied with the project since it is not packaged with the OS yet.

I am relatively new to writing for CodeGuru so here goes... On to the good stuff...

This class will basically use the EnumWindows function to get a handle to all the running windows. This function will be called from the Process() function. The Process function takes an integer called filter. This paramter can be ORed together to create a filter list of windows you do not want to include.


#define FILTER_VISIBLEONLY    1 // not compatible with NONVISIBLEONLY
#define FILTER_NONVISIBLEONLY 2 // not compatible with VISIBLEONLY
#define FILTER_PARENTONLY     4 // not compatible with CHILDONLY
#define FILTER_CHILDONLY      8 // not compatible with PARENTONLY
#define FILTER_APPS          16 // filter out programs by class name on 
                                // the exclude list, use AddExclusion

The FILTER_VISIBLEONLY will filter out any invisible windows. The FILTER_NONVISIBLEONLY will of course filter out any visible windows. The FITLER_PARENTONLY will filter all child windows and get only the parent windows. The FILTER_CHILDONLY will filter out anything that is not a child window. The FILTER_APPS will filter any applications based on their classname. You can add apps to this exclusion list with the function AddExclusion().

The FILTER_VISIBLEONLY is not compatible with FILTER_NONVISIBLEONLY. As well the FILTER_PARENTONLY is not compatible with the FILTER_CHILDONLY.

Usage

CEnumWinModules em;
em.AddExclusion("AFX:400000:8:10011:0:2ed014d"); // msdev's classname

// this will filter all invisible non-parent and the msdev program.
em.Process(FILTER_VISIBLEONLY | FILTER_APPS | FILTER_PARENTONLY);

for (int i=0;i<em.GetWindowCount();i++)
{
 TRACE3("Title: %s\tClass: %s\tExe: %s\n", 
  em.GetWindowTitle(i), 
  em.GetClassName(i), 
  em.GetModuleName(i));
}

I hope you guys found this article useful. This is my first attempt at writing for CodeGuru and I apologize if I forgot to do something or this topic was already covered. :)

Comments are definitely welcome.

Downloads

Download demo project - 15 Kb
Download source - 5 Kb


Comments

  • Its good but not able to compile

    Posted by Legacy on 04/04/2002 12:00am

    Originally posted by: hemkumar

    Could not fine psapi.h and psapi.lib and also psapi.dll in the project.zip

    Reply
  • GetWindowModuleFileName()

    Posted by Legacy on 08/13/2001 12:00am

    Originally posted by: gura

    check GetWindowModuleFileName() in platform-SDK

    Reply
  • Good Work Mike!

    Posted by Legacy on 04/26/2001 12:00am

    Originally posted by: Paul Bright

    Hi,

    Good work mike. I found this really handy!

    Keep it up

    Paul Bright

    Reply
  • ooohhhh, WOW

    Posted by Legacy on 04/09/2001 12:00am

    Originally posted by: bob

    thank you so much for submitting code that has already been tackled in MSDN................

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Best-in-Class organizations execute on a strategy that supports the multi-channel nature of customer requests. These leading organizations do not just open up their service infrastructures to accommodate new channels, but also empower their teams to deliver an effective and consistent experience regardless of the channel selected by the customer. This document will highlight the key business capabilities that support a Best-in-Class customer engagement strategy.

  • Live Event Date: September 19, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT In response to the rising number of data breaches and the regulatory and legal impact that can occur as a result of these incidents, leading analysts at Forrester Research have developed five important design principles that will help security professionals reduce their attack surface and mitigate vulnerabilities. Check out this upcoming eSeminar and join Chris Sherman of Forrester Research to learn how to deal with the influx of new device …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds