June 8th, 2017

What will GetLastError() return after a failed InitOnceExecuteOnce?

The documentation for Init­Once­Execute­Once says

If the function fails, the return value is zero. To get extended error information, call Get­Last­Error.

On the other hand, the documentation for the Init­Once­Callback says

If the function returns FALSE, the block is not marked as initialized and the call to Init­Once­Execute­Once fails. To communicate additional error information, call Set­Last­Error. before returning FALSE.

The second paragraph implies that the Init­Once­Execute­Once function does not itself call Get­Last­Error, because if it did, then that would wipe out the value set by the callback. Is that really the case?

Yes, that’s really the case. The remark in the first paragraph about calling Get­Last­Error presupposes that your callback uses Get­Last­Error to report why it wasn’t able to initialize.

I agree, however, that the documentation is misleading here.

But if your callback wants to return status information more complex than a single “success/failed”, you are probably better served by the synchronous two-phase initialization pattern, which makes it a lot easier to return more complex information. You can even use it to throw a C++ exception.

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.

Feedback