枚举窗口不工作
EnumWindows not working
我正在创建一个dll文件
我代码:BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);
void test() {
EnumWindows(EnumWindowsProc, NULL);
}
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
char class_name[80];
char title[80];
GetClassName(hwnd, (LPWSTR) class_name, sizeof(class_name));
GetWindowText(hwnd, (LPWSTR) title,sizeof(title));
std::string titlas(title);
std::string classas(class_name);
Loggerc(titlas);
Loggerc("Gooing");
return TRUE;
}
那么我就调用test()
。
日志中titlas
为空,code停止。
当我用CodeBlock在Win32应用程序中尝试这段代码时,一切正常,所有的标题都显示出来了。但是在dll中,它不起作用。
问题在哪里?
char class_name[80]; char title[80]; GetClassName(hwnd, (LPWSTR) class_name, sizeof(class_name)); GetWindowText(hwnd, (LPWSTR) title,sizeof(title)); std::string titlas(title); std::string classas(class_name);
考虑到自VS2005以来默认已在Unicode模式(而不是ANSI/MBCS)中构建,并且您拥有那些(丑陋的c风格)(LPWSTR)
类型转换,我假设您在将基于字符的字符串缓冲区传递给GetClassName()和GetWindowText()等api时遇到编译时错误,并且您试图用类型转换修复这些错误。
这是错误的。编译器实际上是在帮助你处理这些错误,所以请遵循它的建议,而不是将编译器错误抛掉。
假设Unicode构建,您可能希望使用 wchar_t
和 std::wstring
代替char
和std::string
,并使用 _countof()
代替sizeof()
来获取wchar_t
s中的缓冲区大小,而不是以字节(char
s)。
例如:
// Note: wchar_t used instead of char
wchar_t class_name[80];
wchar_t title[80];
// Note: no need to cast to LPWSTR (i.e. wchar_t*)
GetClassName(hwnd, class_name, _countof(class_name));
GetWindowText(hwnd, title, _countof(title));
// Note: std::wstring used instead of std::string
std::wstring titlas(title);
std::wstring classas(class_name);
如果你的代码的其他部分使用std::string
,你可能想从存储在std::wstring
(由Windows api返回)的utf -16编码的文本转换为utf -8编码的文本,并将其存储在std::string
实例。
相关文章:
- 如果不在窗口 10 中声明名为 openCV 的 openCV namedWindow,QT 图像显示将无法正常工作
- Visual Studio 2017 停止工作,并在打开后显示许多控制台窗口
- 自 Windows 10 20H1 以来,具有单独线程的多个窗口停止工作
- 我想使用此代码单击某个特定窗口,但它无法正常工作
- 渲染窗口无法跨多个函数工作
- QtCUrl post不再工作(Linux nok...窗口确定)
- 如何将C++ dll 在 C# 窗口窗体应用程序下的工作线程中运行
- 如何在工作区中使用 HTML/CSS UI 在C++中制作本机 UI 窗口
- 在工作线程中使用 QT 主窗口
- xcb_get_image_reply另一个工作区/桌面上的窗口失败
- Qt拖放在窗口中工作.我可以做到我只能放入Qlabel而不是整个窗口吗?
- 如何将子窗口(MFC 应用程序)的工作区保存到图像
- 从Windows注册表中保存的工作区检索窗口放置
- 资源窗口在其他系统上无法正常工作 (Visual Studio 2012)
- 将工作线程信号与主窗口插槽 (Qt5) 连接时出错
- CMD窗口停止工作,文本文件使用fstream读取错误
- 如何允许工作线程更新 X11 窗口
- Qt:为什么connect()只在主窗口类中工作
- C++ 如果某处有空间,则窗口系统 ( "path" ) 无法正常工作
- WM_PAINT在最小化并恢复窗口后停止工作