简体   繁体   English

在参数传递中无法将'const char *'转换为'WCHAR *'

[英]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: 从中选择:

  • Unicode character set (UTF-16), default Unicode字符集(UTF-16),默认
  • Multi-Byte character set (UTF-8) 多字节字符集(UTF-8)
  • Not Set 没有设置

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.

相关问题 无法将参数1从&#39;const char *&#39;转换为&#39;const wchar_t *&#39; - cannot convert argument 1 from 'const char *' to 'const wchar_t *' &#39;默认参数&#39;:无法从&#39;const char [1]&#39;转换为&#39;const wchar_t *&#39; - 'default argument' : cannot convert from 'const char [1]' to 'const wchar_t *' 在MessageBOX等中,不能将const wchar_t转换为&#39;const_char&#39;作为参数2 - cannot convert const wchar_t to 'const_char' for argument 2 in MessageBOX etc etc 无法将“const char*”转换为“LPCWSTR {aka const wchar_t*}” - cannot convert 'const char*' to 'LPCWSTR {aka const wchar_t*}' 不能从&#39;ATL :: CStringT转换参数1 <wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> &gt;&gt;&#39;到&#39;const char *&#39; - cannot convert argument 1 from 'ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>' to 'const char *' &#39;strcpy&#39;:无法将参数2从&#39;WCHAR *&#39;转换为&#39;const char * - 'strcpy' : cannot convert parameter 2 from 'WCHAR *' to 'const char * 无法将 'wchar_t*' 转换为 'LPCSTR' {aka 'const char*'} - cannot convert 'wchar_t*' to 'LPCSTR' {aka 'const char*'} 如何将const char *转换为const WCHAR * - How to convert const char* to const WCHAR* 将 const char* 转换为 const wchar_t* - Convert const char* to const wchar_t* 如何将 const WCHAR * 转换为 const char * - how to convert const WCHAR * to const char *
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM