问题
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*' to
WCHAR*' in argument passing
Which kind of variable must I use for those arguments and how?
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/26073814/cannot-convert-const-char-to-wchar-in-argument-passing