Cannot convert 'const char *' to 'WCHAR *' when passing arguments

I have documentation saying that the username, IP and password must be const char* , and when I put varaibles in a const char , I get 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*' to WCHAR *' when passing arguments

Which variable should I use for these arguments and how?

+6
source share
2 answers

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

  • Unicode character set (UTF-16), default
  • Multibyte Character Set (UTF-8)
  • Not set

Call functions that accept strings in Unicode settings require you to make Unicode string literals:

 "hello" 

It is a type of const char* , whereas:

 L"hello" 

has type const wchar_t* . Therefore, either change the configuration to Not set , or change the string literals to wide.

+12
source

For literals, you want to use L in a string, as in:

 L"My String" 

If you can compile with a wide character or not, you might want to use the _T() macro _T() :

 _T("My String") 

Wide string characters in MS-Windows use the UTF-16 format. See the Unicode website for more information on Unicode formats.

To dynamically convert a string, you need to know the format of your string char * . In most cases under Windows it is Win1252, but not always. Microsoft Windows supports many 8-bit formats, including UTF-8 and ISO-8859-1.

If you trust the locale setting, you can use the mbstowc_s() functions.

For other conversions, you can look at the MultiByteToWideChar() function

+3
source

Source: https://habr.com/ru/post/975922/


All Articles