Click here to Skip to main content
Licence 
First Posted 17 Jul 2006
Views 48,401
Bookmarked 31 times

Finding module name from the window handle

By | 17 Jul 2006 | Article
This article describes how to find the module name from the window handle.

Introduction

A few days back, in one of my projects, I needed to enumerate all the visible windows, and then needed to determine if a given application was running or not. I first struggled a little, but soon found a simple method to do that.

Enumerating the windows

With the EnumWindows API, I filtered out the visible windows. Next, with GetWindowThreadProcessId, I found the process ID of that window, and with that, I found the name of the executable file.

Below is the callback function for EnumWindows:

BOOL CALLBACK CProcessDlg::EnumWindowsProc (HWND hWnd, LPARAM lParam)
{
    CProcessDlg *pDlg = (CProcessDlg *)lParam;

    int nItem = 0;

    //Make sure that the window is visible
    TCHAR szWindowText [MAX_PATH];
    if (!::IsWindowVisible (hWnd))
        return TRUE;

    //Get the text on the title bar
    ::GetWindowText (hWnd, szWindowText, MAX_PATH);

    //If the window is Process Manager then don't display it
    if (_tcsstr (_T("Program Manager"), szWindowText))
        return TRUE;

    //Get process ID
    DWORD dwProcessID;
    GetWindowThreadProcessId (hWnd, &dwProcessID);
    
    //Get the name of the executable file
    CString strEXEName = pDlg->GetEXEName (dwProcessID);

    //Add the info to the list control
    nItem = pDlg->m_List.InsertItem (0, szWindowText);
    pDlg->m_List.SetItemText (nItem, 1, strEXEName);    

    return TRUE;
}

Finding module name

The GetEXEName procedure returns the name of the executable file from its process ID:

#include "psapi.h"
#pragma comment(lib, "psapi.lib")

CString GetEXEName(DWORD dwProcessID)
{
    DWORD aProcesses [1024], cbNeeded, cProcesses;
    unsigned int i;
        
    //Enumerate all processes
    if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
        return NULL;

    // Calculate how many process identifiers were returned.
    cProcesses = cbNeeded / sizeof(DWORD);

    TCHAR szEXEName[MAX_PATH];
    //Loop through all process to find the one that matches
    //the one we are looking for
    for (i = 0; i < cProcesses; i++)
    {
        if (aProcesses [i] == dwProcessID)
        {
            // Get a handle to the process
            HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
                              PROCESS_VM_READ, FALSE, dwProcessID);
        
            // Get the process name
            if (NULL != hProcess)
            {
                HMODULE hMod;
                DWORD cbNeeded;
            
                if(EnumProcessModules(hProcess, &hMod, 
                                      sizeof(hMod), &cbNeeded))
                {
                    //Get the name of the exe file
                    GetModuleBaseName(hProcess, hMod, szEXEName, 
                        sizeof(szEXEName)/sizeof(TCHAR));

                    return CString (szEXEName);
                }
            }
        }    
    }

    return NULL;
}

Conclusion

The code is very easy to understand and follow, please see the demo project if you have any trouble using it.

I hope you find it useful.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Hitesh Sharma



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionCompiling problem PinmemberInkasator1:35 11 Sep '07  
GeneralAnother way to get the enum the running processes and get their module name Pinmembervertex_x23:47 15 Jan '07  
GeneralRe: Another way to get the enum the running processes and get their module name Pinmembervertex_x23:48 15 Jan '07  
GeneralRe: Another way to get the enum the running processes and get their module name Pinmemberhitesh_sharma1:23 16 Jan '07  
GeneralRe: Another way to get the enum the running processes and get their module name PinmemberOmar.Pessoa6:35 19 Jun '08  
Generalbug Pinmembertwinssen1:05 6 Jan '07  
QuestionProblem? Pinmemberkiranin7:48 3 Dec '06  
AnswerRe: Problem? Pinmemberhitesh_sharma22:45 3 Dec '06  
GeneralRe: Problem? Pinmemberkiranin23:12 3 Dec '06  
GeneralRe: Problem? Pinmemberhitesh_sharma2:10 4 Dec '06  
GeneralRe: Problem? Pinmemberkiranin5:55 4 Dec '06  
QuestionCan we get the URLS? Pinmemberkiranin7:16 3 Dec '06  
GeneralA simpler way PinmemberDo Trung Hieu15:32 25 Jul '06  
GeneralRe: A simpler way Pinmemberhitesh_sharma5:59 26 Jul '06  
GeneralRelated page PinmemberJJimenezShaw21:50 24 Jul '06  
GeneralWay to use the Above MFC in c#.NET Pinmemberajai80851:46 18 Jul '06  
GeneralRe: Alternate way to get module name Pinmemberhitesh_sharma2:42 18 Jul '06  
GeneralRe: Alternate way to get module name Pinmemberajai80853:13 18 Jul '06  
GeneralRe: Alternate way to get module name Pinmemberhitesh_sharma4:25 18 Jul '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 17 Jul 2006
Article Copyright 2006 by Hitesh Sharma
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid