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?
我在COM对象上有一个MFC包装器。有一个函数需要大量选项,这些选项大多是可选的。我如何传递一些参数但不传递其他参数?
For what it's worth, the optional arguments are listed as VARIANT*.
对于它的价值,可选参数列为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);
2 个解决方案
#1
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.
要跳过COM方法中的可选参数,请传递VT_ERROR类型的VARIANT,错误代码必须通过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.
现在,您可以使用vtOptional作为参数,如果参数是可选的,则不希望指定该参数。
Here is the official word on this: "How to pass optional parameters when you call a function in Visual C++"
以下是关于此的官方说法:“在Visual C ++中调用函数时如何传递可选参数”
#2
An unspecified variant is normally VT_EMPTY:
未指定的变体通常是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.
您显然已经在COM接口周围编写了CDocuments和CApplication包装器,因此您可以将可选参数指定为具有默认值vtEmpty。
#1
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.
要跳过COM方法中的可选参数,请传递VT_ERROR类型的VARIANT,错误代码必须通过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.
现在,您可以使用vtOptional作为参数,如果参数是可选的,则不希望指定该参数。
Here is the official word on this: "How to pass optional parameters when you call a function in Visual C++"
以下是关于此的官方说法:“在Visual C ++中调用函数时如何传递可选参数”
#2
An unspecified variant is normally VT_EMPTY:
未指定的变体通常是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.
您显然已经在COM接口周围编写了CDocuments和CApplication包装器,因此您可以将可选参数指定为具有默认值vtEmpty。