Click here to Skip to main content
Click here to Skip to main content
Go to top

EXE Name From CWnd*

, 24 Jan 2005
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

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

Comments and Discussions

 
GeneralC# Pinmemberjlkdaslkfjd10-Jun-11 2:39 
QuestionDo you have a solution that can detect an window generated by a 64 bit system? Pinmemberdtgbrown4-May-09 15:52 
GeneralFor those who don't have the include file "Psapi.h" header file. Pinmemberspinoza2-Jun-05 21:38 
QuestionToo much access requested? PinmemberBlake Miller26-Jan-05 8:36 
GeneralNot tricky at all PinmemberTom Collins24-Jan-05 22:54 
GeneralRe: Not tricky at all PinmemberDiarrhio25-Jan-05 9:24 
GeneralRe: Not tricky at all PinmemberTom Collins25-Jan-05 23:38 

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.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 25 Jan 2005
Article Copyright 2005 by Stephen Love
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid