BOOL CALLBACK callback(HWND hwnd, LPARAM param) {
DWORD pid;
GetWindowThreadProcessId(hwnd, &pid);
if (pid == param) {
TCHAR classNameBuf[MAX_PATH];
GetClassName(hwnd, classNameBuf, MAX_PATH);
std::string className(&classNameBuf[0]);
if (className != ("MSCTFIME UI") && className != ("IME") && className != ("ConsoleWindowClass")) {
window_handle = hwnd;
return false;
}
}
return true;
}
When I try to compile my project and run it, it gives these errors.:
Error E0289 no instance of constructor "std::basic_string<_Elem, _Traits, _Alloc>::basic_string [with _Elem=char, _Traits=std::char_traits<char>, _Alloc=std::allocator<char>]" matches the argument list
And this one
Error C2664 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string(std::initializer_list<_Elem>,const _Alloc &)': cannot convert argument 1 from 'TCHAR *' to 'std::initializer_list<_Elem>'
Can someone point out what I am doing wrong?
You are calling the
TCHAR
-basedGetClassName()
macro, and you have your project set to defineUNICODE
, thusTCHAR
is mapped towchar_t
, notchar
. You can't create astd::string
from awchar[]
array (well, not the way you are trying to, anyway).So, either:
change your project settings to use the MBCS charset instead of UNICODE, so that
TCHAR
maps tochar
, andGetClassName
maps toGetClassNameA()
.don't use
TCHAR
at all, useGetClassNameA()
directly, eg:if you really want to use
TCHAR
(which you shouldn't - this is not the '90s), you can usestd::basic_string<TCHAR>
instead, just be sure to wrap your string literals with theTEXT()
macro, eg:you can just avoid
std::(basic_)string
altogether and uselstrcmp()
instead to match the sameTCHAR
encoding thatGetClassName()
uses, eg: