Click here to Skip to main content
15,888,579 members
Articles / Desktop Programming / MFC
Article

EXE Name From CWnd*

Rate me:
Please Sign up or sign in to vote.
2.60/5 (8 votes)
24 Jan 2005CPOL1 min read 49.6K   507   15   7
Retrieve executable name from CWnd.

Sample Image

Introduction

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){
        GetWindowThreadProcessId(pWnd->m_hWnd,&procid);
        HANDLE process = OpenProcess(PROCESS_ALL_ACCESS | 
           PROCESS_QUERY_INFORMATION, bInheritHandle,procid);
        if(process != NULL){
            lphModule = new HMODULE[1];
            if(EnumProcessModules(process,lphModule, 
                      (sizeof(HMODULE)),&modulesize) != 0){
                GetModuleBaseName(process,lphModule[0],FileName,1024);
                CloseHandle(process);
                delete lphModule;
                return FileName;
            }
            delete lphModule;
        }
        CloseHandle(process);
    }
    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.

License

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


Written By
Software Developer Logic Source
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralC# Pin
jlkdaslkfjd10-Jun-11 2:39
jlkdaslkfjd10-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
dtgbrown4-May-09 15:52 
GeneralFor those who don't have the include file "Psapi.h" header file. Pin
spinoza2-Jun-05 21:38
spinoza2-Jun-05 21:38 
QuestionToo much access requested? Pin
Blake Miller26-Jan-05 8:36
Blake Miller26-Jan-05 8:36 
GeneralNot tricky at all Pin
<b>T</b>om <b>C</b>ollins24-Jan-05 22:54
<b>T</b>om <b>C</b>ollins24-Jan-05 22:54 
GeneralRe: Not tricky at all Pin
Diarrhio25-Jan-05 9:24
Diarrhio25-Jan-05 9:24 
GeneralRe: Not tricky at all Pin
<b>T</b>om <b>C</b>ollins25-Jan-05 23:38
<b>T</b>om <b>C</b>ollins25-Jan-05 23:38 
Sorry, I didn't see the Beginner highlight. I still can't see it, but it doesen't matter.
You 're right. This article doesn't hurt anybody Poke tongue | ;-P . ( But on the other hand, does it really help ?? Confused | :confused: )
What I told in my first message were just some improvements and the fact that this is not a particulary tricky way. This article can easyly be improved by supporting Win9x Platforms. And this is what I call tricky, besause there is no PS-Api on Win9x Systems.OMG | :OMG:

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.