8

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?

the swine
  • 10,713
  • 7
  • 58
  • 100
DTDest
  • 99
  • 1
  • 2
  • 8
  • A `LPWSTR` aka `wchar_t *`? – T.C. Sep 27 '14 at 10:47
  • i'm new in c++, cannot convert const char*' toWCHAR*' in argument passing this is error from dev++ – DTDest Sep 27 '14 at 10:49
  • 1
    Is there some reason `ProcAdd` takes a `LPWSTR` if you're mandating (per your question) a `const char*`, where neither the const-ness not the character width match? – WhozCraig Sep 27 '14 at 10:49

2 Answers2

18

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.

the swine
  • 10,713
  • 7
  • 58
  • 100
  • i'm using dev C++ compiler what i must change in this compiler? – DTDest Sep 27 '14 at 11:04
  • That's a pretty unknown compiler to me. There could be a similar setting somewhere. If you can't find it, just make the literals Unicode (at lines where the errors you describe occur). – the swine Sep 27 '14 at 11:07
  • Also, your `MYPROC` is flawed, it has one non-`const` argument but you are passing four const ones (port, ip, user and password). But that bug is unrelated to your question. Correct would be `typedef int (__cdecl *MYPROC)(LPCWSTR, int, LPCWSTR, LPCWSTR)`. But you best refer to the page where the interface of `LmServerAPI.dll` is described, as using a different interface will result in crash or undefined behavior at runtime. – the swine Sep 27 '14 at 11:12
  • I just explained in my answer. Non-Unicode: `const char *text = "hello";` Unicode: `const wchar_t *text = L"hello";`. That goes also for the arguments to `LoadLibrary` or `GetProcAddress`. – the swine Sep 27 '14 at 11:13
  • It would also probably help to say on which lines the error(s) are reported. – the swine Sep 27 '14 at 11:15
  • thanks bro i love you soo much, problem was typedef int (__cdecl *MYPROC)(LPCWSTR, int, LPCWSTR, LPCWSTR) that line it works now, thanks thanks thanks ^_^ – DTDest Sep 27 '14 at 11:16
  • 1
    If only UTF-8 was that well-supported by MS. – Deduplicator Sep 27 '14 at 11:19
  • 1
    For those looking for this in Visual Studio 2017, it's under Project Properties > Configuration > General tab > Project Defaults > Character Set. – Phlucious Jan 16 '19 at 22:31
3

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

L"My String"

If you may compile in wide character or not, then you may want to consider using the _T() macro instead:

_T("My String")

Wide string characters under MS-Windows make use of the UTF-16 format. For more information about Unicode formats, look on the Unicode website.

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

If you trust the locale setup, you could use the mbstowc_s() functions.

For other conversions, you may want to look at the MultiByteToWideChar() function

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156