[英]const WCHAR * myVar vs const char * myVar
我開發了一個小型的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中表現不佳,請指導我以后的方法出了什么問題。
編輯:
錯誤消息: 錯誤C2664:'Gdiplus :: Status Gdiplus :: Image :: Save(const WCHAR *,const CLSID *,const Gdiplus :: EncoderParameters *)':無法將參數1從'char *'轉換為'const WCHAR * '1>指出的類型是無關的; 轉換需要reinterpret_cast,C風格的轉換或函數式轉換
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...
}
好吧,看起來你正在編譯Unicode支持。 可在此處找到Win32數據類型列表
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;
這是一個鏈接,顯示如何在各種字符串類型之間轉換字符串轉換樣本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.