I'm using the following code inside a global CBT hook procedure:
TCHAR title[256];
int getT = GetWindowText(hWnd, title, 256);
if (getT == 0) {
int err = GetLastError();
logFile << "Error GetWindowText(): " << err << endl;
} else {
logFile << "getT = " << getT << endl;
}
The problem is that for certain windows the GetWindowText() function works just fine and I get the correct window title, but for some others it returns 0 and I get an empty string. The GetLastError() returns 183 which is ERROR_ALREADY_EXISTS:
Cannot create a file when that file already exists.
The error is not random: I always get it with the same kind of window opened by the same application, but for all the other windows it seems to work fine.
SetLastError(666)before callingGetWindowText. If you will start getting 666 back, it may meanGetWindowTextis intercepted with an api hook which prevents reading a certain window and doesn't bother setting a proper error code.