Error C2664: MessageBoxW cannot convert argument 2 from 'const char ' to 'LPCWSTR'

Cody S

I keep getting this error message:

State Error C2664 -- int MessageBoxW(HWND,LPCWSTR,LPCWSTR,UINT)': cannot convert argument 2 from 'const char *' to 'LPCWSTR' " 31

This is my code below. I understand it has to do with passing a const type through the what() function in the error class. For some reason it's incompatible. Any ideas?

// on startup execute to determine exceptions
try
{
    // instantiate object app of class AppWindow
    AppWindow app;

    // if(initialize function in class app is executed, and while the app is running, broadcast app)
    if (app.init())
    {
        while (app.isRun())
        {
            app.broadcast();
        }
    }
}

// if the following error is found, execute MessageBox function with following parameters
catch (const std::runtime_error& error)
{
    // parameters(has no owner window so displays independently)
    MessageBox(nullptr,  error.what(), L"An error has occured", MB_OK);
}

return 0;
MikeCAT

std::runtime_error::what() returns const char*, so you should use MessageBoxA(), not MessageBox() nor MessageBoxW().

MessageBoxA(nullptr,  error.what(), "An error has occured", MB_OK);

Also don't forget to remove the L prefix from the string literal.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

C++ --- error C2664: 'int scanf(const char *,...)' : cannot convert argument 1 from 'int' to 'const char *'

error C2440: '=': cannot convert from 'const char *' to 'LPCWSTR'

Error C2664: 'Strategy::Interphase' : cannot convert parameter 2 from 'char *' to 'char *[]'

error C2664: 'errno_t strcpy_s(char *,rsize_t,const char *)' : cannot convert parameter 2 from 'char [512]' to 'rsize_t'

Error C2664 'HRESULT IUnknown::QueryInterface(const IID &,void **)': cannot convert argument 1 from 'const winrt::guid' to 'const IID &'

Cannot convert argument 1 from 'const char [11]' to 'LPCWSTR'

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

C++ c2664 error "cannot convert argument 1 from std::string to _Elem *"

c++ doubly linked list error C2664 'object1::object1(object1 &&)': cannot convert argument 1 from 'int' to 'const object1 &'

Error C2664 'BOOL CryptBinaryToStringW(const BYTE *,DWORD,DWORD,LPWSTR,DWORD *)': cannot convert argument 4 from 'std::unique_ptr' to 'LPWSTR'

error C2664: cannot convert 'IDWriteFactory2 **' to 'IUnknown **'

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

error C2664: 'LoadLibraryA' : cannot convert parameter 1 from 'WCHAR *' to 'LPCSTR'

convert from char to LPCWSTR

C2664 Cannot convert parameter 1 from 'int' to 'hwnd'

Cannot convert from string to template C2664

C/C++ strcmp cannot convert argument 1 from 'char' to 'const char *'

C++: cannot convert argument 2 from 'int' to 'const Vector &'

error: cannot convert const string to const char* for argument 1 to size_t strlen(const char*)

error C2664: 'print_result' : cannot convert parameter 1 from 'int (__cdecl *)(int,int,int)' to 'int'

cannot convert from const char* to const char *&

Cannot convert 'const char*' to 'WCHAR*' in argument passing

QObject::connection(const QObject*, const char*, const char*, Qt::ConnectionType) "Cannot convert argument 3 from 'fileProcessor' to const QObject *"

VS2017 C++ compiler error C2664 can not convert argument

C2664 cannot convert to && value

C2440: '=': cannot convert from 'const char [9]' to 'char*'

error: cannot convert 'std::basic_string<char>::iterator ...' to 'const char* for argument '1' ...'

error C2440: 'return' : cannot convert from 'char *const ' to 'LPCTSTR'

error: cannot convert 'const char**' to 'const char (*)[64]'