Click here to Skip to main content
11,492,914 members (61,190 online)
Click here to Skip to main content

Advanced Task Manager in MFC

, 4 Dec 2005 CPOL 58.3K 2.7K 43
Rate this:
Please Sign up or sign in to vote.
This article demonstrates how to create a Task Manager in MFC which lists all the processes running in the system along with their process details and version information.

Sample Image - AdvancedTaskManager.jpg

Introduction

This article demonstrates how to create a task manager using MFC. This task manager known as Advanced Task Manager will display extra information than a standard task manager, like the properties of a running executable and process details.

Background

The standard task manager available will show the details about running processes, but in some cases, we need to know the path of the executable from which the process is running, which is not available. This application will show the details of a process like the path from which it is executing, product name, product version etc.

Implementation

The design is simple here. There are three classes which perform the action. CProcessEnumerator is responsible for loading all NT processes. All these processes are loaded to a list control. CProcessViewer can provide information about a specified process, and CVersionViewer can provide information about a running executable.

The following code enumerates all the running processes on a WIN NT/2000 system:

//
BOOL CProcessEnumerator::EnumWinNTProcs( PROCENUMPROC lpProc, LPARAM lParam )
{
    LPDWORD        lpdwPIDs ;
    DWORD          dwSize, dwSize2, dwIndex ;
    HMODULE        hMod ;
    HANDLE         hProcess ;
    char           szFileName[ MAX_PATH ] ;
    ENUMINFOSTRUCT sInfo ;

    dwSize2 = 256 * sizeof( DWORD ) ;
    lpdwPIDs = NULL ;
    do
    {
        if( lpdwPIDs )
        {
            HeapFree( GetProcessHeap(), 0, lpdwPIDs ) ;
            dwSize2 *= 2 ;
        }
        lpdwPIDs = (LPDWORD)HeapAlloc( GetProcessHeap(), 0, dwSize2 );
        if( lpdwPIDs == NULL )
        {
            return FALSE ;
        }
        if( !m_lpfEnumProcesses( lpdwPIDs, dwSize2, &dwSize ) )
        {
            HeapFree( GetProcessHeap(), 0, lpdwPIDs ) ;
            return FALSE ;
        }
    }while( dwSize == dwSize2 ) ;
    
    dwSize /= sizeof( DWORD ) ;
    
    // Loop through each ProcID.
    for( dwIndex = 0 ; dwIndex < dwSize ; dwIndex++ )
    {
        szFileName[0] = 0 ;
        hProcess = OpenProcess(
            PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
            FALSE, lpdwPIDs[ dwIndex ] ) ;
        if( hProcess != NULL )
        {
            if( m_lpfEnumProcessModules( hProcess, &hMod,
                sizeof( hMod ), &dwSize2 ) )
            {
                // Get Full pathname:
                if( !m_lpfGetModuleFileNameEx( hProcess, hMod,
                    szFileName, sizeof( szFileName ) ) )
                {
                    szFileName[0] = 0 ;
                }
            }
            CloseHandle( hProcess ) ;
        }
        DWORD lastErr = GetLastError();
        if(!lpProc( lpdwPIDs[dwIndex], 0, szFileName, lParam))
            break ;
        if( _stricmp( szFileName+(strlen(szFileName)-9),
            "NTVDM.EXE")==0)
        {
            sInfo.dwPID = lpdwPIDs[dwIndex] ;
            sInfo.lpProc = lpProc ;
            sInfo.lParam = lParam ;
            sInfo.bEnd = FALSE ;
            try
            {
                m_lpfVDMEnumTaskWOWEx( lpdwPIDs[dwIndex],
                    (TASKENUMPROCEX) Enum16,
                    (LPARAM) &sInfo);
            }
            catch(...)
            {
                continue;
            }
            if(sInfo.bEnd)
                break ;
        }
    }
    
    HeapFree( GetProcessHeap(), 0, lpdwPIDs ) ;
    
    return TRUE ;
}


BOOL CProcessEnumerator::Enum16( 
                DWORD dwThreadId, 
                WORD hMod16, 
                WORD hTask16,
                PSZ pszModName, 
                PSZ pszFileName, 
                LPARAM lpUserDefined )
{
  BOOL bRet ;

  ENUMINFOSTRUCT *psInfo = (ENUMINFOSTRUCT *)lpUserDefined ;

  bRet = psInfo->lpProc( psInfo->dwPID, 
         hTask16, pszFileName, psInfo->lParam ) ;

  if(!bRet)
  {
     psInfo->bEnd = TRUE ;
  }

  return !bRet;
} 
//

License

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

Share

About the Author

Madhu Raykar
Web Developer
India India
I have been working in software industry for the past 5 years now. Enjoy working on complex and new technologies. Worked on Microsoft technologies Like VC++, COM, XML, SQL. Currently working on .Net, C#.

Comments and Discussions

 
GeneralСomment Pin
v.k.l.chr..by17-May-10 12:19
memberv.k.l.chr..by17-May-10 12:19 
GeneralPls help Pin
jinijinu16-Sep-08 23:46
memberjinijinu16-Sep-08 23:46 
GeneralPls help Pin
jinijinu16-Sep-08 19:47
memberjinijinu16-Sep-08 19:47 
GeneralFamilar UI Pin
Vasudevan Deepak Kumar30-Jul-07 19:24
memberVasudevan Deepak Kumar30-Jul-07 19:24 
Generalapi Pin
beto.ch30-Jan-06 1:58
memberbeto.ch30-Jan-06 1:58 
Generalhelp please... Pin
tzvila23-Dec-05 0:35
membertzvila23-Dec-05 0:35 
GeneralRe: help please... Pin
Ianussea16-Apr-07 21:22
memberIanussea16-Apr-07 21:22 
QuestionHow to get Application Owner Name? Pin
lellasr11-Dec-05 20:29
memberlellasr11-Dec-05 20:29 
AnswerRe: How to get Application Owner Name? Pin
Madhu Raykar11-Dec-05 23:02
memberMadhu Raykar11-Dec-05 23:02 
GeneralProblems Pin
JimD.99998-Dec-05 5:25
memberJimD.99998-Dec-05 5:25 
QuestionWindows version? Pin
Mister Transistor6-Dec-05 11:57
memberMister Transistor6-Dec-05 11:57 
AnswerRe: Windows version? Pin
Madhu Raykar6-Dec-05 18:40
memberMadhu Raykar6-Dec-05 18:40 
GeneralRe: Windows version? Pin
Wessam Fathi6-Dec-05 23:24
memberWessam Fathi6-Dec-05 23:24 
GeneralRe: Windows version? Pin
Madhu Raykar8-Dec-05 1:13
memberMadhu Raykar8-Dec-05 1:13 
GeneralRe: Windows version? Pin
Vasudevan Deepak Kumar30-Jul-07 19:25
memberVasudevan Deepak Kumar30-Jul-07 19:25 
Generalfeature requests Pin
nnnnnnn6-Dec-05 5:29
membernnnnnnn6-Dec-05 5:29 
GeneralRe: feature requests Pin
Madhu Raykar6-Dec-05 6:25
memberMadhu Raykar6-Dec-05 6:25 
GeneralCool Pin
Razr3334-Dec-05 6:33
memberRazr3334-Dec-05 6:33 
GeneralRe: Cool Pin
Dandy Cheung4-Dec-05 15:33
memberDandy Cheung4-Dec-05 15:33 

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 | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 4 Dec 2005
Article Copyright 2005 by Madhu Raykar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid