問題描述
我嘗試獲取所有已啟動窗口的可執行文件名稱,但我的問題是:
I try to get the name of executable name of all of my launched windows and my problem is that:
我用的方法
UINT GetWindowModuleFileName(
HWND hwnd,
LPTSTR lpszFileName,
UINT cchFileNameMax);
我不明白為什么它不起作用.
And I don't understand why it doesn't work.
關于窗口的數據是:
-HWND AND PROCESSID
Data which I have about a window are:
-HWND AND PROCESSID
錯誤是:例如:
HWND: 00170628
ProcessId: 2336
WindowTitle: C: est.cpp - Notepad++
GetWindowModuleFileName(): C: est.exe
HWND: 00172138
ProcessId: 2543
WindowTitle: Firefox
GetWindowModuleFileName(): C: est.exe
HWND: 00120358
ProcessId: 2436
WindowTitle: Mozilla Thunderbird
GetWindowModuleFileName(): C: est.exe
注意:test.exe 是我的可執行文件的名稱,但它不是 Notepad++ 的完整路徑...它也適用于 Mozilla Thunderbird...我不明白為什么
Note: test.exe is the name of my executable file, but it is not the fullpath of Notepad++... and it make this for Mozilla Thunderbird too... I don't understand why
我使用這樣的函數:
char filenameBuffer[4000];
if (GetWindowModuleFileName(hWnd, filenameBuffer, 4000) > 0)
{
std::cout << "GetWindowModuleFileName(): " << filenameBuffer << std::endl;
}
感謝您的回復.
推薦答案
GetWindowModuleFileName
函數僅適用于當前進程中的窗口.
The GetWindowModuleFileName
function works for windows in the current process only.
您必須執行以下操作:
- 使用
檢索窗口進程GetWindowThreadProcessId
. - 使用PROCESS_QUERY_INFORMATION和
PROCESS_VM_READ
訪問權限打開進程api/processthreadsapi/nf-processthreadsapi-openprocess" rel="noreferrer">OpenProcess
. - 使用
GetModuleFileNameEx
在進程句柄上.
- Retrieve the window's process with
GetWindowThreadProcessId
. - Open the process with
PROCESS_QUERY_INFORMATION
andPROCESS_VM_READ
access rights usingOpenProcess
. - Use
GetModuleFileNameEx
on the process handle.
如果您確實想獲取注冊窗口的模塊的名稱(而不是進程可執行文件),則可以使用 GetWindowLongPtr
和 GWLP_HINSTANCE
.然后可以將模塊句柄傳遞給前面提到的 GetModuleFileNameEx
.
If you really want to obtain the name of the module with which the window is registered (as opposed to the process executable), you can obtain the module handle with GetWindowLongPtr
with GWLP_HINSTANCE
. The module handle can then be passed to the aforementioned GetModuleFileNameEx
.
TCHAR buffer[MAX_PATH] = {0};
DWORD dwProcId = 0;
GetWindowThreadProcessId(hWnd, &dwProcId);
HANDLE hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ , FALSE, dwProcId);
GetModuleFileName((HMODULE)hProc, buffer, MAX_PATH);
CloseHandle(hProc);
這篇關于如何獲取窗口的可執行文件名稱的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!