views:

322

answers:

2

I have an MFC wrapper over a COM object. There is a function that takes a large number of options, which are mostly optional. How do I pass some arguments but not others?

For what it's worth, the optional arguments are listed as VARIANT*.

Below is the code

CComVariant vFalse = false;
CApplication application;

{
    application.CreateDispatch(_T("Word.Application"));

    CDocuments documents = application.get_Documents();       

    CComVariant vFilename = _T("c:\\temp\\test.rtf");
    CComVariant vNothing;
    CComVariant vEmpty = _T("");
    CComVariant vOpenFormat = 0;
    application.put_Visible(TRUE);

    //
    // THIS FUNCTION has a number of optional arguments
    //
    LPDISPATCH pDocument = documents.Open(&vFilename, &vFalse, &vFalse, &vFalse, &vEmpty, &vEmpty, &vFalse, &vEmpty, &vEmpty, &vOpenFormat, &vOpenFormat, &vFalse, &vFalse, &vOpenFormat, &vFalse, &vFalse);
}
application.Quit(&vFalse, NULL, NULL);
A: 

An unspecified variant is normally VT_EMPTY:

_variant_t vtEmpty(VT_EMPTY);

You obviously have written the CDocuments and CApplication wrappers around the COM interfaces, so you could specify the optional parameters as having default value of vtEmpty.

1800 INFORMATION
+1  A: 

To skip an optional parameter in a COM method pass a VARIANT of type VT_ERROR and the error code must by DISP_E_PARAMNOTFOUND.

CComVariant vtOptional;
vtOptional.vt = VT_ERROR;
vtOptional.scode = DISP_E_PARAMNOTFOUND;

Now you can use vtOptional as a parameter you don't want to specify if the parameter is optional.

Here is the official word on this: http://support.microsoft.com/kb/238981

m-sharp