Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

processes management 1

Status
Not open for further replies.

Raywall

Programmer
Oct 1, 2003
51
RO
I have searched the forum to see what has been told in here about processes but ...
The thing is that one of my collegs needs to implement something like Task Manager using Visual C(or Borland) and I must help him(kind of coding it for him :) ).I saw lots of functions in help wich perform actions on processes and there should be no problem but I can't get the list of running processes since there is no such function.
I believe I should use some dll,like "psapi"
but I don't now how to use it(is this a library or a dll???).Is there any way you could show me a few code lines who list the active procesess?
Regards...
 
Please take a look in MSDN at the CreateToolhelp32Snapshot API.

It can create a snapshot of the specified (or all) processes in the system, as well as the heaps, modules, and threads used by these processes.
Afterwards you can parse the list of processes using Process32First and Process32Next. You can parse the list of modules loaded by a process using Module32First and Module32Next. For threads, use Thread32First and Thread32Next.
To parse a process' heap, use Heap32First, Heap32ListFirst, Heap32ListNext, Heap32Next.

Hope this was helpful enough.
 
Below is tha code which list all processes in a console aplication.
You must use psapi.h, psapi.lib and psapi.dll.
In order to do that you have to copy the psapi.h and psapi.lib in your project directory, and you have to put the psapi.dll in the same directory with your executable file(i.e. Debug or Releas for now)

You can find these files in the sample directory from your MSDN colection

Good luck,

#include "stdafx.h"
#include "windows.h"
#include "psapi.h" // for the compiler

#pragma comment (lib, "psapi") //for the linker

void EnumerateProceses();
void PrintProcessName(DWORD dwProcessName);

int main(int argc, char* argv[])
{
EnumerateProceses();
printf("Hello World!\n");
return 0;
}

void EnumerateProceses()
{
DWORD aProcesses[1024], // Array of process IDs
cbNeeded, // Byte count returned...
cProcesses; // The number of 'em obtained
unsigned int i; // Get a list of all current PIDs running
if(!EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded))
return; // Get the count of PIDs in the array
cProcesses = cbNeeded / sizeof(DWORD); // Print the name and process identifier for each process
for(i = 0; i < cProcesses; i++)
PrintProcessName(aProcesses);
}

void PrintProcessName(DWORD processID)
{
char szProcessName[MAX_PATH] = &quot;unknown&quot;;
char szTemp[80]; // Get a handle to the passed-in process ID
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, processID ); // Get the process name
if(hProcess)
{
HMODULE hMod;
DWORD cbNeeded;
if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded))
{
GetModuleBaseName(hProcess, hMod, szProcessName,
sizeof(szProcessName));
}
} // Print the process name and ID
wsprintf(szTemp, &quot;%s (PID: %u)\n&quot;, szProcessName, processID );
printf(szTemp);

}

s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
psapi is a lib which link a dll. So, to use you should link with psapi.lib, but distribute application with psapi.dll. psapi.h you will copy in some include directory. psapi.h and psapi.lib are required only at build time.

Ion Filipski
1c.bmp
 
Yes I found the example and now I should start working :)
Thanks all for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top