Code:
#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>
BOOL ListAllModules(DWORD dwProcessID);
int main(int argc, char **argv )
{
DWORD dwPriorityClass;
HANDLE hProcess;
PROCESSENTRY32 ProcessStruct;
ProcessStruct.dwSize = sizeof(PROCESSENTRY32);
//Making a snapshot and checking for error
HANDLE hProcessSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0);
if(hProcessSnapshot == INVALID_HANDLE_VALUE)
{
// cout << "error1 at making snapshot";
// Something went wrong at making a snapshot
// todo: Making some code if error
return 1;
}
// Set the size of the structure before using it.
ProcessStruct.dwSize = sizeof( PROCESSENTRY32 );
//Process32First(hProcessSnapshot, &ProcessStruct);
if(Process32First(hProcessSnapshot, &ProcessStruct) == false)
{
// cout << "error2 at making snapshot";
// Error,
// Todo: error handling
return 1;
}
// Now walk the snapshot of processes
do
{
if(strcmp(ProcessStruct.szExeFile,argv[1]) != 0)
continue;
//while(true)
//{
// if(Process32Next(hProcessSnapshot, &ProcessStruct) == false)
// {
// cout << "error3 while looking for handle";
// Error handling
// todo!
// return 0;
// }
// if(strcmp(ProcessStruct.szExeFile, "filename.exe") == 0)
// {
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessStruct.th32ProcessID);
if( hProcess == NULL )
printf( "OpenProcess failed\n" );
else
{
dwPriorityClass = GetPriorityClass( hProcess );
if( !dwPriorityClass )
{
printf( "GetPriorityClass failed\n" );
CloseHandle( hProcess );
}
else
printf( "Priority Class = %d\n", dwPriorityClass );
printf( "process ID = 0x%08X\n", ProcessStruct.th32ProcessID );
printf( "thread count = %d\n", ProcessStruct.cntThreads );
printf( "parent process ID = 0x%08X\n", ProcessStruct.th32ParentProcessID );
printf( "Priority Base = %d\n", ProcessStruct.pcPriClassBase );
}
ListAllModules( ProcessStruct.th32ProcessID );
} while( Process32Next( hProcessSnapshot, &ProcessStruct ) );
CloseHandle( hProcessSnapshot );
// break;
return 0;
}
BOOL ListAllModules(DWORD dwProcessID)
{
HANDLE hModule = INVALID_HANDLE_VALUE;
MODULEENTRY32 ModuleEntry;
// Take a snapshot of all modules in the specified process.
hModule = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwProcessID );
if( hModule == INVALID_HANDLE_VALUE )
{
printf( "CreateToolhelp32Snapshot of modules failed\n" );
return( FALSE );
}
ModuleEntry.dwSize = sizeof( MODULEENTRY32 );
if( !Module32First( hModule, &ModuleEntry ) )
{
printf( "Module32First failed\n" );
CloseHandle( hModule );
return( FALSE );
}
// Walk the list of the process,
do
{
printf( "\n\nName: %s\n", ModuleEntry.szModule );
printf( "executable = %s\n", ModuleEntry.szExePath );
printf( "process ID = 0x%08X\n", ModuleEntry.th32ProcessID );
printf( "base address = 0x%08X\n", (DWORD) ModuleEntry.modBaseAddr );
printf( "base size = %d\n", ModuleEntry.modBaseSize );
} while( Module32Next( hModule, &ModuleEntry ) );
CloseHandle( hModule );
return( TRUE );
}