Convert const wchar_t* to LPWSTR

Donald C. Spencer

I'm trying to convert a const wchar_t* to LPWSTR but I'm getting the error E0513.

I'm using Visual Studio with C++17.

Here is my code:

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    LPWSTR* argv;
    int argCount;

    argv = CommandLineToArgvW(GetCommandLineW(), &argCount);

    if (argv[1] == nullptr) argv[1] = L"2048"; <-- conversion error
}

How to fix this?

Remy Lebeau

To answer your question:

You can use const_cast:

argv[1] = const_cast<LPWSTR>(L"2048");

Or a local wchar_t[] array:

wchar_t arg[] = L"2048";
argv[1] = arg;

However, CommandLineToArgvW() will never return any array elements set to nullptr to begin with. All array elements are null-terminated string pointers, so empty parameters would have to be specified as quoted strings ("") on the command-line in order to be parsed, and as such will be returned as 0-length string pointers in the array. So, you would need to check for that condition instead, eg:

if (argCount > 1 && *(argv[1]) == L'\0')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert const wchar_t* into a WCHAR*

Convert const char* to const wchar_t*

Convert LOWORD(wParam) to const wchar_t*

cannot convert 'const char*' to 'LPCWSTR {aka const wchar_t*}'

How to convert int variable and append to const wchar_t*

cannot convert argument 1 from 'ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>' to 'const char *'

Is " const wchar_t* " a pointer?

Cannot convert parameter 1 from 'const wchar_t *' to 'LPCTSTR' in MFC / C++ project

"cannot convert 'wchar_t*' to 'LPCSTR' {aka 'const char*'}gcc" when trying to use WriteConsoleOutputCharacter()

win32 atoi issue which can't convert argument 1 from 'wchar_t [5]' to 'const char *

Make a condition on const wchar_t*

GETTING THE ERROR IN VISUAL STUDIO: error C2664: '_chmod' : cannot convert parameter 1 from 'wchar_t [260]' to 'const char *'

Convert Swift String to wchar_t

Cannot convert 'wchar_t' to 'LPCSTR'

How to convert char to wchar_t*?

How to convert std::string to wchar_t*

Convert unicode string into wchar_t

How convert LPCWSTR to wchar_t*?

String parameter const char* and const wchar_t*

C++ Argument of type "const wchar_t" * incompatible with parameter of type "wchar_t"

How to use regex_iterator with const char/wchar_t*?

Unordered_map find const wchar_t*

Casting std::wstring to const wchar_t x[]

How to transform const wchar_t type to LPTSTR (C++)

Poco::Path compiles with const wchar_t* but behaves unexpectedly

How to make concatenations at const wchar_t* parameters?

String conversion from System::String ^ to const wchar_t *

Argument of type "const wchar_t*" is incompatible with parameter of type "LPTSTR"

Cannot convert CHAR to LPWSTR