我开发了一个小型 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);
然后函数将被调用为,(尽管我只需要遵循上面的原型(prototype)),
char *src_bmp_path = "test.bmp";
char *dest_jpeg_path= "test.jpg";
convertBMPtoJPG(src_bmp_path,dest_jpeg_path);
关于 stackover 的另一个问题提供了太多有关 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...
}
关于c++ - const WCHAR * myVar 与 const char * myVar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14987537/