ddacot
ddacot

Reputation: 1210

Converting Int to LPCWSTR

I Have a function in which i get the display resolution. I came up with an idea but the result is just some squares.

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;
}

and i'm calling this function from a MessageBox

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

and here is the output:

My Question is, what's causing this output? What are the other ways to accomplish that? (converting int to LPWCSTR)? Thanks.

Upvotes: 0

Views: 2595

Answers (1)

Matthias247
Matthias247

Reputation: 10416

Your problem is most likely that you are returning a pointer (LPCWSTR) which is not valid outside of the function, because the object holding the data (ress) was already destructed. So you should change your function to return std::wstring and call .c_str() where you need it (at creating the message box):

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

Upvotes: 6

Related Questions