简体   繁体   English

const WCHAR * myVar vs const char * myVar

[英]const WCHAR * myVar vs const char * myVar

I've developed a small bmp to jpg conveter. 我开发了一个小型的bmp到jpg转换器。 Following code is working and providing exact results as I need 以下代码正在运行,并根据需要提供准确的结果

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

then calling function as, 然后调用函数,

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

However I need the function to be changed as following (as per requirements I've been given), but doing so results as a compilation error. 但是我需要将函数更改为如下(根据我已经给出的要求),但这样做会导致编译错误。

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

then function would be called as, (though I need to follow just the prototype as above), 然后函数将被称为,(虽然我需要按照上面的原型),

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

Another question on stackover provided too much information about Win32 types, however I yet couldn't resolve the issue. 关于堆栈的另一个问题提供了有关Win32类型的太多信息,但是我还是无法解决问题。 I'm not that great in Win32 API, please guide me what's going wrong in later approach. 我在Win32 API中表现不佳,请指导我以后的方法出了什么问题。

Edit: 编辑:

Error Message: 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; 错误消息: 错误C2664:'Gdiplus :: Status Gdiplus :: Image :: Save(const WCHAR *,const CLSID *,const Gdiplus :: EncoderParameters *)':无法将参数1从'char *'转换为'const WCHAR * '1>指出的类型是无关的; conversion requires reinterpret_cast, C-style cast or function-style cast 转换需要reinterpret_cast,C风格的转换或函数式转换

Image::Save() only accepts WCHAR* values, so your char* wrapper will have to convert to WCHAR* , such as with MultiByteToWideChar() (just like Win32 API Ansi functions do when they call Win32 API Unicode functions internally), eg: 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...
}

Well, looks like your are compiling for Unicode support. 好吧,看起来你正在编译Unicode支持。 The list of Win32 data types can be found here 可在此处找到Win32数据类型列表

WCHAR is defined as - 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;

Here is a link showing how to convert between various string types String Conversion Samples. 这是一个链接,显示如何在各种字符串类型之间转换字符串转换样本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM