Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++] Listing all processes in a console window
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Anden100
Grandmaster Cheater
Reputation: 0

Joined: 20 Apr 2007
Posts: 668

PostPosted: Sat Jan 24, 2009 4:39 am    Post subject: [C++] Listing all processes in a console window Reply with quote

I've been playing a bit with CreateToolhelp32Snapshot, and i succeded listing all processes Wink

The reason that im so proud, is that this is the first "advanced" piece of programming, that i've been coding only using MSDN, without looking at any code examples Very Happy


Code:
#include <windows.h>
#include <iostream>
#include <TlHelp32.h> // Used for CreateToolhelp32Snapshot and Process32Next

int main(){
   HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
   if(hSnap == INVALID_HANDLE_VALUE) // Invalid handle returned by CreateToolhelp32Snapshot
      return 1; // Failed, no need to continue
   PROCESSENTRY32 lppe;
   while(Process32Next(hSnap, &lppe)){ // While Process32Next finds a process
      std::cout << "Process found: " << lppe.szExeFile << std::endl; // Print the process name
   }
   std::cin.sync();
   std::cin.ignore();
   return 0;
}
Back to top
View user's profile Send private message
tombana
Master Cheater
Reputation: 2

Joined: 14 Jun 2007
Posts: 456
Location: The Netherlands

PostPosted: Sat Jan 24, 2009 6:34 am    Post subject: Reply with quote

Great Smile
But I think you are supposed to call Process32First once first before you call Process32Next.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sat Jan 24, 2009 6:52 am    Post subject: Reply with quote

snapshot handle needs to be closed. also note process32first which should be called first needs to have its dwSize parameter filled in. this was my code:
Code:
#include "stdafx.h"
#include "windows.h"
#include "Tlhelp32.h"
#include <conio.h>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
   PROCESSENTRY32 pe32;
   HANDLE hSnapshot;
   HANDLE hConsoleOutput;
   WORD wAttributes = (FOREGROUND_GREEN | FOREGROUND_INTENSITY);
   WORD XorKey = (FOREGROUND_GREEN | FOREGROUND_INTENSITY) ^ (FOREGROUND_RED | FOREGROUND_INTENSITY);
   int Counter = 0;

   hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
   SetConsoleTextAttribute(hConsoleOutput, wAttributes);

   if (hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0))
   {
      pe32.dwSize = sizeof pe32;
      Process32First(hSnapshot, &pe32);
      do
      {
         Sleep(10);
         wAttributes = wAttributes ^ XorKey;
         SetConsoleTextAttribute(hConsoleOutput, wAttributes ^ XorKey);
         Counter++;
         cout << "EXEName\t:\t" << pe32.szExeFile << "\n";
         cout << "PID\t:\t" << pe32.th32ProcessID << "\n\n";
      }
      while(Process32Next(hSnapshot, &pe32));
   }
   else
   {
      cout << "Snapshot could not be taken, error code : " << GetLastError() << "\n";
   }

   SetConsoleTextAttribute(hConsoleOutput, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);

   cout << "\n" << "Number of processes : " << Counter << "\n\n";
   cout << "Press any key to exit.. ";

   CloseHandle(hSnapshot);
   CloseHandle(hConsoleOutput);

   while(!_kbhit())
   {
      Sleep(10);
   }

   return 0;
}

http://pastebin.com/m5349da46
Back to top
View user's profile Send private message
Anden100
Grandmaster Cheater
Reputation: 0

Joined: 20 Apr 2007
Posts: 668

PostPosted: Sat Jan 24, 2009 6:52 am    Post subject: Reply with quote

It works just as well if i do not, but if i wanted to, i could've added:
Code:
Process32First(hSnap, &lppe);
std::cout << "Process found: " << lppe.szExeFile << std::endl;
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sat Jan 24, 2009 7:00 am    Post subject: Reply with quote

not closing handles results in memory leaks especially if it is in a function called many times. that code will also not work as i said dwSize needs to be initialised or process32first fails with bad_length error.
Back to top
View user's profile Send private message
Anden100
Grandmaster Cheater
Reputation: 0

Joined: 20 Apr 2007
Posts: 668

PostPosted: Sat Jan 24, 2009 7:39 am    Post subject: Reply with quote

Slugsnack wrote:
not closing handles results in memory leaks especially if it is in a function called many times. that code will also not work as i said dwSize needs to be initialised or process32first fails with bad_length error.

Would this be better then?:

Code:
#include <windows.h>
#include <iostream>
#include <TlHelp32.h> // Used for CreateToolhelp32Snapshot and Process32Next

