首页 文章

const WCHAR * myVar vs const char * myVar

提问于
浏览
0

我开发了一个小型的bmp到jpg转换器 . 以下代码正在运行,并根据需要提供准确的结果

BOOLEAN convertBMPtoJPG(const WCHAR *src_bmp_path,const WCHAR *dest_jpeg_path);

然后调用函数,

const WCHAR *src_bmp_path = L"test.bmp";
const WCHAR *dest_jpeg_path= L"test.jpg";
convertBMPtoJPG(src_bmp_path,dest_jpeg_path);

但是我需要将函数更改为如下(根据我已经给出的要求),但这样做会导致编译错误 .

BOOLEAN convertBMPtoJPG(char *src_bmp_path,char *dest_jpeg_path);

然后函数将被称为,(虽然我需要按照上面的原型),

char *src_bmp_path = "test.bmp";
char *dest_jpeg_path= "test.jpg";
convertBMPtoJPG(src_bmp_path,dest_jpeg_path);

关于堆栈的另一个问题提供了有关Win32类型的太多信息,但是我还是无法解决问题 . 我在Win32 API中表现不佳,请指导我以后的方法出了什么问题 .

Edit:

错误消息: **error C2664: 'Gdiplus::Status Gdiplus::Image::Save(const WCHAR *,const CLSID *,const Gdiplus::EncoderParameters *)' : cannot convert parameter 1 from 'char ' to 'const WCHAR ' 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

2 回答

  • 0

    Image::Save() 只接受 WCHAR* 值,所以你的 char* 包装器必须转换为 WCHAR* ,例如 MultiByteToWideChar() (就像Win32 API Ansi函数在内部调用Win32 API Unicode函数时那样),例如:

    std::wstring towstring(const char *src)
    {
        std::wstring output;
        int src_len = strlen(src);
        if (src_len > 0)
        {
            int out_len = MultiByteToWideChar(CP_ACP, 0, src, src_len, NULL, 0);
            if (out_len > 0)
            {
                output.resize(out_len);
                MultiByteToWideChar(CP_ACP, 0, src, src_len, &output[0], out_len);
            }
        }
        return output;
    }
    
    BOOLEAN convertBMPtoJPG(char *src_bmp_path,char *dest_jpeg_path)
    {
        return convertBMPtoJPG(towstring(src_bmp_path).c_str(), towstring(dest_jpeg_path).c_str());
    }
    
    BOOLEAN convertBMPtoJPG(const WCHAR *src_bmp_path, const WCHAR *dest_jpeg_path)
    {
       // your existing conversion logic here...
    }
    
  • 2

    好吧,看起来你正在编译Unicode支持 . 可以找到Win32数据类型列表here

    WCHAR定义为 -

    A 16-bit Unicode character. For more information, see Character Sets Used By Fonts.
     This type is declared in WinNT.h as follows:
     typedef wchar_t WCHAR;
    

    这是一个链接,显示如何在各种字符串类型之间进行转换String Conversion Samples.

相关问题