Click here to Skip to main content
13,052,342 members (47,751 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 24 Jan 2005

EXE Name From CWnd*

, 24 Jan 2005
Rate this:
Please Sign up or sign in to vote.
Retrieve executable name from CWnd.

Sample Image


Recently, our company decided to produce a time management application. It was not enough for us to just know when people clocked in and clocked out, we wanted to know what they did while they were there. We decided that we would produce a timekeeping application which would allow us to track exactly what the user is doing while logged in. Initially, we thought we would record the title of the active window every second. This provided us more information than we wanted to know as well as made it cumbersome to do reporting on the data. We then decided it would be best to record just the executable name. I looked around for an article that I had seen before on how to do this, but it was not designed to run once a second. So I wrote this function which allows you to easily convert a CWnd* to the EXE name.

Using the code

The code is simple to use. The function GetWindowModuleName is passed a CWnd* and it returns the name of the executable.

#include "Psapi.h"

CString GetWindowModuleName(CWnd *pWnd)
    HMODULE* lphModule;
    char FileName[1024];
    DWORD procid = 0;
    DWORD modulesize = 0;
    BOOL bInheritHandle = false;

    if(pWnd != NULL){
        HANDLE process = OpenProcess(PROCESS_ALL_ACCESS | 
           PROCESS_QUERY_INFORMATION, bInheritHandle,procid);
        if(process != NULL){
            lphModule = new HMODULE[1];
                      (sizeof(HMODULE)),&modulesize) != 0){
                delete lphModule;
                return FileName;
            delete lphModule;
    return "";

How does it work?

The function first calls the GetWindowThreadProcessId to get the current process ID. From there, we open the process and enumerate the process modules. The trick here is to send the EnumProcessModules function an array of HMODULE of length one. This will pull only the first module of the process which is the executable. Do not forget to include "Psapi.h" or link to Psapi.Lib.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Stephen Love
Software Developer Logic Source
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralC# Pin
jlkdaslkfjd10-Jun-11 2:39
memberjlkdaslkfjd10-Jun-11 2:39 
QuestionDo you have a solution that can detect an window generated by a 64 bit system? Pin
dtgbrown4-May-09 15:52
memberdtgbrown4-May-09 15:52 
GeneralFor those who don't have the include file "Psapi.h" header file. Pin
spinoza2-Jun-05 21:38
memberspinoza2-Jun-05 21:38 
QuestionToo much access requested? Pin
Blake Miller26-Jan-05 8:36
memberBlake Miller26-Jan-05 8:36 
GeneralNot tricky at all Pin
Tom Collins24-Jan-05 22:54
memberTom Collins24-Jan-05 22:54 
GeneralRe: Not tricky at all Pin
Diarrhio25-Jan-05 9:24
memberDiarrhio25-Jan-05 9:24 
GeneralRe: Not tricky at all Pin
Tom Collins25-Jan-05 23:38
memberTom Collins25-Jan-05 23:38 

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
Web02 | 2.8.170713.1 | Last Updated 25 Jan 2005
Article Copyright 2005 by Stephen Love
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid