Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

char

wchar

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?

like image 266
DTDest Avatar asked Sep 27 '14 10:09

DTDest


1 Answers

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.

like image 86
the swine Avatar answered Sep 22 '22 22:09

the swine