我想在控制台或文件中写入当前窗口标题,但我对将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);
WCHAR
肯定不够。无论如何,如果你需要窄字符串,为什么不直接使用GetWindowTextA
函数呢? - chrisstd::wstring
(C++11)或std::vector<WCHAR>
而不是简单手动管理的数组。 - chris