如何从Windows任务管理器中获取所有正在运行的应用程序 [英] how to grab all the running applications from windows task manager

查看:248
本文介绍了如何从Windows任务管理器中获取所有正在运行的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我正在使用c ++(code :: blocks)开发一个项目,以获取Windows(非进程)中所有正在运行的应用程序的列表,与任务管理器的应用程序相同标签。我正在使用下面的代码

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;
}



但此代码只读取标题,因此无法读取我的电脑,文件夹名称,诺顿防病毒等应用程序以及更多应用程序标题。



请帮我提取这些应用程序的名称。过去三个月我对此非常厌倦,真的想完成这个。



感谢您的预期

suryakant


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

推荐答案

您的枚举窗口,而不是进程。您获取窗口然后获取创建它的过程。当然,这并不是所有的过程,因为不是每个过程都会创建一个窗口。



有一个例子可以完全解释你所说的此处 [ ^ ]。
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[^].


除了解决方案1:



要枚举进程,您可以使用函数 Process32First,Process32Next

Process32First功能( Windows) [ ^ ],

Process32Next功能(Windows) [ ^ ]。



有关更多详细信息,请参阅此CodeProject文章:枚举过程:一种实用的方法 [ ^ ]。



-SA
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


使用此代码:



Use this code:

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

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


这篇关于如何从Windows任务管理器中获取所有正在运行的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