Enumerate COM object (IDispatch) methods using ATL?

前端 未结 3 1711
我在风中等你
我在风中等你 2020-12-05 21:24

Using ATL (VS2008) how can I enumerate the available methods available on a given IDispatch interface (IDispatch*)? I need to search for a method with a specif

3条回答
  •  甜味超标
    2020-12-05 21:56

    You can't enumerate all the available methods unless the object implements IDispatchEx.

    However, if you know the name of the method you want to call, you can use GetIDsOfNames to map the name to the proper DISPID.

    HRESULT hr;
    CComPtr dispatch;
    DISPID dispid;
    WCHAR *member = "YOUR-FUNCTION-NAME-HERE";
    DISPPARAMS* dispparams;
    
    // Get your pointer to the IDispatch interface on the object here.  Also setup your params in dispparams.
    
    hr = dispatch->GetIDsOfNames(IID_NULL, &member, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
    if (SUCCEEDED(hr)) {
      hr = dispatch->Invoke(1, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, dispparams, &varResult, NULL, NULL);
    }
    

    Edit: For completeness, I suspect there is a way to interrogate the ITypeInfo2 interface (assuming there is a type library for the object) that you get from IDispatch::GetTypeInfo for a list of methods, but I've not done it. See the other answer.

提交回复
热议问题