在 C++ 中获取所有打开窗口的列表并存储它们 [英] Getting a list of all open windows in c++ and storing them
问题描述
我目前正在尝试获取所有打开的窗口的列表并将它们存储在一个向量中.我一直在研究代码,以至于解决方案可能非常简单,但如果没有全局变量(我想避免使用),我似乎无法完成它.
I'm currently trying to get a list of all opened windows and storing them inside a vector. I've been looking at the code so much that the solution could be very easy but I don't seem to get it done without a global variable (which I want to avoid).
代码如下:
#include "stdafx.h"
#include "json.h"
#include <algorithm>
using namespace std;
vector<string> vec;
BOOL CALLBACK speichereFenster(HWND hwnd, LPARAM substring){
const DWORD TITLE_SIZE = 1024;
TCHAR windowTitle[TITLE_SIZE];
GetWindowText(hwnd, windowTitle, TITLE_SIZE);
int length = ::GetWindowTextLength(hwnd);
wstring temp(&windowTitle[0]);
string title(temp.begin(), temp.end());
if (!IsWindowVisible(hwnd) || length == 0 || title == "Program Manager") {
return TRUE;
}
vec.push_back(title);
return TRUE;
}
int main() {
EnumWindows(speichereFenster, NULL);
cin.get();
return 0;
}
我想将所有标题存储在向量中,但我不知道如何将向量传递给函数...
I want to store all titles in the vector but I don't know how as I can't pass the vector into the function...
谢谢!!!
推荐答案
EnumWindows 记录为:
The second parameter (lParam) to EnumWindows is documented as:
要传递给回调函数的应用程序定义的值.
An application-defined value to be passed to the callback function.
只需将您的容器传递给 API 调用:
Just pass your container to the API call:
int main() {
std::vector<std::wstring> titles;
EnumWindows(speichereFenster, reinterpret_cast<LPARAM>(&titles));
// At this point, titles if fully populated and could be displayed, e.g.:
for ( const auto& title : titles )
std::wcout << L"Title: " << title << std::endl;
cin.get();
return 0;
}
并在您的回调中使用它:
And use it in your callback:
BOOL CALLBACK speichereFenster(HWND hwnd, LPARAM lParam){
const DWORD TITLE_SIZE = 1024;
WCHAR windowTitle[TITLE_SIZE];
GetWindowTextW(hwnd, windowTitle, TITLE_SIZE);
int length = ::GetWindowTextLength(hwnd);
wstring title(&windowTitle[0]);
if (!IsWindowVisible(hwnd) || length == 0 || title == L"Program Manager") {
return TRUE;
}
// Retrieve the pointer passed into this callback, and re-'type' it.
// The only way for a C API to pass arbitrary data is by means of a void*.
std::vector<std::wstring>& titles =
*reinterpret_cast<std::vector<std::wstring>*>(lParam);
titles.push_back(title);
return TRUE;
}
注意事项:
- 呈现的代码使用
std::wstring
代替std::string
.这是必要的,以便可以表示整个字符集. - 正如所写,代码不正确.有(不可见的)代码路径,没有明确定义的含义.Windows API 严格公开为 C 接口.因此,它不理解 C++ 异常.特别是对于回调,永远不要让 C++ 异常跨越未知的堆栈帧是至关重要的.要修复代码,请应用以下更改:
- [仅限 C++11] 标记回调 noexcept.
- 将整个回调包裹在 try-catch 块中,并处理任何例外情况.
- [仅限 C++11] 使用 C++11,您可以通过传递一个 std::exception_ptr,并调用 std::rethrow_exception 在调用站点.
- The code presented uses a
std::wstring
in place ofstd::string
. This is necessary so that the entire character set can be represented. - As written, the code isn't correct. There are (invisible) code paths, that have no well-defined meaning. The Windows API is strictly exposed as a C interface. As such, it doesn't understand C++ exceptions. Particularly with callbacks it is vital to never let C++ exceptions travel across unknown stack frames. To fix the code apply the following changes:
- [C++11 only] Mark the callback noexcept.
- Wrap the entire callback inside a try-catch block, and handle any exceptions appropriately.
- [C++11 only] With C++11 you can pass C++ exceptions across unknown stack frames, by passing a std::exception_ptr, and calling std::rethrow_exception at the call site.
这篇关于在 C++ 中获取所有打开窗口的列表并存储它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!