I can't figure this one out. I get all the other module information as expected, but I can't get GetWindowText to give me the window title of each process. The code below just spits out "Notification Area" repeatedly. I tried using aProcesses[i] instead and got a bunch of the wrong window titles. I don't get it. I'm also curious how to determine the user running each process. I specifically want to know which ones are SYSTEM processes in order to exclude them from the list.
Code:
int ListProcesses(HWND hProcessList) {
HANDLE hProcess;
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
char szProcessName[500];
char szProcessPath[500];
char szProcessID[20];
char szProcessSize[20];
char szProcessTitle[1024];
PROCESS_MEMORY_COUNTERS szProcessMemory;
SendMessage(hProcessList, LVM_DELETEALLITEMS, 0, 0);
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return 1;
cProcesses = cbNeeded / 4;
for ( i = 0; i < cProcesses; i++ ) {
if( aProcesses[i] != 0 ) hProcess = OpenProcess( PROCESS_QUERY_INFORMATION|PROCESS_VM_READ, FALSE, aProcesses[i] );
if (NULL != hProcess )
{
GetModuleBaseName( hProcess, NULL, szProcessName, 500 );
GetModuleFileNameEx( hProcess, NULL, szProcessPath, 500 );
memset(&szProcessMemory,0,sizeof(PROCESS_MEMORY_COUNTERS));
szProcessMemory.cb = sizeof(PROCESS_MEMORY_COUNTERS);
GetProcessMemoryInfo(hProcess, &szProcessMemory, sizeof(PROCESS_MEMORY_COUNTERS));
sprintf(szProcessSize, "%.2f", (float)szProcessMemory.WorkingSetSize / 1024 / 1024);
szProcessTitle[0] = '\0';
GetWindowText(hProcess, szProcessTitle, sizeof(szProcessTitle));
sprintf(szProcessID, "%u", aProcesses[i]);
ListViewAddRow(hProcessList, 5, szProcessName, szProcessTitle, szProcessID, szProcessSize, szProcessPath);
}
CloseHandle( hProcess );
}
return 0;
}