Hello,
I am working on a project in c++(code::blocks), to get list of all the running applications in windows(not processes), same as in the task manager's application tab. And I am using the code below
#include <windows.h> //windows.h
#include <stdio.h> //stdio.h
#include <tchar.h> //tchsr.h
#include <psapi.h> //psapi.h
#include <iostream> //iostream
using namespace std;
HWND g_HWND = NULL;
TCHAR lpstring[500];
HWND hwnd;
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM processID)
{
DWORD lpdwProcessId;
g_HWND = NULL;
int nmax;
if(IsWindowVisible(hwnd))
{
GetWindowThreadProcessId(hwnd, &lpdwProcessId);
GetWindowText(hwnd, lpstring, nmax);
if (lpdwProcessId == processID)
{
g_HWND=hwnd;
return FALSE;
}
}
return TRUE;
}
void PrintProcessNameAndID(DWORD processID)
{
TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID);
DWORD pass = 0;
DWORD processIDReturn = -1;
if (NULL != hProcess)
{
HMODULE hMod;
DWORD cbNeeded;
if (EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded))
pass = GetModuleBaseName(hProcess, hMod, szProcessName, sizeof(szProcessName) / sizeof(TCHAR));
}
EnumWindows(EnumWindowsProc, processID);
if ((g_HWND != NULL) && (pass != 0))
{
if(!_tcsstr(_T("Start""Program Manager"),lpstring))
{
_tprintf(TEXT("%s (PID: %u)\n\n"),lpstring,processID);
}
}
CloseHandle(hProcess);
}
int main(void)
{
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
return 1;
cProcesses = cbNeeded / sizeof(DWORD);
for (i = 0; i < cProcesses; i++)
{
if (aProcesses[i] != 0)
{
PrintProcessNameAndID(aProcesses[i]);
}
}
return 0;
}
But this code reads only "caption" and thus does not read applications like "My Computer",Folder name,Norton antivirus and many more apps without caption.
Please help me to fetch the name of these applications too.I am very tired of this for last three months and really want to complete this.
Thank You in anticipation
suryakant