2

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.

9
  • Are you attempting to retrieve the text of an edit control in another application? Commented Aug 15, 2011 at 9:32
  • No, I'm trying to get the title of a window Commented Aug 15, 2011 at 9:35
  • Is this a window we will have on our systems that we can test against? Commented Aug 15, 2011 at 9:39
  • 3
    Try to call SetLastError(666) before calling GetWindowText. If you will start getting 666 back, it may mean GetWindowText is intercepted with an api hook which prevents reading a certain window and doesn't bother setting a proper error code. Commented Aug 15, 2011 at 9:48
  • 1
    Maybe their table windows is an edit control, just modified enough to appear as a regular window. Or it might be just mimicing a real window and not using SetWindowText() at all. What does Spy++ tell you about the window? Commented Aug 15, 2011 at 9:52

1 Answer 1

2

You might not have the rights to retrieve text from certain windows on Windows Vista and above.

My guess is that ERROR_ALREADY_EXISTS comes from your log file when you print "Error GetWindowText(): ". You should get the error code first before doing anything else.

Another possibility is that the window returns 0 from its WM_GETTEXT handler without setting the last error. As GetWindowText documentation states, if you call it on a window belonging to the same process, it retrieves the text by sending this message. Since you are calling the function from a hook, you might be in the same process.

Sign up to request clarification or add additional context in comments.

3 Comments

Edited the code to retrieve the error before writing on file, and it still returns the same strange error. Also, I'm on Win XP.
You are calling this from a hook, right? If the window is in the same process, then window text is retrieved by sending the WM_GETTEXT message, which might be returning 0 without setting the last error. Would GetLastError return 183, if you call it before GetWindowText?
After over 10 years, here I am hitting this same issue. To debug, I called SetLastError(ERROR_SUCCESS) right before the call to GetWindowText() to ensure it is clear, and the result is that, as the window handle points to own process, WM_GETTEXT gets called, returns a zero-length string, and never really updates the last error (as there was no last error anyway).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.