[英]Cannot convert 'const char*' to 'WCHAR*' in argument passing
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. 我有文件,其中写的用户名,IP和密码必须是
const char*
,当我在const char
放置变量时,我收到此错误消息。
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在参数传递中不能将
const char*' to
转换const char*' to
WCHAR *'
Which kind of variable must I use for those arguments and how? 我必须使用哪种变量用于这些参数以及如何使用?
You are most likely using one of the Visual Studio compilers, where in Project Settings
, there is a Character set
choice. 您最有可能使用Visual Studio编译器之一,在“
Project Settings
,有一个Character set
选项。 Choose from: 从中选择:
Calling functions that accept strings in the Unicode setting requires you to make Unicode string literals: 调用接受Unicode设置中的字符串的函数需要您创建Unicode字符串文字:
"hello"
Is of type const char*
, whereas: 是
const char*
类型,而:
L"hello"
is of type const wchar_t*
. 是
const wchar_t*
类型。 So either change your configuration to Not set
or change your string literals to wide ones. 因此,要么将配置更改为“
Not set
要么将字符串文字更改为宽文字。
For literals, you want to use L
on the string as in: 对于文字,您希望在字符串上使用
L
,如下所示:
L"My String"
If you may compile in wide character or not, then you may want to consider using the _T()
macro instead: 如果您可以编译宽字符,那么您可能需要考虑使用
_T()
宏:
_T("My String")
Wide string characters under MS-Windows make use of the UTF-16 format. MS-Windows下的宽字符字符使用UTF-16格式。 For more information about Unicode formats, look on the Unicode website .
有关Unicode格式的更多信息,请查看Unicode网站 。
To dynamically convert a string, you need to know the format of your char *
string. 要动态转换字符串,您需要知道
char *
字符串的格式。 In most cases, under Windows it is a Win1252, but definitively not always. 在大多数情况下,在Windows下它是Win1252,但最终并非总是如此。 Microsoft Windows supports many 8 bit formats, including UTF-8 and ISO-8859-1.
Microsoft Windows支持许多8位格式,包括UTF-8和ISO-8859-1。
If you trust the locale setup, you could use the mbstowc_s()
functions. 如果您信任语言环境设置,则可以使用
mbstowc_s()
函数。
For other conversions, you may want to look at the MultiByteToWideChar()
function 对于其他转换,您可能需要查看
MultiByteToWideChar()
函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.