January 23rd, 2006

If your callback fails, it's your responsibility to set the error code

There are many cases where a callback function is allowed to halt an operation. For example, you might decide to return FALSE to the WM_NCCREATE message to prevent the window from being created, or you might decide to return FALSE to one of the many enumeration callback functions such as the EnumWindowsProc callback. When you do this, the enclosing operation will return failure back to its caller: the CreateWindow function returns NULL; the EnumWindows function returns FALSE. Of course, when this happens, the enclosing operation doesn’t know why the callback failed; all it knows is that it failed. Consequently, it can’t set a meaningful value to be retrieved by the GetLastError function. If you want something meaningful to be returned by the GetLastError function when your callback halts the operation, it’s the callback’s responsibility to set that value by calling the SetLastError function.

This is something that is so obvious I didn’t think it needed to be said; it falls into the “because computers aren’t psychic (yet)” category of explanation. But apparently it wasn’t obvious enough, so now I’m saying it.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.