Olaseni
Olaseni

Reputation: 7916

Plain C++ Code to Invoke COM Method with parameter

I have been able to create an instance of SharePoint.OpenDocuments.1 ActiveX Control like so:

CLSID clsid;
HRESULT hResult; 
IDispatch *pWApp;
LPCOLESTR strPid = L"SharePoint.OpenDocuments.1";

CoInitialize(NULL);  
hResult = CLSIDFromProgID(strPid, &clsid);
if(SUCCEEDED(hResult))
    hResult = CoCreateInstance(clsid, NULL, CLSCTX_ALL , IID_IDispatch, (void **)&pWApp);

I have some trouble invoking the "EditDocument" method with a document name. I can't figure out how to invoke or use Variants.

Any code tips?

Upvotes: 1

Views: 169

Answers (2)

yms
yms

Reputation: 10418

If you are using ATL in your C++ project, you can easily create a BSTR string by using the class CComBSTR and pass it as parameter to OpenDocuments.EditDocuments

CComBSTR tempBstr = _T("c:\\myfolder\\myfile.txt");
someObj->SomeMethodThatUsesBSTR(tempBstr);

If you are not using ATL, you can then use the class bstr_t from comutil.h the same way:

bstr_t tempBstr = _T("c:\\myfolder\\myfile.txt");
someObj->SomeMethodThatUsesBSTR(tempBstr.GetBSTR());

Both classes (CComBSTR and bstr_t) are just wrappers that will call SysAllocString and SysFreeString internally.

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490108

At least if I'm reading the docs correctly, you need a BSTR, which you can create with SysAllocString.

Upvotes: 2

Related Questions