Support Forums
List Processes - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Programming with C++ (https://www.supportforums.net/forumdisplay.php?fid=20)
+---- Thread: List Processes (/showthread.php?tid=692)



List Processes - brett7 - 10-08-2009

Just a small c++ script I found ages ago but editted slightly by me, when run this program will show all process's that are running as well as their ids. Can be usefull for use with other scripts or to help protect your system from viruses


Code:
/*When run this program will show all processes that are running as well as their ids*/
#include <stdio.h>
#include <windows.h>
#include <tlhelp32.h>

int main(void)
{
    int info;
    HANDLE Snap;
    PROCESSENTRY32 proc32;
    
    Snap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);/*take a snap of all processes*/
  if(Snap==INVALID_HANDLE_VALUE)
  {
    printf("Error creating snapshot of current processes");
    return EXIT_FAILURE;
  }
  proc32.dwSize=sizeof(PROCESSENTRY32); /*set size of structure*/  
  
  system("cls");
  printf("Process Viewer\n");
  printf("PID:\t\tPROCESS NAME:\n");
  while((Process32Next(Snap,&proc32))==TRUE)/*while we haven't reached the final process*/
  {
      printf("\n%d\t\t%s",proc32.th32ProcessID,proc32.szExeFile);/*print pid and processname*/
  }
  printf("\n");
  CloseHandle(Snap);/*cleaning up*/  
  system("PAUSE");            
  return EXIT_SUCCESS;
}



RE: List Processes - charnet3d - 10-09-2009

Thanks that's useful!
Just a little addition is that when used as it is in Visual Studio I can't see the full process name, just the first character.
The solution to that is to have the Conversion Code %ls instead of %s in printf, because proc32.szExeFile is of type WCHAR

printf("\n%d\t\t%ls",proc32.th32ProcessID,proc32.szExeFile);/*print pid and processname*/


RE: List Processes - brett7 - 10-09-2009

ah ok, i just use dev-cpp so i didn't test with visual studio but thanks for that Smile


RE: List Processes - Akshay* - 10-10-2009

Usefull