C++ 为什么不能将 TCHAR* 转换为 char*

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2287879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:54:24  来源:igfitidea点击:

Why can't convert TCHAR* to char*

c++visual-studiovisual-studio-2010

提问by Christoferw

error C2664: 'strcpy' : cannot convert parameter 1 from 'TCHAR *' to 'char *' code:

错误 C2664:“strcpy”:无法将参数 1 从“TCHAR *”转换为“char *”代码:

LPCTSTR name, DWORD value
strcpy (&this->valueName[0], name);

error C2664: 'strlen' : cannot convert parameter 1 from 'LPCTSTR' to 'const char *'

错误 C2664:“strlen”:无法将参数 1 从“LPCTSTR”转换为“const char *”

LPCTSTR name; 
strlen (name)     

The code above to a class which works fine in another project, I can't find the reason why it doesn't work in this MS VS2010 Project.

上面的代码在另一个项目中运行良好的类,我找不到它在这个 MS VS2010 项目中不起作用的原因。

回答by Goz

You need to use a function such as wcstombswhen _UNICODE is defined. Either that or just use _tcslen(Look under Generic-Text Routine Mappings) on the TCHAR string and the compiler will transfer it to either strlen or wcslen depending if you are using unicode or not.

定义_UNICODE 时需要使用wcstombs等函数。或者只是在 TCHAR 字符串上使用_tcslen(在Generic-Text Routine Mappings下查看),编译器会将其传输到 strlen 或 wcslen ,具体取决于您是否使用 unicode 。

回答by villintehaspam

Probably because TCHAR is defined to be a char in one of your projects, but not in the VS2010 one where it is probably wchar_t.

可能是因为 TCHAR 在你的一个项目中被定义为一个字符,但在 VS2010 中它可能不是 wchar_t。

If your project defines UNICODE/_UNICODE, which is the same as specifying it to be a Unicode build in the project settings, TCHARs will be wchar_t.

如果您的项目定义了 UNICODE/_UNICODE,这与在项目设置中将其指定为 Unicode 构建相同,则 TCHAR 将为 wchar_t。

You basically need to decide whether to use Unicode or not and if you do, you need to change the regular calls to strncpy et al to the wide-char equivalents or use the t-variants that change the same way as TCHARs do. Look at the help for strncpy or the other functions to see what the wide or t-variants are called.

您基本上需要决定是否使用 Unicode,如果使用,您需要将对 strncpy 等的常规调用更改为宽字符等效项,或者使用与 TCHAR 相同的更改方式的 t 变体。查看 strncpy 或其他函数的帮助以了解宽变体或 t 变体的名称。

You can also look at MSDN for the calls such as strcpy, where you can see that the wide-char version is called wcscpy and the t version is called _tcscpy. I would recommend you to stick with the t-versions if you are going to use the code in different projects that either use UNICODE or not, or to make an informed decision which one you are going to use and then stick with that. Which is better depends on your scenario I would say and may invoke some "religious" opinions...

您还可以查看 MSDN 以获取诸如strcpy 之类的调用,在那里您可以看到宽字符版本称为 wcscpy,而 t 版本称为 _tcscpy。如果您打算在使用或不使用 UNICODE 的不同项目中使用代码,我建议您坚持使用 t 版本,或者做出明智的决定,您将使用哪个,然后坚持下去。哪个更好取决于您的情况,我会说并且可能会援引一些“宗教”意见......

回答by jkp

Is your project a unicode project? If so I believe TCHARwill be equivalent to a wchar_trather than a charmaking your conversion attempts invalid. See herefor more info.

您的项目是 unicode 项目吗?如果是这样,我相信TCHAR将相当于一个wchar_t而不是char使您的转换尝试无效。 请参阅此处了解更多信息。

回答by Jan-Albert van den Berg

Here is some code that will do the trick for you, it was originally posted to www.wincli.com/?p=72 but here I encapsulated it into a small class :)

这是一些可以为您解决问题的代码,它最初发布到 www.wincli.com/?p=72 但在这里我将其封装成一个小类:)

class char_args
{
private:
char **l_argn;
int arg_num;

int wstrlen(_TCHAR * wstr)
{
    int l_idx = 0;
    while (((char*)wstr)[l_idx] != 0) l_idx += 2;
    return l_idx;
}

// Allocate char string and copy TCHAR->char->string
char *wstrdup(_TCHAR *wSrc)
{
    int l_idx = 0;
    int l_len = wstrlen(wSrc);
    char *l_nstr = (char *)malloc(l_len);
    if (l_nstr) {
        do {
            l_nstr[l_idx] = (char)wSrc[l_idx];
            l_idx++;
        } while ((char)wSrc[l_idx] != 0);
    }
    l_nstr[l_idx] = 0;
    return l_nstr;
}

char_args & operator=(const char_args&); // does not allow assignment of class
char_args(const char_args&); // does not allow copy construction

public:
char_args(int argcc, _TCHAR* argv[]) : arg_num(argcc)
{
    l_argn = (char **)malloc(argcc *sizeof(char*));
    for (int idx = 0; idx < argcc; idx++) l_argn[idx] = wstrdup(argv[idx]);
}

~char_args()
{
    for(int idx = 0; idx < arg_num; idx++) if (l_argn[idx]) free(l_argn[idx]);
    free(l_argn);
}

const char * operator[](const int &i)
{
    if (i < arg_num) return l_argn[i]; else return 0;
}

const int argc() { return arg_num; }
};

Here is a demonstration of use of the code:

下面是代码的使用演示:

int _tmain(int argc, _TCHAR* argv[])
{
  char_args C_ARGV(argc, argv);
  for(int i = 0; i  < C_ARGV.argc(); i++) cout << C_ARGV[i] << endl;
}