![](/img/trans.png)
[英]How can I get the process name of the current active window in windows with winapi?
[英]How can I get the name of any process having a visible window - WinAPI?
我正在尝试获取具有可见窗口的进程的名称。 例如,如果我打开了Chrome,我想获取字符串“ chrome.exe”,但仅使用以下代码获取初始化值“ unknown”。
我读到它可能是一个访问权限问题,您能建议我如何更改它们以获取进程名称吗?
DWORD idProc = 0; //pointer to the process which created the window
DWORD idThread = GetWindowThreadProcessId(Wnd->get_handle(), &idProc);
Wnd->set_pid(idThread); //Wnd is an object of a class i created, to collect processes info
// Get a handle to the process.
TCHAR szProcessName[DEFAULT_BUFLEN] = TEXT("<unknown>");
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, idProc);
if (hProcess!=NULL) {
HMODULE hMod;
DWORD cbNeeded;
if (EnumProcessModules(hProcess, &hMod, sizeof(hMod),
&cbNeeded))
{
GetModuleBaseName(hProcess, hMod, szProcessName,
sizeof(szProcessName) / sizeof(TCHAR));
}
}
Wnd->set_processname(szProcessName);
CloseHandle(hProcess);
正如我所说的,它在某些进程中工作正常,但不适用于Chrome等其他许多进程。
编辑:我忘了说,我只是过滤了可见窗口,所以假设句柄是我需要的。
改用GetProcessImageNamr
API:
#include <iostream>
using namespace std;
#include <windows.h>
#include <Psapi.h>
#pragma comment(lib, "Psapi.lib")
int main()
{
DWORD dwProcessId;
DWORD dwThreadId ;
while(1)
{
Sleep(2000);
HWND hForg = GetForegroundWindow(); // to get the foreground windows' handle window
dwThreadId = GetWindowThreadProcessId(hForg, &dwProcessId); // getting the window's process ID
DWORD dwDesiredAccess =
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ;
bool bInheritHandle = false;
HANDLE hProcess = OpenProcess(dwDesiredAccess,
bInheritHandle, dwProcessId);
if(INVALID_HANDLE_VALUE == hProcess)
cout << "Failed to open process!" << endl;
HINSTANCE hMod = (HINSTANCE)GetWindowLongPtr(hForg, GWLP_HINSTANCE);
if(!hMod)
cout << "Null Module!" << endl;
char szModFileName[MAX_PATH] = "";
// never use this cause it won't get you what you want
// GetModuleFileNameEx(hProcess, hMod, szModFileName, MAX_PATH);
// use this
GetProcessImageFileName(hProcess, szModFileName, MAX_PATH);
CloseHandle(hProcess);
char szWindowName[MAX_PATH] = "";
GetWindowText(hForg, szWindowName, MAX_PATH);
cout << "Window Name: " << szWindowName << endl;
cout << "Created by: " << szModFileName << endl << endl;
}
cout << endl << endl << endl;
return 0;
}
GetModuleFileNameEx
而是使用GetProcessImageFileName
这个问题如何通过ID获取进程名称/路径 -在这里已经有很多次了。
如果仅需要名称(而不是完整路径),则可以使用CreateToolhelp32Snapshot
/ Process32First
/ Process32Next
将PROCESSENTRY32.th32ProcessID
与idProc
进行比较,并使用PROCESSENTRY32.szExeFile
。
替代和更有效的方式使用ZwQuerySystemInformation
与SystemProcessInformation
信息class.compare SYSTEM_PROCESS_INFORMATION.UniqueProcessId
与idProc
并使用SYSTEM_PROCESS_INFORMATION.ImageName
。 真正的第一种方法是使用Shell替代此方法。
如果您不仅需要名称,还需要完整路径:
如果您有SE_DEBUG_PRIVILEGE
您需要启用它,请使用PROCESS_QUERY_LIMITED_INFORMATION
(vista +)或PROCESS_QUERY_INFORMATION
(xp / 2003)打开进程,并将ZwQueryInformationProcess
与ProcessImageFileName
(以NT形式返回路径)或GetProcessImageFileName
(在内部调用ZwQueryInformationProcess(,ProcessImageFileName,)
)一起使用
或从Vista开始-您可以使用ProcessImageFileNameWin32
(返回Win32路径)或QueryFullProcessImageName
( 同样仅以此方式记录的薄壳 )
也从Vista开始-最有效的查询过程全路径(以NT形式)-将ZwQuerySystemInformation
与SystemProcessIdInformation
信息类一起使用。 这种方式不需要任何特权和开放过程
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.