获取所有正在运行的进程及其标题
C++ Get all running processes and their titles
我目前正在做一个c++项目,我想做的是把所有正在运行的可用进程和它们的标题,但目前我只能跟踪它们的处理程序并计算它们,而不能取它们的标题。
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam);
int i;
string hwndTitle;
LPTSTR WindowTitle;
int length = 0;
int getHWND()
{
std::cout << "Finding all open windowsn";
if(EnumWindows(EnumWindowsProc, 0)) {
std::cout << i << " windows are openn"<<hwndTitle<<"n"<<"Call was successful...n" << std::endl;
std::cin.get();
} else {
std::cout << "Call was unsuccessful...n" << std::endl;
std::cin.get();
}
return 0;
}
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) {
i++;
HWND WindowHandle;
WindowHandle = GetForegroundWindow();
length = GetWindowTextLength (hWnd);
hwndTitle = GetWindowText(hWnd , WindowTitle , length);
return true;
}
您的代码正在滥用GetWindowText()
。您没有为它分配任何内存来填充,并且它不返回std::string
甚至char*
/TCHAR*
,就像您的代码所假设的那样。即使您正确使用它,您也只输出分配给hwndTitle
的最后一个窗口标题,而不是将多个标题连接在一起。
试试这样写:
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam);
int getHWND()
{
std::cout << "Finding all open windows" << std::endl;
int i = 0;
if (EnumWindows(EnumWindowsProc, (LPARAM) &i)) {
std::cout << i << " windows are openn" << "Call was successful...n" << std::endl;
} else {
std::cout << "Call was unsuccessful...n" << std::endl;
}
std::cin.get();
return 0;
}
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
int *i = (int*) lParam;
++(*i);
int length = GetWindowTextLengthA(hWnd);
std::vector<char> WindowTitle(length+1);
length = GetWindowTextA(hWnd, &WindowTitle[0], length);
if (length > 0) {
WindowTitle[length] = 0;
std::cout << &WindowTitle[0] << std::endl;
} else {
std::cout << "(none)" << std::endl;
}
return TRUE;
}
另外:
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam);
int getHWND()
{
std::cout << "Finding all open windows" << std::endl;
std::vector<std::string> WindowTitles;
if (EnumWindows(EnumWindowsProc, (LPARAM) &WindowTitles)) {
for (std::vector<std::string>::iterator i = WindowTitles.begin(); i != WindowTitles.end(); ++i) {
if (!i->empty()) {
std::cout << *i << std::endl;
} else {
std::cout << "(none)" << std::endl;
}
}
std::cout << WindowTitles.size() << " windows are openn" << "Call was successful...n" << std::endl;
} else {
std::cout << "Call was unsuccessful...n" << std::endl;
}
std::cin.get();
return 0;
}
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
std::vector<std::string> *WindowTitles = (std::vector<std::string>*) lParam;
int length = GetWindowTextLengthA(hWnd);
std::string WindowTitle(length+1, ' ');
length = GetWindowTextA(hWnd, &WindowTitle[0], length);
WindowTitle.resize(length);
WindowTitles->push_back(WindowTitle);
return TRUE;
}
相关文章:
- boost::进程间消息队列引发错误
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 是否可以通过C++扩展强制多个python进程共享同一内存
- IPC使用多个管道和分支进程来运行Python程序
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- 异常属于C++中的线程还是进程
- WMI检测进程创建事件-c++
- c++多进程编写一个唯一的文件
- 如何在C++中将函数发送到另一个进程
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 对于C++,方括号中的标题必须总是在引号中的标题之上吗
- 库标题在标题中不可见,但在 cmake build 下.cpp文件中完全可见.为什么?
- 终止 QProcess 不会终止子进程
- 为什么我不应该把所有东西都放在标题中?
- 将返回值从 exe 传递到 bat,并将其传递给 C# 中的进程
- C++:通过进程id、进程句柄和标题名称从进程中获取唯一窗口的窗口句柄的最佳方法
- 如何从其进程路径中获取窗口标题
- 获取所有正在运行的进程及其标题
- 如何通过窗口标题找到进程,并在打开时关闭它
- C + + Kill进程窗口标题