4

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);
Adam Tegen
  • 25,378
  • 33
  • 125
  • 153

2 Answers2

7

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: "How to pass optional parameters when you call a function in Visual C++"

zett42
  • 25,437
  • 3
  • 35
  • 72
m-sharp
  • 16,443
  • 1
  • 26
  • 26
-1

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
  • 131,367
  • 29
  • 160
  • 239