无法从“char *”转换参数 1至“LPCWSTR”
我试图加载一个
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
}
来自示例的 BMP 文件,但是我现在收到错误
错误 C2664:“auxDIBImageLoadW”:无法将参数 1 从“char *”转换为“LPCWSTR”
我该如何更正此问题?
Im trying to load a BMP file
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'
how could I correct this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在编译应用程序,并将字符集设置为 UNICODE(项目设置 -> 配置选项 -> 常规)。 Windows 头文件使用 #defines 将函数名称“映射”到 nameA(对于多字节字符串)或 nameW(对于 unicode 字符串)。
这意味着头文件中的某个地方会有一个像这样的#define
所以你实际上并没有调用
auxDIBImageLoad
(没有具有该名称的函数),你正在调用auxDIBImageLoadW
代码>.并且auxDIBImageLoadW
需要一个 unicode 字符串 (wchar_t const*
)。您正在传递一个多字节字符串 (char const*
)。您可以执行以下操作之一
LoadBMP
函数以接受 unicode 字符串本身LoadBMP
内的 unicode我建议将
LoadBMP
更改为接受unicode 字符串本身或直接调用 auxDIBImageLoadA(按该顺序)。如果不破坏很多其他代码,更改项目设置可能没问题。
不过,我不建议转换字符串,因为这是不必要的。直接调用 auxDIBImageLoadA 要容易得多,而且结果是一样的。
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).
That means somewhere in a header file there will be a #define like this
So you're not actually calling
auxDIBImageLoad
(there is no function with that name), you're callingauxDIBImageLoadW
. AndauxDIBImageLoadW
expects a unicode string (wchar_t const*
). You're passing a multi-byte string (char const*
).You can do one of the following
auxDIBImageLoad
withauxDIBImageLoadA
LoadBMP
function to accept a unicode string itselfLoadBMP
I'd recommend either changing
LoadBMP
to accept a unicode string itself or callingauxDIBImageLoadA
directly (in that order).Changing the project settings might be OK if it doesn't break a lot of other code.
I would not suggest converting the string though, since it's unnecessary. Calling
auxDIBImageLoadA
directly is far easier, and the result is the same.您有几个选择:
auxDIBImageLoadA
而不是auxDIBImageLoad
文件名
的类型从char*
到wchar_t*
std::mbstowcs
将文件名从char*
转换为wchar_t*
You have a few options:
auxDIBImageLoadA
instead ofauxDIBImageLoad
Filename
's type fromchar*
towchar_t*
std::mbstowcs
to convert Filename from achar*
to awchar_t*
看起来您试图使用两种不同的字符集。 'char ' 是典型的 ANSI,LPCWSTR 是宽字符(即 unicode)。如果您想使用字符,请将项目设置中的“字符集”属性更改为“无集”。
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 char change the 'Character Set' property in your project setting to 'No Set'.
尝试按以下方式使用 MultiByteToWideChar():
Try using MultiByteToWideChar() the following way:
将字符数组转换为 LPCWSTR。您可以在此处的第二个人帖子中看到这一点
Convert the character array to a LPCWSTR. You can see this in the 2nd guys post here