Reputation: 1
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?
Upvotes: 0
Views: 135
Reputation: 596186
You are calling the TCHAR
-based GetClassName()
macro, and you have your project set to define UNICODE
, thus TCHAR
is mapped to wchar_t
, not char
. You can't create a std::string
from a wchar[]
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 to char
, and GetClassName
maps to GetClassNameA()
.
don't use TCHAR
at all, use GetClassNameA()
directly, eg:
CHAR classNameBuf[MAX_PATH];
GetClassNameA(hwnd, classNameBuf, MAX_PATH);
std::string className(classNameBuf);
if (className != "MSCTFIME UI" && ...)
if you really want to use TCHAR
(which you shouldn't - this is not the '90s), you can use std::basic_string<TCHAR>
instead, just be sure to wrap your string literals with the TEXT()
macro, eg:
TCHAR classNameBuf[MAX_PATH] = {};
GetClassName(hwnd, classNameBuf, MAX_PATH);
std::basic_string<TCHAR> className(classNameBuf);
if (className != TEXT("MSCTFIME UI") && ...)
you can just avoid std::(basic_)string
altogether and use lstrcmp()
instead to match the same TCHAR
encoding that GetClassName()
uses, eg:
TCHAR classNameBuf[MAX_PATH] = {};
GetClassName(hwnd, classNameBuf, MAX_PATH);
if (lstrcmp(classNameBuf, TEXT("MSCTFIME UI")) != 0 && ...)
Upvotes: 1