2010-11-17 202 views
2

我有一個TCHAR如下定義:錯誤C2446:==:沒有從爲const char *到TCHAR *轉換

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

,我想如下comapare:

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

但後來我我得到的錯誤:

錯誤C2446:==:沒有從爲const char *到TCHAR *
錯誤C2440轉換: '==':canno噸從 '爲const char [14]' 到 'TCHAR [260]' 轉換

回答

8

"NDSClient.exe"const char*串上的窗戶。如果你想讓它變成const TCHAR*那麼你需要使用宏TEXT。此外,您無法使用==使用等效的TCHAR功能比較字符串,如_tcscmp

5

你也可以使用。 L"some string"使TCHAR *。但我建議您使用std::wstringstd::string的模擬和std::string需要#include <string>)而不是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; 
} 
+0

修正你的代碼標記。 – 2010-11-17 08:42:55

+0

L「some string」是「WCHAR *」而不是「TCHAR *」。 – MSalters 2010-11-17 09:13:59

+0

如果你使用'std :: wstring',那麼你應該在每個地方使用'wchar_t'而不是TCHAR。 (並且擴展名不能使用TEXT宏) – 2010-11-17 14:30:55