January 29th, 2014

One of my favorite error codes: Optional parameter missing

The error Optional parameter missing sounds awfully paradoxical, doesn’t it. I mean, if the parameter is optional, why are you complaining that it’s missing? This KB article explains why, specifically, the part that says, “If a parameter is omitted, the calling program must…”. For those who don’t want to click through, here’s the deal: Methods described in a type library can declare parameters as optional. Optional parameters must come at the end of the parameter list, of course. The catch is how you programmatically invoke a method that contains optional parameters. If you want to call a method that has optional parameters, you still have to pass the parameters. But you have to pass them a special way so that the method knows that they were omitted. (Specifically, you have to pass them as type VT_ERROR, code DISP_E_PARAM­NOT­FOUND.) In other words, an optional parameter is optional from the high-level language, but it isn’t optional at the low-level interface. Rather the optional keyword instructs the scripting engine to deploy syntactic sugar and allow the script to omit the parameters. The scripting engine then must follow specific rules in providing values for the parameters that were omitted by the script author.

In the lingo of C# and C++, a parameter declared optional in the TLB is really a mandatory parameter whose default value is DISP_E_PARAM­NOT­FOUND.

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.