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

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

  • "Security" is the number one issue holding business leaders back from the cloud. But does the reality match the perception? Keeping data close to home, on premises, makes business and IT leaders feel inherently more secure. But the truth is, cloud solutions can offer companies real, tangible security advantages. Before you assume that on-site is the only way to keep data safe, it's worth taking a comprehensive approach to evaluating risks. Doing so can lead to big benefits.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds