我想获得用户启动的任何应用程序的应用程序名称,但我不知道如何实现,我想在控制台应用程序中执行此操作。
我是否需要挂接到新启动的应用程序以获取其名称或将其从任务管理器中删除?
编辑:平台是Windows
如果要在创建一个进程时设置某种回调,则应该看一下在PsSetCreateProcessNotifyRoutineEx和用户CreateProcessNotifyEx。 PS_CREATE_NOTIFY_INFO-结构包含有关应用程序名称(ImageFileName)甚至路径的信息(有关更多信息,请参见链接)。
要删除回调,只需将PsSetCreateProcessNotifyRoutineEx的第二个参数设置为TRUE。
上行:除了WDK的安装以外,还没有很多实现。
缺点:
您需要Windows驱动程序工具包(WDK)和VisualC ++的副本。 (头文件)您只能安装一定数量的hooks(64)->应该足够使用在驱动程序级别附加的用于文件名的回调。
替代项:
EnumWindow()-使用辅助线程/计时器功能调用。
应用程序的名称作为main()
的第一个值传递到argv[]
:
int main(int argc, char** argv) {
std::cout << "app name is: " << argv[0] << std::endl;
return 0;
}
如果您的目标只是在桌面窗口中显示正在运行的应用程序列表。(因此不包括系统进程)
EnumWindows函数可能是一个好方法。
这里有一些示例代码。
#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <iostream>
using namespace std;
#pragma comment(lib, "user32.lib")
int window_num1=0;
BOOL CALLBACK MyEnumProc(HWND hWnd, LPARAM lParam)
{
TCHAR title[256] = {0,};
if (IsWindowVisible(hWnd) && GetWindowTextLength(hWnd) > 0)
{
window_num1++;
GetWindowText(hWnd, title, _countof(title));
_tprintf(_T("Value is %d, %s\n"), window_num1, title);
}
return TRUE;
}
int main()
{
EnumWindows(MyEnumProc, 0);
getchar();
return 0;
}