Click here to Skip to main content
15,910,234 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C++
#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
Posted
Updated 1-Oct-16 1:53am
v3

Your enumerating windows, not processes. Your getting the window then getting the process that created it. Of course, that's not all the process because not every process creates a window.

There's an example that does exactly what you're talking about here[^].
 
Share this answer
 
Comments
Suryakant Turing 10-Jan-16 9:00am    
Thanks Dave, but please can u tell me that what to do after I get all the running processes. Because I only need the "running applications(or-> a running application that has a window on my laptop screen)" like in application tab of Windows Task Manager and NOT the running Processes.
Dave Kreskowiak 10-Jan-16 11:45am    
OK, in that case these are processes that are launched by and running as the user. Use OpenProcessToken function (Windows)[^] to the the security token the process was launched under, the use GetTokenInformation function (Windows)[^] with the TokenOwner flag set to the SID of the owner (the user). You can then call LookupAccountSid function (Windows)[^] to get the account of the SID. If the account matches that of the currently logged in user, the user launched that process, what you're calling an application.
Suryakant Turing 12-Jan-16 13:40pm    
Dave look what I have got,
#include<iostream>//iostream
#include<windows.h> //windows.h
#include<stdio.h> //stdio.h
#include<tchar.h> //tchar.h
#include<psapi.h> //psapi.h

using namespace std;

TCHAR lpstring[500];
HWND hwnd;

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);


int main()
{
EnumWindows(EnumWindowsProc, NULL);
return 0;
}


BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{

char class_name[80];
char title[80];

DWORD lpdwProcessId;

int nmax;
if(IsWindowVisible(hwnd))
{

GetWindowText(hwnd, lpstring, nmax);
if(!_tcsstr(_T("Start""Program Manager"),lpstring))
_tprintf(TEXT("%s (PID: %u)\n\n"),lpstring,lParam);

}

return TRUE;
}

I minimized my code and got all the application as in Task manager. Every thing is working properly except:-
1. Windows Media Player(It is traced 4 times)
2. I want to make this program dynamic i.e. whenever I open an application it should be automatically shown on the console without run it again and again.
Tell me.
Thanks
Dave Kreskowiak 12-Jan-16 13:35pm    
I gave you the links to the documentation on these functions for a reason. It was NOT for ME to do your research into these functions and the structures they use for you.
Suryakant Turing 12-Jan-16 13:45pm    
I agree but these can't give me the dynamic control as I want.
In addition to Solution 1:

To enumerate processes, you can use use the functions Process32First, Process32Next
Process32First function (Windows)[^],
Process32Next function (Windows)[^].

See also this CodeProject article for further detail: Enumerating processes : A practical approach[^].

—SA
 
Share this answer
 
Comments
Suryakant Turing 10-Jan-16 8:52am    
Thanks Sergey, but as I already specified in my question that I only want the "current Running applications" as we see in "Application Tab of the Windows Task Manager" and "NOT the Processes".
Sergey Alexandrovich Kryukov 10-Jan-16 12:52pm    
So what? This is what API gives you. Choose what you need. :-)
—SA
Use this code:

C++
HANDLE hProcessSnap;
HANDLE hProcess;
PROCESSENTRY32 pe32;
DWORD dwPriorityClass;

// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900