in
Windows Task Manager --> Processes
there is a list of running exe files.
I am going to write a program that lists these running exe files.
How can I obtain the list?
I use Windows XP :$
& Windows API!:)

Dani AI

Generated

A quick, practical follow-up to the thread: 's PSAPI sample is fine for XP, and it explains how EnumProcesses/GetModuleBaseName work, but is right to warn about static PSAPI linking — the simpler, more portable approach is the Tool Help snapshot (CreateToolhelp32Snapshot + Process32First/Process32Next). Use Tool Help when you want a quick list of running executables from Kernel32 rather than pulling in PSAPI. (learn.microsoft.com)

Example: enumerate with the Tool Help API (prints PID and exe name).

#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>

void ListProcesses()
{
    HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (snap == INVALID_HANDLE_VALUE) return;
    PROCESSENTRY32 pe; pe.dwSize = sizeof(pe);
    if (Process32First(snap, &pe)) {
        do {
            printf("PID=%lu\t%s\n", (unsigned long)pe.th32ProcessID, pe.szExeFile);
        } while (Process32Next(snap, &pe));
    }
    CloseHandle(snap);
}

The PROCESSENTRY32.szExeFile field holds the base executable name; remember to initialize dwSize before calling Process32First. (learn.microsoft.com)

To kill or change priority, open the target process with the correct access and call TerminateProcess or SetPriorityClass:

HANDLE h = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
if (h) { TerminateProcess(h, 1); CloseHandle(h); }

h = OpenProcess(PROCESS_SET_INFORMATION|PROCESS_QUERY_INFORMATION, FALSE, pid);
if (h) { SetPriorityClass(h, HIGH_PRIORITY_CLASS); CloseHandle(h); }

OpenProcess requires the matching access rights; TerminateProcess is forceful and should be used as last resort. (learn.microsoft.com)

Notes and pitfalls: getting a full file path differs by Windows version — on XP use GetModuleFileNameEx (PSAPI); on Vista+ prefer QueryFullProcessImageName. Some system processes (CSRSS, SYSTEM) cannot be opened and higher privileges (SeDebugPrivilege / elevated token) are required to manipulate other users' or protected processes. Always check return values and CloseHandle when finished. (learn.microsoft.com)

Recommended Answers

All 3 Replies

in
Windows Task Manager --> Processes
there is a list of running exe files.
I am going to write a program that lists these running exe files.
How can I obtain the list?
I use Windows XP :$
& Windows API!:)

Something like this? - Just an old program I had laying around. Needs the "psapi.h", "psapi.lib" and "psapi.dll".

int FindPIDs()
{
DWORD ProcessesIDs[1000], cbNeeded, cProcesses; 
unsigned int i;
//The default of <unknown> is given so that if GetModuleBaseName does not return 
//the base name of the module then <unknown> will be printed instead of the base name.
 
TCHAR szProcessName[50] = TEXT("<unknown>");
 

EnumProcesses( ProcessesIDs, sizeof(ProcessesIDs), &cbNeeded ) ;
 
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
 
// This for loop will be enumerating each process.
for ( i = 0; i < cProcesses; i++ )
{
// Get a handle to the process. The process to which the handle will be returned //will depend on the variable i.
    HANDLE hProcess =OpenProcess( PROCESS_QUERY_INFORMATION |PROCESS_VM_READ, FALSE, ProcessesIDs[i]);
 
    // Get the process name.
    if (NULL != hProcess )
        {
        GetModuleBaseName( hProcess, NULL, szProcessName, sizeof(szProcessName)/sizeof(TCHAR) );
        }
 
    // Print the process name and identifier.
    cout << "Processname= " << szProcessName << endl;
    cout << "PID = " << ProcessesIDs[i];
    cout << endl << endl;
    
    //Every handel is to be closed after its use is over.
    CloseHandle( hProcess);
    //End of for loop.
    }
    
    return 0;
}

Usefull in anyway?

Thank you very much! It works :)
can't rep u cos my rep power == 0

And I have another question
how can I kill a process?
or change its priority?

Don't use psapi statically, its not on all OS
See MSDN sample to list processes

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.