C++ 无法将参数 1 从“char”转换为“LPCWSTR”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3924926/
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 parameter 1 from 'char' to 'LPCWSTR'
提问by sebastian
I keep getting this error:
cannot convert parameter 1 from 'char' to 'LPCWSTR'
我不断收到此错误:
cannot convert parameter 1 from 'char' to 'LPCWSTR'
int main(int argc, char argv[])
{
// open port for I/O
HANDLE h = CreateFile(argv[1],GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if(h == INVALID_HANDLE_VALUE) {
PrintError("E012_Failed to open port");
can someone help?
有人可以帮忙吗?
采纳答案by ybungalobill
It should be
它应该是
int main(int argc, char* argv[])
And
和
HANDLE h = CreateFileA(argv[1],GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
回答by Michael Goldshteyn
Go to the Properties for your Project and under Configuration Properties/General, change the Character Set to "Not Set". This way, the compiler will not assume that you want Unicode characters, which are selected by default:
转到项目的属性,然后在配置属性/常规下,将字符集更改为“未设置”。这样,编译器就不会假设您需要 Unicode 字符,默认情况下会选择这些字符:
回答by Nikola Smiljani?
This is the main function that Visual Studio creates by default:
这是 Visual Studio 默认创建的主要函数:
int _tmain(int argc, _TCHAR* argv[])
Where _TCHAR is defined to be char or wchar_t depending if _UNICODE is defined or not. The same thing happens with API functions. I would advise you against using explicit CreateFileA. Change your main and use CreateFile.
其中 _TCHAR 定义为 char 或 wchar_t 取决于是否定义了 _UNICODE。API 函数也会发生同样的事情。我建议您不要使用显式 CreateFileA。更改您的主文件并使用 CreateFile。
回答by Allbite
Depending on your compiler setting for CharacterSet, you may need to perform a multibyte / widechar conversion, or change the CharacterSet if you don't care what it is.
根据您对CharacterSet的编译器设置,您可能需要执行多字节/宽字符转换,或者如果您不关心它是什么,则更改 CharacterSet。
For converting with MultiByteToWideChar, see the following...
要使用 MultiByteToWideChar 进行转换,请参阅以下内容...
回答by Johann Gerell
I guess you're compiling with Unicode enabled. Then with char argv[]
, argv
is a char
array, so argv[1]
is a char
, and CreateFile
wants a const wchar_t*
as first parameter, not a char
.
我猜你是在启用 Unicode 的情况下编译的。然后 with char argv[]
,argv
是一个char
数组,argv[1]
a 也是char
,并且CreateFile
希望 aconst wchar_t*
作为第一个参数,而不是 a char
。
That said, your main
definition is also broken, it should have char* argv[]
. With that change, you can call CreateFileA
.
也就是说,你的main
定义也被打破了,它应该有char* argv[]
. 通过该更改,您可以调用CreateFileA
.