Skip to content

Instantly share code, notes, and snippets.

@andr1972
Created January 19, 2017 07:34
Show Gist options
  • Save andr1972/8e5885773c315cd85ac75eb2a3f4bada to your computer and use it in GitHub Desktop.
Save andr1972/8e5885773c315cd85ac75eb2a3f4bada to your computer and use it in GitHub Desktop.
#include <iostream>
#include <stdio.h>
#include <Windows.h>
#include <Psapi.h>
#include <TlHelp32.h>
BOOL ListProcessModules(DWORD dwPID)
{
HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
MODULEENTRY32 me32;
// Take a snapshot of all modules in the specified process.
hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID);
if (hModuleSnap == INVALID_HANDLE_VALUE)
{
std::cerr << "CreateToolhelp32Snapshot (of modules)";
return(FALSE);
}
// Set the size of the structure before using it.
me32.dwSize = sizeof(MODULEENTRY32);
// Retrieve information about the first module,
// and exit if unsuccessful
if (!Module32First(hModuleSnap, &me32))
{
std::cerr << "Module32First"; // show cause of failure
CloseHandle(hModuleSnap); // clean the snapshot object
return(FALSE);
}
// Now walk the module list of the process,
// and display information about each module
do
{
printf("\n\n MODULE NAME: %ls", me32.szModule);
printf("\n Executable = %ls", me32.szExePath);
printf("\n Process ID = 0x%08X", me32.th32ProcessID);
printf("\n Ref count (g) = 0x%04X", me32.GlblcntUsage);
printf("\n Ref count (p) = 0x%04X", me32.ProccntUsage);
printf("\n Base address = 0x%08X", (DWORD)me32.modBaseAddr);
printf("\n Base size = %d", me32.modBaseSize);
} while (Module32Next(hModuleSnap, &me32));
CloseHandle(hModuleSnap);
return(TRUE);
}
int main()
{
int num = 64;
DWORD *ProcessIds = NULL;
DWORD cb, cbNeeded;
do { //start from room to 128 processes
num *= 2;
delete ProcessIds;
ProcessIds = new DWORD[num];
cb = num * sizeof(DWORD);
cbNeeded = 0;
EnumProcesses(ProcessIds, cb, &cbNeeded);
} while (cbNeeded>=cb);
for (int i = 0; i < cbNeeded / sizeof(DWORD); i++)
{
/*HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,false, ProcessIds[i]);
if (hProcess != 0)
{
CloseHandle(hProcess);
}*/
ListProcessModules(ProcessIds[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment