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 impact of a data loss event can be significant. Real-time data is essential to remaining competitive. Many companies can no longer afford to rely on a truck arriving each day to take backup tapes offsite. For most companies, a cloud backup and recovery solution will eliminate, or significantly reduce, IT resources related to the mundane task of backup and allow your resources to be redeployed to more strategic projects. The cloud - can now be comfortable for you – with 100% recovery from anywhere all …

  • Download the Information Governance Survey Benchmark Report to gain insights that can help you further establish business value in your Records and Information Management (RIM) program and across your entire organization. Discover how your peers in the industry are dealing with this evolving information lifecycle management environment and uncover key insights such as: 87% of organizations surveyed have a RIM program in place 8% measure compliance 64% cannot get employees to "let go" of information for …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds