Search code examples
c++windowscomole

Parameters construction for OLE interaction using IDispatch::Invoke


I am strudying Microsoft article about OLE automation. As far as I understand AutoWrap function is used to call external methods, get/set properties.

Let's try to comprehend the code calling this function:

//Make it visible (i.e. app.visible = 1)
{
    //parameter preparation
    VARIANT x;
    x.vt = VT_I4;// type of parameter
    x.lVal = 1;// value

    AutoWrap(
        DISPATCH_PROPERTYPUT, // set property
        NULL, // we need no result
        pXlApp,// pointer to Excel app
        L"Visible",// "Visible" property
        1,// 1 argument passed
        x//passed argument
        );
}

What I don't understand is the structure of x:

  1. What is x.vt = VT_I4? Probably it denotes the type of argument. When they pass an array as parameter they write this:
VARIANT arr;
arr.vt = VT_ARRAY | VT_VARIANT;

Looks like a bit mask.

  1. x.lVal = 1; - it means that we pass an integer 1 to Visible function. But what properties must be used for each type of argument? I saw "lVal" and "bstrVal" in the article. For array parameter it is "parray":
VARIANT arr;
//...
arr.parray = SafeArrayCreate(VT_VARIANT, 2, sab);

If we need to call a function with multiple parameters say: MyFun(x y), we need to pass them in reverse order:

VARIANT x;
x.vt = VT_I4;
x.lVal = 1;

VARIANT y;
x.vt = VT_I4;
x.lVal = 2;

AutoWrap(
    DISPATCH_METHOD, NULL, pXlApp, L"MyFun", 2, y, x
    );
  1. Where can I find the rules for parameter construction for any function call?

  2. Am I correct in my investigations?


Solution

  • As far as the parameters expected by some random method you will need to look at the docs for that specific call, or the type library if it's not a documented interface. And realize that most COM objects implement dual interfaces which are much easier for C++ to deal with (dual interfaces allows both IDispatch late binding as well as regular early binding with all of the type checking available for any function call).

    The docs for the VARENUM type tells you what member of the VARIANT needs to be set for any given VT_ value.