c++ - 将 Int 转换为 LPCWSTR

标签 c++ winapi

我有一个函数可以获取显示分辨率。我想出了一个主意,但结果只是一些正方形。

LPCWSTR GetDispRes(HWND hWnd)
{
    HMONITOR monitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
    MONITORINFO info;
    info.cbSize = sizeof(MONITORINFO);
    GetMonitorInfo(monitor, &info);
    int arr[2];
    arr[0] = info.rcMonitor.right - info.rcMonitor.left;
    arr[1] = info.rcMonitor.bottom - info.rcMonitor.top;
    LPCWSTR a;
    std::wstring s = std::to_wstring(arr[0]);
    std::wstring d = std::to_wstring(arr[1]);

    std::wstring ress = s + d;
    a = (LPCWSTR)ress.c_str();

    return a;
}

我从 MessageBox 调用这个函数

MessageBox(NULL, GetDispRes(hWnd) , TEXT("TEST"), NULL);

这是输出:

我的问题是,是什么导致了这个输出?还有什么其他方法可以做到这一点? (将 int 转换为 LPWCSTR)?谢谢。

最佳答案

您的问题很可能是您返回的指针 (LPCWSTR) 在函数外部无效,因为保存数据 (ress) 的对象已被破坏。 因此,您应该更改函数以返回 std::wstring 并在需要的地方调用 .c_str()(在创建消息框时):

std::wstring res = GetDispRes(hWnd);
MessageBox(NULL, res.c_str() , TEXT("TEST"), NULL);

关于c++ - 将 Int 转换为 LPCWSTR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19322361/

相关文章:

c - HINSTANCE 跨线程有效吗?

c++ - 使用自定义凭据提供程序的远程桌面连接

C++ 创建 LPWSTR 的简单数组

c++ - 使用生成器和分布扩展 c++11 random 的教程或示例代码

c++ - C/C++ 中的 (nil) 指针

winapi - 哪些原因会导致 ShellExecute 失败?

命令行的 C++ Winapi CreateProcess 问题

c++ - Big Oh 是唯一用于衡量 STL 复杂性的符号吗

c++ - 使用 boost 的字符串和函数映射给出编译错误

c++ - 如何更改 Boost.Serialization 中的默认枚举序列化