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

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds