C++ 无法在参数传递中将“const char*”转换为“WCHAR*”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26073814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot convert 'const char*' to 'WCHAR*' in argument passing
提问by DTDest
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
在参数传递中转换WCHAR*'
Which kind of variable must I use for those arguments and how?
对于这些参数,我必须使用哪种变量以及如何使用?
回答by the swine
You are most likely using one of the Visual Studio compilers, where in Project Settings
, there is a Character set
choice. Choose from:
您最有可能使用的是其中一种 Visual Studio 编译器,其中Project Settings
有一个Character set
选项。从中选择:
- Unicode character set (UTF-16), default
- Multi-Byte character set (UTF-8)
- Not Set
- Unicode 字符集 (UTF-16),默认
- 多字节字符集 (UTF-8)
- 没有设置
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*
. So either change your configuration to Not set
or change your string literals to wide ones.
是 类型const wchar_t*
。因此,要么将您的配置更改为Not set
或将您的字符串文字更改为宽字符串。
回答by Alexis Wilke
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. For more information about Unicode formats, look on the Unicode website.
MS-Windows 下的宽字符串字符使用 UTF-16 格式。有关 Unicode 格式的更多信息,请查看Unicode 网站。
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.
要动态转换字符串,您需要知道char *
字符串的格式。在大多数情况下,在 Windows 下它是 Win1252,但并非总是如此。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()
函数