Winapi - Receive the process name and PID in C++

Asked

Viewed 22 times

0

I am working on Windows processes, detect process, receive image name (Program) and your PID using Winapi.

#include <windows.h>
#include <tlhelp32.h>
#include <tchar.h>
#include <stdio.h>
#include <list>
#include <iostream>
using namespace std;
//  Forward declarations:
BOOL GetProcessList(list<const char*>*);
void printError( TCHAR* msg );

int main()
{
    list<const char*> process;
    
  GetProcessList(&process);
  
  
  for (std::list<const char*>::iterator it = process.begin(); it != process.end(); it++)
    std::cout << *it << '\n';
  return 0;
}

BOOL GetProcessList(list<const char*>*result)
{
  HANDLE hProcessSnap;
  HANDLE hProcess;
  PROCESSENTRY32 pe32;
  DWORD dwPriorityClass;

  // Take a snapshot of all processes in the system.
  hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
  if( hProcessSnap == INVALID_HANDLE_VALUE )
  {
    printError( TEXT("CreateToolhelp32Snapshot (of processes)") );
    return( FALSE );
  }

  // Set the size of the structure before using it.
  pe32.dwSize = sizeof( PROCESSENTRY32 );

  // Retrieve information about the first process,
  // and exit if unsuccessful
  if( !Process32First( hProcessSnap, &pe32 ) )
  {
    printError( TEXT("Process32First") ); // show cause of failure
    CloseHandle( hProcessSnap );          // clean the snapshot object
    return( FALSE );
  }

  // Now walk the snapshot of processes, and
  // display information about each process in turn
  do
  {
    _tprintf( TEXT("\nPROCESS NAME:  %s"), pe32.szExeFile );
    string out;
    out = pe32.szExeFile;
    result->push_back(TEXT(out.c_str()));

  } while( Process32Next( hProcessSnap, &pe32 ) );

  CloseHandle( hProcessSnap );
  return( TRUE );
}

void printError( TCHAR* msg )
{
  DWORD eNum;
  TCHAR sysMsg[256];
  TCHAR* p;

  eNum = GetLastError( );
  FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
         NULL, eNum,
         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
         sysMsg, 256, NULL );

  // Trim the end of the line and terminate it with a null
  p = sysMsg;
  while( ( *p > 31 ) || ( *p == 9 ) )
    ++p;
  do { *p-- = 0; } while( ( p >= sysMsg ) &&
                          ( ( *p == '.' ) || ( *p < 33 ) ) );

  // Display the message
  _tprintf( TEXT("\n  WARNING: %s failed with error %d (%s)"), msg, eNum, sysMsg );
}

I wanted to return a list in function GetProcessList() but I needed to make it return BOLL then I added a parameter to receive the address of a list<const char*> and then iterate to see if you received the processes. PrintError(TCHAR msg) prints an error that may have happened during the process.


PROCESS NAME:  [System Process] //De tprintf
PROCESS NAME:  System
PROCESS NAME:  smss.exe
PROCESS NAME:  csrss.exe
PROCESS NAME:  wininit.exe
PROCESS NAME:  csrss.exe
PROCESS NAME:  winlogon.exe
PROCESS NAME:  services.exe
PROCESS NAME:  lsass.exe
PROCESS NAME:  lsm.exe
PROCESS NAME:  svchost.exe
PROCESS NAME:  nvvsvc.exe
PROCESS NAME:  svchost.exe
PROCESS NAME:  MsMpEng.exe
PROCESS NAME:  svchost.exe
PROCESS NAME:  svchost.exe
PROCESS NAME:  svchost.exe
PROCESS NAME:  svchost.exe
PROCESS NAME:  svchost.exe
PROCESS NAME:  nvxdsync.exe
PROCESS NAME:  spoolsv.exe
PROCESS NAME:  svchost.exe
PROCESS NAME:  svchost.exe
PROCESS NAME:  svchost.exe
PROCESS NAME:  taskhost.exe
PROCESS NAME:  dwm.exe
PROCESS NAME:  explorer.exe
PROCESS NAME:  msseces.exe
PROCESS NAME:  StikyNot.exe
PROCESS NAME:  GoogleCrashHandler.exe
PROCESS NAME:  SearchIndexer.exe
PROCESS NAME:  NisSrv.exe
PROCESS NAME:  svchost.exe
PROCESS NAME:  sppsvc.exe
PROCESS NAME:  wmpnetwk.exe
PROCESS NAME:  audiodg.exe
PROCESS NAME:  Core Temp.exe
PROCESS NAME:  devcpp.exe
PROCESS NAME:  chrome.exe
PROCESS NAME:  chrome.exe
PROCESS NAME:  chrome.exe
PROCESS NAME:  chrome.exe
PROCESS NAME:  chrome.exe
PROCESS NAME:  chrome.exe
PROCESS NAME:  chrome.exe
PROCESS NAME:  chrome.exe
PROCESS NAME:  chrome.exe
PROCESS NAME:  cmd.exe
PROCESS NAME:  conhost.exe
PROCESS NAME:  Main.exe
                         //De list<const char*>













Main.exe
°m
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe
Main.exe

The output came out with just the name of the program running it. So I don’t know what’s wrong and would it be so if I did with PID? This is my API project.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.