STDMETHODIMP CallMethod(LPDISPATCH pDisp, LPOLESTR pszName, VARIANT* pvResult, UINT cArgs, VARIANTARG* rgVarParams)
{
if (NULL == pDisp) return E_POINTER;
DISPID dwDispID;
DISPPARAMS dispparams = {NULL, NULL, 0, 0};
dispparams.rgvarg = rgVarParams;
dispparams.cArgs = cArgs;
HRESULT hr = pDisp->GetIDsOfNames(IID_NULL, &pszName, 1, LOCALE_USER_DEFAULT, &dwDispID);
if(SUCCEEDED(hr)) {
hr = pDisp->Invoke(dwDispID, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_METHOD, &dispparams, pvResult, NULL, NULL);
if (FAILED(hr)) {
hr = pDisp->Invoke(dwDispID, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_METHOD | DISPATCH_PROPERTYGET, &dispparams, pvResult, NULL, NULL);
}
}
return hr;
}
VARIANT vtCmdBars; VariantInit(&vtCmdBars);
VARIANT vtCmdBar; VariantInit(&vtCmdBar);
VARIANT vtParam; VariantInit(&vtParam);
HRESULT hr;
// Look for our command bar -- this prevents us from making a new one
// if the old one was never deleted.
vtParam.vt = VT_BSTR; vtParam.bstrVal = ::SysAllocString(L"MyBar");
hr = CallMethod(vtCmdBars.pdispVal, L"Item", &vtCmdBar, 1, &vtParam);
VariantClear(&vtParam);
// If we can't find it, make a new bar...
if (FAILED(hr)) {
vtParam.vt = VT_BSTR; vtParam.bstrVal = ::SysAllocString(L"MyBar");
hr = CallMethod(vtCmdBars.pdispVal, L"Add", &vtCmdBar, 1, &vtParam);
VariantClear(&vtParam);
if (FAILED(hr)) goto cleanup;
}