Do you want to translate the Delphi function into a C++ function? Delphi's compiler hides lots of tedious details and makes Automation easier. You will need to write hundreds of lines to achieve the same functionality using C++ with COM and plain APIs.
You may want to look into the example from Microsoft first.
CppAutomatePowerPoint[
^]. Also, you might be interested in
http://msdn.microsoft.com/en-us/library/aa155776(v=office.10).aspx[
^]. This one is old but it's still worth reading.
I just rewrote the first part of your codes. But it should help you grab the idea a bit.
CoInitialize(NULL);
CLSID ClassID;
CLSIDFromProgID(TEXT("PowerPoint.Application"), &ClassID);
IUnknown *pUnk;
HRESULT hr = CoCreateInstance(ClassID, NULL, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pUnk);
if (hr != S_OK)
return 0;
IDispatch *pDisp;
hr = pUnk->QueryInterface(IID_IDispatch, (void**)&pDisp);
if (hr != S_OK)
{
pUnk->Release();
return 0;
}
OLECHAR* szPresentations = OLESTR("Presentations");
DISPID dispid_Presentations;
hr = pDisp->GetIDsOfNames(IID_NULL, &szPresentations, 1, LOCALE_USER_DEFAULT, &dispid_Presentations);
if (hr != S_OK)
{
pDisp->Release();
pUnk->Release();
return 0;
}
DISPPARAMS dpNoArgs = {NULL, NULL, 0, 0};
VARIANT vResult;
IDispatch *pDispPresentations;
hr = pDisp->Invoke(dispid_Presentations, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dpNoArgs, &vResult, NULL, NULL);
if (hr != S_OK)
{
pDisp->Release();
pUnk->Release();
return 0;
}
pDispPresentations = vResult.pdispVal;
VARIANT vArgsOpen[4];
for (int i = 0; i < 4; ++i)
{
VariantInit(vArgsOpen+i);
}
vArgsOpen[0].vt = VT_BSTR;
vArgsOpen[0].bstrVal = SysAllocString(L"I need to go out for lunch.ppt");;
vArgsOpen[1].vt = VT_BOOL;
vArgsOpen[1].boolVal = FALSE;
vArgsOpen[2].vt = VT_BOOL;
vArgsOpen[2].boolVal = TRUE;
vArgsOpen[3].vt = VT_BOOL;
vArgsOpen[3].boolVal = FALSE;
DISPPARAMS dpOpen;
dpOpen.cArgs = 4;
dpOpen.cNamedArgs = 0;
dpOpen.rgvarg = vArgsOpen;
IDispatch *pDispPresentation;
OLECHAR* szOpen = OLESTR("Open");
DISPID dispid_Open;
hr = pDispPresentations->GetIDsOfNames(IID_NULL, &szOpen, 1, LOCALE_USER_DEFAULT, &dispid_Open);
if (hr != S_OK)
{
pDisp->Release();
pUnk->Release();
pDispPresentations->Release();
return 0;
}
hr = pDispPresentations->Invoke(dispid_Open, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dpOpen, &vResult, NULL, NULL);
SysFreeString(vArgsOpen[0].bstrVal);
if (hr != S_OK)
{
pDisp->Release();
pUnk->Release();
pDispPresentations->Release();
return 0;
}
pDispPresentation = vResult.pdispVal;
pDispPresentation->Release();
pDisp->Release();
pUnk->Release();
pDispPresentations->Release();
CoUninitialize();
return 1;