int main(){
   HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
   int count = 1;
   if(hSnap == INVALID_HANDLE_VALUE) // Invalid handle returned by CreateToolhelp32Snapshot
      return 1; // Failed, no need to continue
   PROCESSENTRY32 lppe;
   Process32First(hSnap, &lppe);
   do{ // While Process32Next finds a process
      std::cout << "Process found: " << lppe.szExeFile << "   PID: " << lppe.th32ProcessID << std::endl; // Print the process name
      count++;
   }while(Process32Next(hSnap, &lppe));
   std::cout << "Currently running processes: " << count;
   std::cin.sync();
   std::cin.ignore();
   CloseHandle(hSnap);
   return 0;
}


(Keep in mind, that i did still not look at any source examples (neither the one you posted), i wish to only use MSDN as a reference...)
Im using do {} while loop to avoid an additional std::cout

And a quick question about MSVC++, when i debug, and end the debugging, the Compiler will make sure to close all handles, right?
Back to top
View user's profile Send private message
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Sat Jan 24, 2009 7:53 am    Post subject: Reply with quote

increase count after Process32First and also display that process

Anden100 wrote:
Slugsnack wrote:
not closing handles results in memory leaks especially if it is in a function called many times. that code will also not work as i said dwSize needs to be initialised or process32first fails with bad_length error.

Would this be better then?:

Code:
#include <windows.h>
#include <iostream>
#include <TlHelp32.h> // Used for CreateToolhelp32Snapshot and Process32Next

int main(){
   HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
   int count = 1;
   if(hSnap == INVALID_HANDLE_VALUE) // Invalid handle returned by CreateToolhelp32Snapshot
      return 1; // Failed, no need to continue
   PROCESSENTRY32 lppe;
   Process32First(hSnap, &lppe);
   do{ // While Process32Next finds a process
      std::cout << "Process found: " << lppe.szExeFile << "   PID: " << lppe.th32ProcessID << std::endl; // Print the process name
      count++;
   }while(Process32Next(hSnap, &lppe));
   std::cout << "Currently running processes: " << count;
   std::cin.sync();
   std::cin.ignore();
   CloseHandle(hSnap);
   return 0;
}


(Keep in mind, that i did still not look at any source examples (neither the one you posted), i wish to only use MSDN as a reference...)
Im using do {} while loop to avoid an additional std::cout

And a quick question about MSVC++, when i debug, and end the debugging, the Compiler will make sure to close all handles, right?

_________________
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sat Jan 24, 2009 7:53 am    Post subject: Reply with quote

exitprocess will clean up unclosed memory handles, yes but it is good practice to do it yourself. for example if your procedure is constantly called and say in a dll then it is possible it will not be cleaned up for a very very long time. hence memory leak. but when you debug + end debugging yes it will close handles.

msdn specifically says to use process32first/next if you wanna enumerate/walk process list.
Back to top
View user's profile Send private message
Anden100
Grandmaster Cheater
Reputation: 0

Joined: 20 Apr 2007
Posts: 668

PostPosted: Sat Jan 24, 2009 8:04 am    Post subject: Reply with quote

blankrider wrote:
increase count after Process32First and also display that process

Anden100 wrote:
Slugsnack wrote:
not closing handles results in memory leaks especially if it is in a function called many times. that code will also not work as i said dwSize needs to be initialised or process32first fails with bad_length error.

Would this be better then?:

Code:
#include <windows.h>
#include <iostream>
#include <TlHelp32.h> // Used for CreateToolhelp32Snapshot and Process32Next

int main(){
   HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
   int count = 1;
   if(hSnap == INVALID_HANDLE_VALUE) // Invalid handle returned by CreateToolhelp32Snapshot
      return 1; // Failed, no need to continue
   PROCESSENTRY32 lppe;
   Process32First(hSnap, &lppe);
   do{ // While Process32Next finds a process
      std::cout << "Process found: " << lppe.szExeFile << "   PID: " << lppe.th32ProcessID << std::endl; // Print the process name
      count++;
   }while(Process32Next(hSnap, &lppe));
   std::cout << "Currently running processes: " << count;
   std::cin.sync();
   std::cin.ignore();
   CloseHandle(hSnap);
   return 0;
}


(Keep in mind, that i did still not look at any source examples (neither the one you posted), i wish to only use MSDN as a reference...)
Im using do {} while loop to avoid an additional std::cout

And a quick question about MSVC++, when i debug, and end the debugging, the Compiler will make sure to close all handles, right?


Which is why i made a do {...} while(...)..., it will do the loop first, and check after, displaying after Process32First, will result in it shown' twice...
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sat Jan 24, 2009 8:14 am    Post subject: Reply with quote

