Homepage image
Sample of the Day RSS Feed

Sample Download: http://code.msdn.microsoft.com/CppCheckProcessType-1f81439d

Today’s code sample demonstrates how to list process type information for all running processes in C++, such as

  • Is a Console Application or Is a Windows Application
  • Is a Managed Application or Is a Native Application
  • Is a .NET 4.0 Application
  • Is a WPF Application
  • Is a 64 Bit Application or Is a 32 Bit Application

These topics are frequently asked and needed by developers, so Microsoft All-In-One Code Framework provides the code sample.

The sample was written by Microsoft Software Development Engineer Amit Dey.

imageYou can find more code samples that demonstrate the most typical programming scenarios by using Microsoft All-In-One Code Framework Sample Browser or Sample Browser Visual Studio extension. They give you the flexibility to search samples, download samples on demand, manage the downloaded samples in a centralized place, and automatically be notified about sample updates. If it is the first time that you hear about Microsoft All-In-One Code Framework, please watch the introduction video on Microsoft Showcase, or read the introduction on our homepage http://1code.codeplex.com/.

 

Introduction

This sample code lists process type information for all running processes. Like:

  • Is a Console Application or Is a Windows Application
  • Is a Managed Application or Is a Native Application
  • Is a .NET 4.0 Application
  • Is a WPF Application
  • Is a 64 Bit Application or Is a 32 Bit Application

 

Building the Sample

1) Open Project in Visual Studio 2010

2) Go to "Build" -> "Build Solution"

 

Running the Sample

1) Open project in Visual Studio 2010

2) Go to "Debug" -> "Start Debugging"

3) Click on "Refresh"

4) All running processes will be listed with information

image

 

Using the Code

The code sample provides following reusable components

1)  For a particular process ID, you can determine various process type information

ProcessInfo piProcessInfo(dwProcessID); 
cout << piProcessInfo.m_fIsManaged << endl; 
cout << piProcessInfo.m_fIsDotNet4 << endl; 
cout << piProcessInfo.m_fIsWPF << endl; 
cout << piProcessInfo.m_fIs64Bit << endl; 
cout << piProcessInfo.m_fIsConsole << endl;

2)  You can also list process type information for all running processes

ProcessInfo piProcesssesInfo[SIZE]; 
DWORD cbProcessInfo; 
DWORD cProcessInfo; 
DWORD fResult; 
UINT i; 
TCHAR line[SIZE] = L"";     
  
Enumerator enumerator; 
  
fResult = enumerator.EnumerateProcessInfo(piProcesssesInfo,sizeof(piProcesssesInfo),&cbProcessInfo); 
  
if(!fResult) 
{ 
    return; 
} 
  
cProcessInfo = cbProcessInfo / sizeof(ProcessInfo);     
  
SendMessage(g_hProListBox, LB_RESETCONTENT , 0, 0); 
  
for( i = 0; i < cProcessInfo; i++) 
{ 
    if(piProcesssesInfo[i].m_fIsValid) 
    { 
        StringCchPrintf(line,SIZE,L"PID : %d",piProcesssesInfo[i].m_dwProcessID);             
        SendMessage(g_hProListBox, LB_ADDSTRING, 0, (LPARAM)line);     
        SendMessage(g_hProListBox, LB_ADDSTRING, 0, (LPARAM)piProcesssesInfo[i].m_szFileName); 
        SendMessage(g_hProListBox, LB_ADDSTRING, 0, (LPARAM)(piProcesssesInfo[i].m_fIsManaged?L"Managed Application":L"Native Application"));     
        SendMessage(g_hProListBox, LB_ADDSTRING, 0, (LPARAM)(piProcesssesInfo[i].m_fIsDotNet4?L".NET 4.0 Application":L"Not .NET 4.0 Application"));     
        SendMessage(g_hProListBox, LB_ADDSTRING, 0, (LPARAM)(piProcesssesInfo[i].m_fIsWPF?L"WPF Application":L"Not WPF Application"));     
        SendMessage(g_hProListBox, LB_ADDSTRING, 0, (LPARAM)(piProcesssesInfo[i].m_fIs64Bit?L"64 Bit Application":L"32 Bit Application"));     
        SendMessage(g_hProListBox, LB_ADDSTRING, 0, (LPARAM)(piProcesssesInfo[i].m_fIsConsole?L"Console Application":L"Windows Application"));     
        SendMessage(g_hProListBox, LB_ADDSTRING, 0, (LPARAM)L"");     
    } 
} 

 

More Information

GetConsoleMode Function
http://msdn.microsoft.com/en-us/library/ms683167(VS.85).aspx
 
GetStdHandle Function
http://msdn.microsoft.com/en-us/library/ms683231(VS.85).aspx
 
AttachConsole Function
 http://msdn.microsoft.com/en-us/library/ms681952(VS.85).aspx
 
FreeConsole Function
http://msdn.microsoft.com/en-us/library/ms683150(VS.85).aspx
 
Determine Whether a Program Is a Console or GUI Application
http://www.devx.com/tips/Tip/33584
 
EnumProcessModulesEx Function
http://msdn.microsoft.com/en-us/library/ms682633(VS.85).aspx
 
GetModuleFileNameEx Function
http://msdn.microsoft.com/en-us/library/ms683198(VS.85).aspx
 
IsWow64Process Function
http://msdn.microsoft.com/en-us/library/ms684139(VS.85).aspx