C++ 无法将参数 1 从“char *”转换为“LPCWSTR”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5480588/
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 dactz
Im trying to load a BMP file
我正在尝试加载 BMP 文件
AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image
{
FILE *File=NULL; // File Handle
if (!Filename) // Make Sure A Filename Was Given
{
return NULL; // If Not Return NULL
}
File=fopen(Filename,"r"); // Check To See If The File Exists
if (File) // Does The File Exist?
{
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}
return NULL; // If Load Failed Return NULL
}
this has come from an example however i'm now getting the error
这是来自一个例子但是我现在收到错误
error C2664: 'auxDIBImageLoadW' : cannot convert parameter 1 from 'char *' to 'LPCWSTR'
错误 C2664:“auxDIBImageLoadW”:无法将参数 1 从“char *”转换为“LPCWSTR”
how could I correct this?
我怎么能纠正这个?
回答by Paul Groke
You're compiling your application with Character-Set set to UNICODE (Project Settings -> Configuration Options -> General). Windows header files use #defines to "map" function names to either nameA (for multi-byte strings) or nameW (for unicode strings).
您正在使用字符集设置为 UNICODE(项目设置 -> 配置选项 -> 常规)编译应用程序。Windows 头文件使用 #defines 将函数名称“映射”到 nameA(对于多字节字符串)或 nameW(对于 unicode 字符串)。
That means somewhere in a header file there will be a #define like this
这意味着在头文件中的某处会有这样的#define
#define auxDIBImageLoad auxDIBImageLoadW
So you're not actually calling auxDIBImageLoad
(there is no function with that name), you're calling auxDIBImageLoadW
. And auxDIBImageLoadW
expects a unicode string (wchar_t const*
). You're passing a multi-byte string (char const*
).
因此,您实际上并不是在调用auxDIBImageLoad
(没有具有该名称的函数),而是在调用auxDIBImageLoadW
. 并auxDIBImageLoadW
期望一个 unicode 字符串 ( wchar_t const*
)。您正在传递一个多字节字符串 ( char const*
)。
You can do one of the following
您可以执行以下操作之一
- change your project to use multi-byte character set (-> project settings)
- explicitly call the multi-byte version of the function by replacing
auxDIBImageLoad
withauxDIBImageLoadA
- change your
LoadBMP
function to accept a unicode string itself - convert the string to unicode inside
LoadBMP
- 更改您的项目以使用多字节字符集(-> 项目设置)
- 通过替换
auxDIBImageLoad
为显式调用函数的多字节版本auxDIBImageLoadA
- 更改您的
LoadBMP
函数以接受 unicode 字符串本身 - 将字符串转换为里面的 unicode
LoadBMP
I'd recommend either changing LoadBMP
to accept a unicode string itself or calling auxDIBImageLoadA
directly (in that order).
Changing the project settings might be OK if it doesn't break a lot of other code.
I would notsuggest converting the string though, since it's unnecessary. Calling auxDIBImageLoadA
directly is far easier, and the result is the same.
我建议要么更改LoadBMP
为接受 unicode 字符串本身,要么auxDIBImageLoadA
直接调用(按该顺序)。如果更改项目设置不会破坏很多其他代码,那么它可能没问题。我不建议转换字符串,因为它是不必要的。auxDIBImageLoadA
直接调用要容易得多,结果是一样的。
回答by ildjarn
You have a few options:
您有几个选择:
- Change the 'character set' option in your project settings from 'Unicode' to 'Not Set'
- Call
auxDIBImageLoadA
instead ofauxDIBImageLoad
- Change
Filename
's type fromchar*
towchar_t*
- Use
std::mbstowcs
to convert Filename from achar*
to awchar_t*
- 将项目设置中的“字符集”选项从“Unicode”更改为“未设置”
- 调用
auxDIBImageLoadA
而不是auxDIBImageLoad
- 将
Filename
类型从更改char*
为wchar_t*
- 使用
std::mbstowcs
到文件名从转换char*
到wchar_t*
回答by skimobear
Looks like your trying to use two different character sets. 'char ' is the typical ANSI and LPCWSTR is the wide character (i.e. unicode). If you would like to use charchange the 'Character Set' property in your project setting to 'No Set'.
看起来您尝试使用两种不同的字符集。'char ' 是典型的 ANSI,LPCWSTR 是宽字符(即 unicode)。如果您想使用 char,请将项目设置中的“Character Set”属性更改为“No Set”。
回答by Slavy Mihov
Try using MultiByteToWideChar()the following way:
尝试按以下方式使用MultiByteToWideChar():
void main(int argc, char* argv[])
{
...
wchar_t filename[4096] = {0};
MultiByteToWideChar(0, 0, argv[1], strlen(argv[1]), filename, strlen(argv[1]));
// RenderFile() requires LPCWSTR (or wchar_t*, respectively)
hr = pGraph->RenderFile(filename, NULL);
...
}