yep as you can see yours is pretty much the same as mine in terms of that lol
Back to top
View user's profile Send private message
Anden100
Grandmaster Cheater
Reputation: 0

Joined: 20 Apr 2007
Posts: 668

PostPosted: Sat Jan 24, 2009 8:29 am    Post subject: Reply with quote

I attempted to put it in a loop (mainly for fun), would this be the correct way to do it?:

Code:
#include <windows.h>
#include <iostream>
#include <TlHelp32.h> // Used for CreateToolhelp32Snapshot and Process32Next

int main(){
   HANDLE hSnap;
   int count = 0;
   PROCESSENTRY32 lppe;
   while(true){
      hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
      if(hSnap == INVALID_HANDLE_VALUE){ // Invalid handle returned by CreateToolhelp32Snapshot
         CloseHandle(hSnap); // Lets close it, even if its invalid
         return 1; // Failed, no need to continue
      }
      count = 0;
      system("cls");
      Process32First(hSnap, &lppe);
      do{ // While Process32Next finds a process
         std::cout << "Process found: " << lppe.szExeFile << "   PID: " << lppe.th32ProcessID << std::endl; // Print the process name and id
         count++;
      }while(Process32Next(hSnap, &lppe)); // Keep checking if there is more processes, and if there is, copy it to lppe
      std::cout << "Currently running processes: " << count;
      CloseHandle(hSnap);
      Sleep(500);
   }
   std::cin.sync(); // Wait for the user to press [Return]
   std::cin.ignore();
   CloseHandle(hSnap); // Close the handle, to avoid memory leak?
   return 0;
}


Slugsnack wrote:
yep as you can see yours is pretty much the same as mine in terms of that lol

Which might be cause its the best way to do it? O.o Very Happy


Last edited by Anden100 on Sat Jan 24, 2009 8:46 am; edited 1 time in total
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sat Jan 24, 2009 8:39 am    Post subject: Reply with quote

why you closing the handle again ? it only needs to be closed once for each time a snapshot is made. i would personally define your process walk function in a separate procedure/function and call that in a loop instead, it makes it more readable.
Back to top
View user's profile Send private message
Anden100
Grandmaster Cheater
Reputation: 0

Joined: 20 Apr 2007
Posts: 668

PostPosted: Sat Jan 24, 2009 8:42 am    Post subject: Reply with quote

Slugsnack wrote:
why you closing the handle again ? it only needs to be closed once for each time a snapshot is made. i would personally define your process walk function in a separate procedure/function and call that in a loop instead, it makes it more readable.


You mean the CloseHandle right before return 0;?, i kinda forgot to remove it O.o
And what would i do, if i wish to check if an process is on the list? ex: notepad.exe, cas i cant just do
Code:
if(lppe.szExeFile == "notepad.exe")
   std::cout << "Notepad was found\n";


Last edited by Anden100 on Sat Jan 24, 2009 8:44 am; edited 2 times in total
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sat Jan 24, 2009 8:43 am    Post subject: Reply with quote

yes that one
Back to top
View user's profile Send private message
Anden100
Grandmaster Cheater
Reputation: 0

Joined: 20 Apr 2007
Posts: 668

PostPosted: Sat Jan 24, 2009 8:51 am    Post subject: Reply with quote

I wish to check if notepad.exe is running, and this code seems to work, but i was wondering if there was another way to do it?


Code:
#include <windows.h>
#include <iostream>
#include <string>
#include <TlHelp32.h> // Used for CreateToolhelp32Snapshot and Process32Next

int main(){
   char* procname = "notepad.exe";
   char* curname;
   HANDLE hSnap;
   int count = 0;
   PROCESSENTRY32 lppe;
   std::string choise;
   while(true){
      hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
      if(hSnap == INVALID_HANDLE_VALUE){ // Invalid handle returned by CreateToolhelp32Snapshot
         CloseHandle(hSnap); // Lets close it, even if its invalid
         return 1; // Failed, no need to continue
      }
      count = 0;
      system("cls");
      Process32First(hSnap, &lppe); // Find the first process
      do{
         std::cout << "Process found: " << lppe.szExeFile << "   PID: " << lppe.th32ProcessID << std::endl; // Print the process name and id
         count++;
         curname = lppe.szExeFile;
         if(strcmp(procname, curname) == 0){
            return 0;
         }
      }while(Process32Next(hSnap, &lppe)); // Keep checking if there is more processes, and if there is, copy it to lppe
      std::cout << "Currently running processes: " << count << std::endl;
      CloseHandle(hSnap);
      std::cin.sync();
      std::cin.ignore();
   }
   return 0;
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites