How to get the Executable name of a window(如何获取窗口的可执行文件名称)
问题描述
我尝试获取所有已启动窗口的可执行文件名称,但我的问题是:
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);
这篇关于如何获取窗口的可执行文件名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何获取窗口的可执行文件名称


基础教程推荐
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01