无法将参数1从'const char *'转换为'LPCWSTR' [英] cannot convert parameter 1 from 'const char *' to 'LPCWSTR'

查看:927
本文介绍了无法将参数1从'const char *'转换为'LPCWSTR'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有一些简单的代码可以处理文件,我正尝试将其移植到Windows。我有这样的东西:

Basically I have some simple code that does some things for files and I'm trying to port it to windows. I have something that looks like this:

int SomeFileCall(const char * filename){
#ifndef __unix__
    SomeWindowsFileCall(filename);
#endif
#ifdef __unix__
    /**** Some unix only stat code here! ****/
#endif
}

SomeWindowsFileCall(filename); 导致编译器错误:
无法将参数1从'const char *'转换为'LPCWSTR'

如何在不更改 SomeFileCall 原型的情况下解决此问题?

How do I fix this, without changing the SomeFileCall prototype?

推荐答案

大多数使用字符串的Windows API都有两个版本:一个使用 char * ,另一个使用 WCHAR * (后者等效于 wchar_t * )。

Most of the Windows APIs that take strings have two versions: one that takes char * and one that takes WCHAR * (that latter is equivalent to wchar_t *).

SetWindowText 实际上是一个扩展为 SetWindowTextA (需要 char * )或 SetWindowTextW (需要 WCHAR * )。

SetWindowText, for example, is actually a macro that expands to either SetWindowTextA (which takes char *) or SetWindowTextW (which takes WCHAR *).

在您的项目中,听起来所有这些宏都引用了-W版本。这由 UNICODE 预处理器宏控制(如果在Visual Studio中选择使用Unicode字符集项目选项,则定义该宏)。 (Microsoft的一些C和C ++运行时库函数还具有ANSI和宽版本。您将获得的那个是由类似命名的 _UNICODE 宏选择的,该宏也由该宏定义通常是Visual Studio项目设置。)

In your project, it sounds like all of these macros are referencing the -W versions. This is controlled by the UNICODE preprocessor macro (which is defined if you choose the "Use Unicode Character Set" project option in Visual Studio). (Some of Microsoft's C and C++ run time library functions also have ANSI and wide versions. Which one you get is selected by the similarly-named _UNICODE macro that is also defined by that Visual Studio project setting.)

通常,-A和-W函数都存在于库中并且可用,即使您的应用程序是针对Unicode编译的。 (有例外;一些新功能仅在宽版本中可用。)

Typically, both of the -A and -W functions exist in the libraries and are available, even if your application is compiled for Unicode. (There are exceptions; some newer functions are available only in "wide" versions.)

如果您有 char * 在正确的ANSI代码页中包含文本,则可以显式调用-A版本(例如, SetWindowTextA )。 -A版本通常是包装程序,可对字符串参数进行宽字符复制并将控制权传递给-W版本。

If you have a char * that contains text in the proper ANSI code page, you can call the -A version explicitly (e.g., SetWindowTextA). The -A versions are typically wrappers that make wide character copies of the string parameters and pass control to the -W versions.

另一种方法是制作自己的宽字符副本的字符串。您可以使用 MultiByteToWideChar 。调用它可能很棘手,因为您必须管理缓冲区。如果您可以直接调用-A版本,那通常会更简单并且已经过测试。但是,如果您的 char * 字符串使用的是UTF-8或用户当前ANSI代码页以外的任何编码,则应该自己进行转换。

An alternative is to make your own wide character copies of the strings. You can do this with MultiByteToWideChar. Calling it can be tricky, because you have to manage the buffers. If you can get away with calling the -A version directly, that's generally simpler and already tested. But if your char * string is using UTF-8 or any encoding other than the user's current ANSI code page, you should do the conversion yourself.

-A后缀代表 ANSI,这是Windows中单字节代码页字符的常用术语设置。

The -A suffix stands for "ANSI", which was the common Windows term for a single-byte code-page character set.

后缀-W表示宽(意味着编码单位比单个字节宽)。具体来说,Windows对宽字符串使用小尾数UTF-16。 MSDN文档只是将其称为 Unicode,这有点用词不当。

The -W suffix stands for "Wide" (meaning the encoding units are wider than a single byte). Specifically, Windows uses little-endian UTF-16 for wide strings. The MSDN documentation simply calls this "Unicode", which is a little bit of a misnomer.

这篇关于无法将参数1从'const char *'转换为'LPCWSTR'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