Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

c++ - cannot convert parameter 1 from 'char *' to '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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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 with auxDIBImageLoadA
  • change your LoadBMP function to accept a unicode string itself
  • convert the string to unicode inside 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 not suggest converting the string though, since it's unnecessary. Calling auxDIBImageLoadA directly is far easier, and the result is the same.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...