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

DTDest picture DTDest · Sep 27, 2014 · Viewed 31.3k times · Source

I have documentation where written that username, IP and password must be const char* and when I'm putting varaibles in const char, I'm getting this error message.

This is my code:

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <windows.h>

using namespace std;

typedef int (__cdecl *MYPROC)(LPWSTR);

int main()
{
    HINSTANCE hinstDLL;
    MYPROC ProcAdd;   
    hinstDLL = LoadLibrary("LmServerAPI.dll");
    if(hinstDLL != NULL){
        ProcAdd = (MYPROC) GetProcAddress(hinstDLL,"LmServer_Login");            
        if(ProcAdd != NULL){
            const char* IP = "xxx.177.xxx.23";
            const char* name = "username";
            const char* pass = "password";
            int port = 888;
            ProcAdd(IP,port,name,pass);
            system ("pause");          
        }          
    }
}

And I got this error:

cannot convert const char*' toWCHAR*' in argument passing

Which kind of variable must I use for those arguments and how?

Answer

the swine picture the swine · Sep 27, 2014

You are most likely using one of the Visual Studio compilers, where in Project Settings, there is a Character set choice. Choose from:

  • Unicode character set (UTF-16), default
  • Multi-Byte character set (UTF-8)
  • Not Set

Calling functions that accept strings in the Unicode setting requires you to make Unicode string literals:

"hello"

Is of type const char*, whereas:

L"hello"

is of type const wchar_t*. So either change your configuration to Not set or change your string literals to wide ones.