无法将参数 1 从“char *"转换为“LPCWSTR"

2021-12-19 00:00:00 opengl char visual-c++ c++ bmp

我正在尝试加载 BMP 文件

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

错误 C2664:auxDIBImageLoadW":无法将参数 1 从char *"转换为LPCWSTR"

error C2664: 'auxDIBImageLoadW' : cannot convert parameter 1 from 'char *' to 'LPCWSTR'

我该如何纠正?

推荐答案

您正在编译应用程序,将字符集设置为 UNICODE(项目设置 -> 配置选项 -> 常规).Windows 头文件使用 #defines 将函数名称映射"到 nameA(对于多字节字符串)或 nameW(对于 unicode 字符串).

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).

这意味着在头文件的某处会有一个这样的#define

That means somewhere in a header file there will be a #define like this

#define auxDIBImageLoad auxDIBImageLoadW

所以您实际上并不是在调用 auxDIBImageLoad(没有具有该名称的函数),而是在调用 auxDIBImageLoadW.而 auxDIBImageLoadW 需要一个 unicode 字符串(wchar_t const*).您正在传递一个多字节字符串 (char const*).

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*).

您可以执行以下操作之一

You can do one of the following

  • 更改您的项目以使用多字节字符集(-> 项目设置)
  • 通过将 auxDIBImageLoad 替换为 auxDIBImageLoadA
  • 来显式调用该函数的多字节版本
  • 更改您的 LoadBMP 函数以接受 unicode 字符串本身
  • LoadBMP
  • 中将字符串转换为unicode
  • change your project to use multi-byte character set (-> project settings)
  • explicitly call the multi-byte version of the function by replacing auxDIBImageLoad with auxDIBImageLoadA
  • change your LoadBMP function to accept a unicode string itself
  • convert the string to unicode inside LoadBMP

我建议更改 LoadBMP 以接受 unicode 字符串本身或直接调用 auxDIBImageLoadA(按此顺序).如果更改项目设置不会破坏很多其他代码,那么它可能没问题.我不会不建议转换字符串,因为它是不必要的.直接调用auxDIBImageLoadA要容易得多,结果是一样的.

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 not suggest converting the string though, since it's unnecessary. Calling auxDIBImageLoadA directly is far easier, and the result is the same.

相关文章