我怎样才能得到具有可见窗口的任何进程的名称 – WinAPI?

我试图得到有可视窗口的进程的名称。 例如,如果我打开了Chrome浏览器,我想获取string“chrome.exe”,但是我只使用下面的代码获取init值“unknown”。

我读过它可能是一个访问权限的问题,你能build议我如何改变他们,以获得进程的名称?

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

对于某些stream程来说,它可以正常工作,但正如我所说的,它并不适用于像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 modulee!" << endl; char szModFileName[MAX_PATH] = ""; // never use this cause it won't get you what you want // GetmoduleeFileNameEx(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; } 
  • 不要使用GetmoduleeFileNameEx而是使用GetProcessImageFileName

这个问题如何通过ID获取进程名/路径 – 这里已经有很多时间了。

如果您只需要名称(但不是完整路径) – 您可以使用CreateToolhelp32Snapshot / Process32First / Process32Next比较PROCESSENTRY32.th32ProcessID与您的idProc并使用PROCESSENTRY32.szExeFile

备用和更有效的方式使用ZWQuerySystemInformation与SystemProcessInformation info class.com备用SYSTEM_PROCESS_INFORMATION.UniqueProcessId与您的idProc和使用idProc 。 真正的第一种方法是通过这种方法壳。

如果你不仅需要名字,而且需要完整的路径:

如果你有SE_DEBUG_PRIVILEGE – 你需要启用它,使用PROCESS_QUERY_LIMITED_INFORMATION (vista +)或PROCESS_QUERY_INFORMATION (xp / 2003) PROCESS_QUERY_INFORMATION并使用带有ProcessImageFileName (NT形式的返回路径)的ZwQueryInformationProcess(,ProcessImageFileName,)GetProcessImageFileName内部调用ZwQueryInformationProcess(,ProcessImageFileName,)

或者从vista开始 – 你可以使用ProcessImageFileNameWin32 (return win32-path)或者QueryFullProcessImageName

也从vista开始 – 最有效的方式查询过程全路径(以NT形式) – 使用带有SystemProcessIdInformation信息类的ZwQuerySystemInformation 。 这种方式不需要任何特权和打开过程