【问题标题】:error C2446: == : no conversion from const char * to TCHAR *错误 C2446:==:没有从 const char * 转换为 TCHAR *
【发布时间】:2010-11-17 06:00:19
【问题描述】:

我在下面定义了一个 TCHAR:

 TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");

我想如下:

if(szProcessName == "NDSClient.exe")
{
} 

但是我得到了错误:

错误 C2446:==:没有从 const char * 转换为 TCHAR *
错误 C2440:“==”:无法从“const char [14]”转换为“TCHAR [260]”

【问题讨论】:

    标签: c++ windows tchar


    【解决方案1】:

    "NDSClient.exe" 是 Windows 上的 const char* 字符串。如果您希望它成为const TCHAR*,那么您需要使用TEXT 宏。此外,您不能使用 == 比较字符串,请使用等效的 TCHAR 函数,例如 _tcscmp

    【讨论】:

      【解决方案2】:

      你也可以使用。 L"some string" 制作 TCHAR*。但我建议你使用std::wstringstd::string 的类似物,因为std::string 需要#include &lt;string&gt;)而不是 TCHAR*。

      示例:

      #include <windows.h>
      #include <string>
      #include <iostream>
      using namespace std;
      int main()
      {
       wstring s = TEXT("HELLO");
       wstring ss = L"HELLO";
       if(s == ss)
        cout << "hello" << endl;
       return 0;
      }
      

      【讨论】:

      • L"some string" 是 WCHAR* 而不是 TCHAR*
      • 如果你使用std::wstring,那么你应该在任何地方使用wchar_t而不是TCHAR。 (并且通过扩展你不应该使用 TEXT 宏)
      • 还要注意L"some string" 不是WCHAR*。它是const WCHAR[12] :)
      • 感谢大家提供有用的 cmets。这对我很有帮助。
      猜你喜欢
      • 1970-01-01
      • 2019-01-05
      • 2019-05-22
      • 1970-01-01
      • 2011-11-02
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 1970-01-01
      相关资源
      最近更新 更多