Click here to Skip to main content
15,885,917 members
Articles / Desktop Programming / MFC

Advanced Task Manager in MFC

Rate me:
Please Sign up or sign in to vote.
4.57/5 (19 votes)
4 Dec 2005CPOL 82.7K   4.3K   48   19
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)


Written By
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 11:19
v.k.l.chr..by17-May-10 11:19 
GeneralPls help Pin
jinijinu16-Sep-08 22:46
jinijinu16-Sep-08 22:46 
GeneralPls help Pin
jinijinu16-Sep-08 18:47
jinijinu16-Sep-08 18:47 
GeneralFamilar UI Pin
Vasudevan Deepak Kumar30-Jul-07 18:24
Vasudevan Deepak Kumar30-Jul-07 18:24 
Generalapi Pin
beto.ch30-Jan-06 0:58
beto.ch30-Jan-06 0:58 
Generalhelp please... Pin
tzvila22-Dec-05 23:35
tzvila22-Dec-05 23:35 
GeneralRe: help please... Pin
Ianussea16-Apr-07 20:22
Ianussea16-Apr-07 20:22 
QuestionHow to get Application Owner Name? Pin
lellasr11-Dec-05 19:29
lellasr11-Dec-05 19:29 
AnswerRe: How to get Application Owner Name? Pin
Madhu Raykar11-Dec-05 22:02
Madhu Raykar11-Dec-05 22:02 
GeneralProblems Pin
JimD.99998-Dec-05 4:25
JimD.99998-Dec-05 4:25 
QuestionWindows version? Pin
Mister Transistor6-Dec-05 10:57
Mister Transistor6-Dec-05 10:57 
AnswerRe: Windows version? Pin
Madhu Raykar6-Dec-05 17:40
Madhu Raykar6-Dec-05 17:40 
GeneralRe: Windows version? Pin
Wessam Fathi6-Dec-05 22:24
Wessam Fathi6-Dec-05 22:24 
GeneralRe: Windows version? Pin
Madhu Raykar8-Dec-05 0:13
Madhu Raykar8-Dec-05 0:13 
GeneralRe: Windows version? Pin
Vasudevan Deepak Kumar30-Jul-07 18:25
Vasudevan Deepak Kumar30-Jul-07 18:25 
Generalfeature requests Pin
name_or_alias6-Dec-05 4:29
name_or_alias6-Dec-05 4:29 
GeneralRe: feature requests Pin
Madhu Raykar6-Dec-05 5:25
Madhu Raykar6-Dec-05 5:25 
GeneralCool Pin
Razr3334-Dec-05 5:33
Razr3334-Dec-05 5:33 
Thanks Wink | ;)

Hi, all
GeneralRe: Cool Pin
Dandy Cheung4-Dec-05 14:33
Dandy Cheung4-Dec-05 14:33 

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.