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

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • Live Event Date: September 16, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you starting an on-premise-to-cloud data migration project? Have you thought about how much space you might need for your online platform or how to handle data that might be related to users who no longer exist? If these questions or any other concerns have been plaguing you about your migration project, check out this eSeminar. Join our speakers Betsy Bilhorn, VP, Product Management at Scribe, Mike Virnig, PowerSucess Manager and Michele …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds