【问题标题】:Why can't convert TCHAR* to char*为什么不能将 TCHAR* 转换为 char*
【发布时间】:2010-02-18 10:33:48
【问题描述】:

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

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

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

LPCTSTR name; 
strlen (name)     

上面的代码在另一个项目中可以正常工作,我找不到它在这个 MS VS2010 项目中不起作用的原因。

【问题讨论】:

    标签: c++ visual-studio visual-studio-2010


    【解决方案1】:

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

    【讨论】:

    • 那篇文章为我节省了几个小时。新 C++ 项目的默认设置是使用 unicode 字符集。关闭它,修复了这些错误。
    【解决方案2】:

    可能是因为 TCHAR 在您的一个项目中被定义为 char,但在 VS2010 中却没有,它可能是 wchar_t。

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

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

    你也可以查看MSDN中的strcpy等调用,其中可以看到wide-char版本叫wcscpy,t版本叫_tcscpy。如果您要在使用 UNICODE 或不使用 UNICODE 的不同项目中使用代码,或者做出明智的决定,您将使用哪个,然后坚持使用,我建议您坚持使用 t 版本。哪个更好取决于我会说的您的情况,并且可能会引起一些“宗教”观点...

    【讨论】:

    • @Christoferw:我更新了我的答案,为您提供有关如何解决此问题的指示。如果您需要更多帮助,请再写一条评论。
    【解决方案3】:

    您的项目是 unicode 项目吗?如果是这样,我相信 TCHAR 将等同于 wchar_t 而不是 char 使您的转换尝试无效。 See here 了解更多信息。

    【讨论】:

    • 在Visual Studio的项目设置中选择:字符集:使用多字节字符集
    【解决方案4】:

    这里有一些代码可以帮你解决问题,它最初发布到 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; }
    };
    

    下面是代码的使用演示:

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-18
      • 1970-01-01
      • 2019-01-05
      • 2011-11-02
      • 1970-01-01
      • 2010-12-19
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      相关资源
      最近更新 更多