7

The GetWindowText function's return value is documented as follows:

If the function succeeds, the return value is the length, in characters, of the copied string, not including the terminating null character. If the window has no title bar or text, if the title bar is empty, or if the window or control handle is invalid, the return value is zero. To get extended error information, call GetLastError.

Now, when calling this method and receiving a return value of zero, how do I know whether GetLastError would return a sensible value? After all, zero is not only used to indicate failure, but could also mean that the window text is empty, in which case GetLastError would not return an undefined value.

My own ideas:

  • At first, I thought that maybe GetWindowText might set the last error to 0 on success. But testing shows it doesn't (and if it did, I couldn't rely on it anyway, since this isn't documented).
  • Then, I thought that maybe GetWindowText might leave the last error unchanged on success, so setting it to 0 myself before the call would allow me to check whether the last error has changed. Testing shows this might work, but since it's not doumented this way, I can't rely on it. (And I guess this would depend heavily on the concrete circumstances and implementation of GetWindowText.)
  • And, of course, I could first check the window text length using GetWindowTextLength and then call GetWindowText only if the length was greater than 0. However, what if the window changes its text between my calls to GetWindowTextLength and GetWindowText? I, again, couldn't rely on a return value of zero denoting an error.

So, what can I do to definitely decide whether GetWindowText failed?

3
  • 1
    I'm afraid, that the only way to control a window in full degree is to subclass it first and then monitor all possible events related to text changes (if any) and window existence. Without this you can only rely on a half measures, such as you mentioned: cleaning last error flag, reading text length, plus checking the window handle for validity beforehand by means of IsWindow. All of these lacks transactional consistency though. This looks like a flaw in the API.
    – Stan
    Oct 23, 2012 at 10:19
  • Note if the window you are trying to read its text from is in another process, the call will always succeed by design. blogs.msdn.com/b/oldnewthing/archive/2003/08/21/54675.aspx Oct 23, 2012 at 12:10
  • Well, it could still fail because of a bad handle or something, couldn't it? Oct 23, 2012 at 12:18

2 Answers 2

4

SetLastError(ERROR_SUCCESS) beforehand is perfectly safe and supported.

The documentation for last-error codes make it clear that a function may or may not clear the last-error value on success. What is certain, however, is that if a function succeeds, it will either leave the last-error value unchanged, or set it to zero, but never change it to an error value.

The implementation rules for WinAPI functions may be summarized as:

  1. Don't call SetLastError if no error occurs.
  2. If a helper function sets an error code, which does not cause failure of your API, call SetLastError(0) so the non-fatal internal error isn't visible to the caller, or call RestoreLastError and pass whatever GetLastError returned on entry to your API
  3. If your API fails without throwing an SEH exception, call SetLastError to report it (unless a helper function already did).

The documented behavior "some functions call SetLastError with a zero when they succeed" is a consequence of point #2.

5
  • Sounds good, can you point me to a documentation source for point #2? GetWindowText calling an API which sets an error code, but does not cause the failure of GetWindowText was exactly what I was thinking about. Oct 23, 2012 at 14:19
  • @Fabian: It's in the documentation of GetLastError. It mentions that some functions may set the code to 0 on success while others don't set it. Nowhere is any function permitted to set the last error code to any failure code if they succeed.
    – Ben Voigt
    Oct 23, 2012 at 14:24
  • I don't see that the docs for GetLastError require the helper function to either set the last error to 0 or to restore it in case of success. The docs say, "If the function is not documented to set the last-error code, the value [...] is simply the most recent last-error code to have been set; some functions set the last-error code to 0 on success and others do not." Oct 24, 2012 at 7:40
  • The question is what "most recent" means. Most recent before the call of the "function" (that would be good for solving my problem), or just most recent - i.e., might as well have been set by a helper function while executing the called function. Just from the wording, I can't rely on the former, so I must assume the latter. (BTW, the RestoreLastError function is not documented on MSDN, so it's probably not a good idea to use it.) Oct 24, 2012 at 7:41
  • (However, of course, they probably use it internally, so please ignore my comment above about RestoreLastError not being documented.) Oct 24, 2012 at 9:39
1

Yes, you are right, you cannot actually use GetLastError() to see the difference. Not pretty. The only workaround I can think of is to use IsWindow() afterward. If that returns TRUE then a zero return really means "empty string". That, however, doesn't deal with passing a bad buffer pointer, hopefully easily avoided in your code.

2
  • 1
    Perhaps slightly pedantic, but IsWindow is not guaranteed to do what is expected in this case because of handle recycling.
    – Mike Kwan
    Oct 23, 2012 at 11:53
  • 1
    Hmya, this cannot be a consideration. All bets are off anyway if the handle gets recycled before calling GetWindowText(). Oct 23, 2012 at 12:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.