How to properly convert char to wchar_t

171 views Asked by At

I'm trying to create this message box, but the text on the box is a strange language.

catch (const ExceptionHandling& e)
{
    // initialize vars
    const wchar_t* wideWhat = nullptr;
    const wchar_t* wideType = nullptr;

    size_t size;
    mbstowcs_s(&size, nullptr, 0, e.what(), 0);
    if (size != static_cast<size_t>(-1))
    {

        std::wstring wideString(size + 1, L'\0');
        size_t convertedSize;
        mbstowcs_s(&convertedSize, &wideString[0], size + 1, e.what(), size);

        // save wideWhat
        wideWhat = wideString.c_str();
    }
    size_t size2;
    mbstowcs_s(&size2, nullptr, 0, e.GetType(), 0);
    if (size2 != static_cast<size_t>(-1))
    {
        std::wstring wideString(size2 + 1, L'\0');
        size_t convertedSize;
        mbstowcs_s(&convertedSize, &wideString[0], size2 + 1, e.GetType(), size2);

        // save wideType
        wideType = wideString.c_str();
    }

    // send MessageBox
    MessageBox(nullptr, wideWhat, wideType, MB_OK | MB_ICONEXCLAMATION);
}
catch (const std::exception& e)
{
    size_t size;
    mbstowcs_s(&size, nullptr, 0, e.what(), 0);
    if (size != static_cast<size_t>(-1))
    {
        std::wstring wideString(size + 1, L'\0');
        size_t convertedSize;
        mbstowcs_s(&convertedSize, &wideString[0], size + 1, e.what(), size);

        MessageBox(nullptr, wideString.c_str(), L"Standard Exception", MB_OK | MB_ICONEXCLAMATION);
    }
}
catch (...)
{
    MessageBox(nullptr, L"No details available", L"Unknown Exception", MB_OK | MB_ICONEXCLAMATION);
}
return -1;

This is a code part for to catch exceptions. However the Messagebox require me a LPCWSTR argument, i tried to convert the returning value from e.what() and e.GetType() but the messagebox is broken.

messagebox

I tried converting e.what() and e.GetType() to a LPCWSTR, i expected the messagebox to appear with english language, but it appeared broken. I also expected a smaller code for a conversion.

1

There are 1 answers

0
Igor Levicki On

I know that this answer doesn't really answer your question on why the code doesn't work, but I hope it will still be useful to you.

Code that catches exceptions should not show message boxes — what it should do instead is log the error, and if said error is non-recoverable then it should terminate the program. It should avoid any further complicated operations such as showing dialog boxes, allocating memory, etc.

  1. What if you catch std::bad_alloc exception because your program has ran out of memory, and then you try to allocate buffers to convert and/or format your error message?

  2. What if your code gets an error 87 (ERROR_INVALID_PARAMETER) from some Windows API you called and then throws std::invalid_argument? What will the user do with the exception message when the only way to know which parameter was invalid is to have them all logged before the API call and investigate the log post-mortem?

Message boxes should only show meaningful and actionable error messages to the users. For example "You don't have permission to access C:\Windows\System32\Drivers\etc\hosts file. You must run the program as Administrator" is a meaningful and actionable message, but "Access denied." isn't because it doesn't tell the user what they are denied access to, why, and how to fix it.

Finally, you should really learn how to extract common code into a function and pass parameters to it instead of copy/pasting almost identical code three times like in your example before you delve into advanced concepts such as exception handling, character conversion, and Windows API.