Click here to Skip to main content
13,192,207 members (33,860 online)
Click here to Skip to main content
Add your own
alternative version


37 bookmarked
Posted 17 Jul 2006

Finding module name from the window handle

, 17 Jul 2006
Rate this:
Please Sign up or sign in to vote.
This article describes how to find the module name from the window handle.


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);

    //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, 

                    return CString (szEXEName);

    return NULL;


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.


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
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionThanks hitesh Pin
iampradeepsharma26-Sep-15 19:07
memberiampradeepsharma26-Sep-15 19:07 
QuestionCompiling problem Pin
Inkasator11-Sep-07 1:35
memberInkasator11-Sep-07 1:35 
GeneralAnother way to get the enum the running processes and get their module name Pin
vertex_x15-Jan-07 23:47
membervertex_x15-Jan-07 23:47 
HANDLE hSnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);<br />
<br />
PROCESSENTRY32* processInfo=new PROCESSENTRY32;<br />
<br />
processInfo->dwSize=sizeof(PROCESSENTRY32);<br />
int index=0;<br />
bool bFound = false;<br />
<br />
CString sProcessName;<br />
while(Process32Next(hSnapShot,processInfo)!=FALSE) {<br />
	sProcessName = processInfo->szExeFile;<br />
	printf("process name: %s\n", sProcessName);<br />
}<br />
<br />
CloseHandle(hSnapShot);<br />
delete processInfo;

see also the documentation of CreateToolhelp32Snapshot:
The CreateToolhelp32Snapshot function takes a snapshot of the specified processes in the system, as well as the heaps, modules, and threads used by these processes.

I guess there is a lot of interesting stuff you can do with it.
GeneralRe: Another way to get the enum the running processes and get their module name Pin
vertex_x15-Jan-07 23:48
membervertex_x15-Jan-07 23:48 
GeneralRe: Another way to get the enum the running processes and get their module name Pin
hitesh_sharma16-Jan-07 1:23
memberhitesh_sharma16-Jan-07 1:23 
GeneralRe: Another way to get the enum the running processes and get their module name Pin
Omar.Pessoa19-Jun-08 6:35
memberOmar.Pessoa19-Jun-08 6:35 
Generalbug Pin
twinssen6-Jan-07 1:05
membertwinssen6-Jan-07 1:05 
QuestionProblem? Pin
kiranin3-Dec-06 7:48
memberkiranin3-Dec-06 7:48 
AnswerRe: Problem? Pin
hitesh_sharma3-Dec-06 22:45
memberhitesh_sharma3-Dec-06 22:45 
GeneralRe: Problem? Pin
kiranin3-Dec-06 23:12
memberkiranin3-Dec-06 23:12 
GeneralRe: Problem? Pin
hitesh_sharma4-Dec-06 2:10
memberhitesh_sharma4-Dec-06 2:10 
GeneralRe: Problem? Pin
kiranin4-Dec-06 5:55
memberkiranin4-Dec-06 5:55 
QuestionCan we get the URLS? Pin
kiranin3-Dec-06 7:16
memberkiranin3-Dec-06 7:16 
GeneralA simpler way Pin
Do Trung Hieu25-Jul-06 15:32
memberDo Trung Hieu25-Jul-06 15:32 
GeneralRe: A simpler way Pin
hitesh_sharma26-Jul-06 5:59
memberhitesh_sharma26-Jul-06 5:59 
GeneralRelated page Pin
JJimenezShaw24-Jul-06 21:50
memberJJimenezShaw24-Jul-06 21:50 
GeneralWay to use the Above MFC in c#.NET Pin
ajai808518-Jul-06 1:46
memberajai808518-Jul-06 1:46 
GeneralRe: Alternate way to get module name Pin
hitesh_sharma18-Jul-06 2:42
memberhitesh_sharma18-Jul-06 2:42 
GeneralRe: Alternate way to get module name Pin
ajai808518-Jul-06 3:13
memberajai808518-Jul-06 3:13 
GeneralRe: Alternate way to get module name Pin
hitesh_sharma18-Jul-06 4:25
memberhitesh_sharma18-Jul-06 4:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web04 | 2.8.171017.2 | Last Updated 17 Jul 2006
Article Copyright 2006 by Hitesh Sharma
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid