Cannot convert 'const char*' to 'WCHAR*' in argument passing
Asked Answered
C

2

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?

Clipfed answered 27/9, 2014 at 10:43 Comment(3)
A LPWSTR aka wchar_t *?Monograph
i'm new in c++, cannot convert const char*' toWCHAR*' in argument passing this is error from dev++Clipfed
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?Datum
H
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.

Homily answered 27/9, 2014 at 10:54 Comment(8)
i'm using dev C++ compiler what i must change in this compiler?Clipfed
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).Homily
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.Homily
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.Homily
It would also probably help to say on which lines the error(s) are reported.Homily
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 ^_^Clipfed
If only UTF-8 was that well-supported by MS.Blush
For those looking for this in Visual Studio 2017, it's under Project Properties > Configuration > General tab > Project Defaults > Character Set.Scleroprotein
F
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

Fictive answered 27/9, 2014 at 11:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.