c++ - 为什么 GetWindowThreadProcessId 函数不能与命令行一起使用?
问题描述
当我尝试使用 GetWindowThreadProcessId 函数时,它失败并使用 GetLastError 函数并将其转换为字符串,它显示“参数不正确”。它发现窗口很好,我尝试将 HWND 参数更改为打开的 .txt 文件,它似乎工作正常,所以我认为问题出在命令行而不是其他问题。这是我的代码:
HWND win = FindWindowA(NULL, (LPCSTR)"C:\\WINDOWS\\system32\\cmd.exe");
LPSTR str;
str = new CHAR[50];
GetWindowTextA(win, str, 49);
cout << str;
LPDWORD * pid;
pid = new LPDWORD();
DWORD cmdthread = GetWindowThreadProcessId(win, *pid);
cout << GetLastErrorAsString();
它似乎适用于任何非 Windows 命令提示符,但我需要使用命令提示符。有什么解决办法还是我做错了什么?
解决方案
您将无效DWORD*
指针传递给GetWindowThreadProcessId()
. 试试这个:
HWND win = FindWindowA(NULL, "C:\\WINDOWS\\system32\\cmd.exe");
if (win != NULL)
{
// this part is redundant, since you just did a search for
// the HWND by its window text, so you already know what
// the text is...
char str[50] = {};
GetWindowTextA(win, str, 49);
cout << str;
//
DWORD pid = 0;
DWORD tid = GetWindowThreadProcessId(win, &pid);
if (tid != 0)
cout << "Thread ID: " << tid << " Process ID: " << pid;
else
cout << GetLastErrorAsString();
}
推荐阅读
- python - 计算熊猫中模式匹配的总和
- vue.js - NuxtJS SPA 模式(ssr false)仍然为 pages 文件夹中的每个 vue 文件生成 HTML
- java - 日期类型的@JsonFormat 问题的任何解决方案
- bash - 在同一行打印 grep 结果
- spring - 如何将下拉列表值从 html 表单发送到控制器?
- javascript - csrf 同源策略不会阻止读取响应?
- flutter - Flutter/Dart - 安排一个 Mysql 查询每隔几分钟运行一次?
- vue.js - Vue服务器端渲染:beforeCreate钩子中的错误:ReferenceError:未定义文档
- docker - Docker:在 Buildkit 中使用 SSH
- python - 如何使用 Selenium 获取动态 html?