如何在 Windows 上获取C++ 个字符 * 格式的当前窗口标题?
How can I get the current window's title with char * format in C++ on Windows?
我想在控制台和/或文件中写入当前窗口标题,并且我有LPWSTR
到char *
或const char *
的麻烦。我的代码是:
LPWSTR title = new WCHAR();
HWND handle = GetForegroundWindow();
GetWindowText(handle, title, GetWindowTextLength( handle )+1);
/*Problem is here */
char * CSTitle ???<??? title
std::cout << CSTitle;
FILE *file;
file=fopen("file.txt","a+");
fputs(CSTitle,file);
fclose(file);
您只为一个字符分配了足够的内存,而不是整个字符串。当调用GetWindowText
时,它复制的字符多于导致未定义行为的内存。您可以使用std::string
来确保有足够的可用内存,并避免自己管理内存。
#include <string>
HWND handle = GetForegroundWindow();
int bufsize = GetWindowTextLength(handle);
std::basic_string<TCHAR> title(bufsize, 0);
GetWindowText(handle, &title[0], bufsize + 1);
你需要分配足够的内存来存储标题:
HWND handle = GetForegroundWindow();
int bufsize = GetWindowTextLength(handle) + 1;
LPWSTR title = new WCHAR[bufsize];
GetWindowText(handle, title, bufsize);
相关文章:
- 无法在 GLFW 窗口标题中显示'ä'
- 为什么抓取窗口标题的代码会导致应用崩溃?
- 如何正确获取 Unicode 窗口标题
- 如何随机化程序的窗口标题?
- 如何在QT中获取背景应用程序窗口标题
- 如何在 Ubuntu Qt 中获取活动窗口标题
- 最佳地将窗口标题替换为新的 QString
- 构造函数字符串参数未设置窗口标题
- 获取C++中的窗口标题
- C++:为什么此窗口标题被截断
- 窗口标题GLFW中的FPS计数器
- 如何永久删除主窗口标题栏
- 使用WM_GETTEXT获取窗口标题
- 在窗口标题和消息框中显示错误的语言:win32应用程序,opengl
- 在GLFW窗口标题中显示FPS
- 如何从其进程路径中获取窗口标题
- 无法获取窗口标题
- 在Qt中更改主窗口标题
- 如何在 Windows 上获取C++ 个字符 * 格式的当前窗口标题?
- 在c++和Neko目标中更改窗口标题