gpt4 book ai didi

c++ - 在 vc++ 中获取事件进程名

转载 作者:行者123 更新时间:2023-11-30 02:00:49 25 4
gpt4 key购买 nike

我正在用 vc++ 开发一个后台应用程序

如何获取当前应用程序的进程名称,例如“Iexplore”表示使用 Internet Explorer,“Skype”表示使用“Skype - 用户名”的窗口,“Explorer”表示使用 Windows 资源管理器?

我引用了此链接,但收到空错误:http://www.codeproject.com/Articles/14843/Finding-module-name-from-the-window-handle

最佳答案

这可以使用以下代码完成:

bool GetActiveProcessName(TCHAR *buffer, DWORD cchLen)
{
HWND fg = GetForegroundWindow();
if (fg)
{
DWORD pid;
GetWindowThreadProcessId(fg, &pid);
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
if (hProcess)
{
BOOL ret = QueryFullProcessImageName(hProcess, 0, buffer, &cchLen);
//here you can trim process name if necessary
CloseHandle(hProcess);
return (ret != FALSE);
}
}
return false;
}

然后

TCHAR buffer[MAX_PATH];
if(GetActiveProcessName(buffer, MAX_PATH))
{
_tprintf(_T("Active process: %s\n"), buffer);
}
else
{
_tprintf(_T("Cannot obtain active process name.\n"));
}

请注意,尽管 QueryFullProcessImageName 函数仅在 Windows Vista 之后可用,但在早期的系统上您可以使用 GetProcessImageFileName(它类似,但需要与 psapi.dll 链接并返回设备路径而不是通常的 win32 路径)

关于c++ - 在 vc++ 中获取事件进程名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14745320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com