error C2446: == : no conversion from const char * to TCHAR *
Asked Answered
E

2

3

I have a TCHAR define below:

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

and I want to comapare as below:

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

But then I am getting the errors:

error C2446: == : no conversion from const char * to TCHAR *
error C2440: '==' : cannot convert from 'const char [14]' to 'TCHAR [260]'

Enticement answered 17/11, 2010 at 6:0 Comment(0)
T
8

"NDSClient.exe" is a const char* string on windows. If you want it to become a const TCHAR* then you need to use the TEXT macro. Also, you can not compare strings using == use a equivalent TCHAR function such as _tcscmp.

Tambratamburlaine answered 17/11, 2010 at 6:4 Comment(0)
W
7

Also you can use. L"some string" to make TCHAR*. But I suggest you to use std::wstring (analog of std::string and as std::string needs #include <string>) instead of TCHAR*.

example:

#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;
}
Wop answered 17/11, 2010 at 8:41 Comment(4)
L"some string" is a WCHAR* not a TCHAR*.Annul
If you're using std::wstring then you should just use wchar_t everywhere instead of TCHAR. (And by extension you shouldn't use the TEXT macro)Meir
Also do note that L"some string" is not a WCHAR*. It is const WCHAR[12] :)Unicameral
Thanks to all of you for usefull comments. It was very helpfull for me.Wop

© 2022 - 2024 — McMap. All rights reserved.