Pelles C forum

C language => Tips & tricks => Topic started by: TimoVJL on November 11, 2015, 08:28:04 am

Title: LibreOffice / OpenOffice COM example
Post by: TimoVJL on November 11, 2015, 08:28:04 am
here is silly LibreOffice / OpenOffice COM example
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <windows.h>
//#define INITGUID
#include <ole2.h>

#pragma comment(lib, "user32.lib")
#pragma comment(lib, "oleaut32.lib")
#pragma comment(lib, "ole32.lib")
//#pragma comment(lib, "uuid.lib")

#pragma warn(disable: 2216 2118)

// http://www.oooforum.org/forum/viewtopic.phtml?t=9815

/*
program TestOO1;
uses
  SysUtils, Variants, ComObj;
  var
    objServiceManager: Variant;
    Stardesktop: Variant;
    doc: Variant;
    text: Variant;
begin
  objServiceManager := CreateOleObject('com.sun.star.ServiceManager');
  Stardesktop := objServiceManager.createInstance('com.sun.star.frame.Desktop');
  doc := Stardesktop.LoadComponentFromURL('private:factory/swriter', '_blank', 0, VarArrayCreate([0, - 1], varVariant));
  text := doc.getText();
  text.setString('Hello World');
  //doc.Close(true);
end.
*/
// {82154420-0FBF-11d4-8313-005004526AB4}
const CLSID GUID_NULL = {0};
const GUID IID_IDispatch = {0x00020400, 0, 0, {0xC0, 0, 0, 0, 0, 0, 0, 0x46}};

IDispatch *OOGetServiceManager(void);
IDispatch* OOGetDesktop(IDispatch *pSvcMgr);
IDispatch *OOOpenDocument(IDispatch *pStardesktop);
IDispatch *OOGetText(IDispatch *pDocument);
IDispatch *OOGetTextPos(IDispatch *pText, WCHAR *wString);
HRESULT OOSetString(IDispatch *pText, WCHAR *wString);
HRESULT OOInsetertString(IDispatch *pText, IDispatch *pTextPos, WCHAR *wString, BOOL bAbsorb);

int main(int argc, char **argv)
{
//CLSID clsid; // Holds CLSID of server after CLSIDFromProgID.
//HRESULT hr; // General error/result holder.
IDispatch *pSvcMgr = NULL; // Main IDispatch pointer.
IDispatch *pStardesktop = NULL;
IDispatch *pDocument = NULL;
IDispatch *pText = NULL;
IDispatch *pTextPos = NULL;

// Initialize OLE Libraries.
OleInitialize(NULL);
pSvcMgr = OOGetServiceManager();
if (pSvcMgr) {
pStardesktop = OOGetDesktop(pSvcMgr);
if (!pStardesktop)
MessageBox(NULL, "Couldn't get Stardesktop", "Error", MB_SETFOREGROUND);
else {
pDocument = OOOpenDocument(pStardesktop);
if (pDocument) {
pText = OOGetText(pDocument);
if (pText) {
OOSetString(pText, L"\nHello World\tto all.");
pTextPos = OOGetTextPos(pText, L"getEnd");
OOSetString(pTextPos, L"\nAnother line.");
pTextPos->lpVtbl->Release(pTextPos);
pTextPos = OOGetTextPos(pText, L"getEnd");
OOSetString(pTextPos, L"\nAnother line.");
//pTextPos->lpVtbl->Release(pTextPos);
OOInsetertString(pText, pTextPos, L"\ninsert Another line.", FALSE);
pTextPos->lpVtbl->Release(pTextPos);
pText->lpVtbl->Release(pText);
}
pDocument->lpVtbl->Release(pDocument);
}
}
pSvcMgr->lpVtbl->Release(pSvcMgr);
}
OleUninitialize();
return 0;
}

IDispatch *OOGetServiceManager(void)
{
CLSID clsid; // Holds CLSID of server after CLSIDFromProgID.
HRESULT hr; // General error/result holder.
IDispatch *pSvcMgr = NULL; // Main IDispatch pointer.
hr = CLSIDFromProgID(L"com.sun.star.ServiceManager", &clsid);
if (FAILED(hr)) {
OleUninitialize();
MessageBox(NULL, "com.sun.star.ServiceManager not registered.", "Error", MB_SETFOREGROUND);
return NULL;
}
hr = CoCreateInstance(&clsid, NULL, CLSCTX_LOCAL_SERVER, &IID_IDispatch, (void **)&pSvcMgr);
if (FAILED(hr)) {
OleUninitialize();
MessageBox(NULL, "Couldn't start LibreOffice", "Error", MB_SETFOREGROUND);
return NULL;
}
return pSvcMgr;
}

IDispatch *OOGetDesktop(IDispatch *pSvcMgr)
{
DISPID dispID;
HRESULT hr;
IDispatch *pStardesktop = NULL;
wchar_t *wszName = L"createInstance";
if (!pSvcMgr) return NULL;
hr = pSvcMgr->lpVtbl->GetIDsOfNames(pSvcMgr, &IID_NULL, &wszName, 1, LOCALE_USER_DEFAULT, &dispID);
if (!hr) {
DISPPARAMS dp; // = { NULL, NULL, 0, 0 };
VARIANT vResult;
VariantInit(&vResult);
VARIANT vParam1;
VariantInit(&vParam1);
vParam1.vt = VT_BSTR;
vParam1.bstrVal = SysAllocString( L"com.sun.star.frame.Desktop");
dp.cNamedArgs = 0;
dp.rgdispidNamedArgs = NULL;
dp.cArgs = 1;
dp.rgvarg = &vParam1;
hr = pSvcMgr->lpVtbl->Invoke(pSvcMgr, dispID, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &dp, &vResult, NULL, NULL);
if (!hr)
pStardesktop = vResult.pdispVal;
SysFreeString(vParam1.bstrVal);
}
return pStardesktop;
}

IDispatch *OOOpenDocument(IDispatch *pStardesktop)
{
DISPID dispID;
HRESULT hr;
IDispatch *pDocument = NULL;
wchar_t *wszName = L"loadComponentFromURL";
if (!pStardesktop) return NULL;
hr = pStardesktop->lpVtbl->GetIDsOfNames(pStardesktop, &IID_NULL, &wszName, 1, LOCALE_USER_DEFAULT, &dispID);
if (!hr) {
DISPPARAMS dp; // = { NULL, NULL, 0, 0 };
VARIANT vResult;
VariantInit(&vResult);
VARIANT vParams[4];
VariantInit(&vParams[3]); // opposite order
vParams[3].vt = VT_BSTR;
vParams[3].bstrVal = SysAllocString( L"private:factory/swriter");
VariantInit(&vParams[2]);
vParams[2].vt = VT_BSTR;
vParams[2].bstrVal = SysAllocString( L"_blank");
VariantInit(&vParams[1]);
vParams[1].vt = VT_I4;
vParams[1].bstrVal = 0;
VariantInit(&vParams[0]);
vParams[0].vt = VT_ARRAY | VT_VARIANT;
vParams[0].parray = 0;
dp.cNamedArgs = 0;
dp.rgdispidNamedArgs = NULL;
dp.cArgs = 4;
dp.rgvarg = &vParams[0];
hr = pStardesktop->lpVtbl->Invoke(pStardesktop, dispID, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &dp, &vResult, NULL, NULL);
if (!hr)
pDocument = vResult.pdispVal;
SysFreeString(vParams[0].bstrVal);
SysFreeString(vParams[1].bstrVal);
}
return pDocument;
}

IDispatch *OOGetText(IDispatch *pDocument)
{
DISPID dispID;
HRESULT hr;
IDispatch *pText = NULL;
wchar_t *wszName = L"getText";
if (!pDocument) return NULL;
hr = pDocument->lpVtbl->GetIDsOfNames(pDocument, &IID_NULL, &wszName, 1, LOCALE_USER_DEFAULT, &dispID);
if (!hr) {
DISPPARAMS dp;
VARIANT vResult;
VariantInit(&vResult);
dp.cNamedArgs = 0;
dp.rgdispidNamedArgs = NULL;
dp.cArgs = 0;
dp.rgvarg = NULL;
hr = pDocument->lpVtbl->Invoke(pDocument, dispID, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &dp, &vResult, NULL, NULL);
if (!hr)
pText = vResult.pdispVal;
}
return pText;
}

IDispatch *OOGetTextPos(IDispatch *pText, WCHAR *wString)
{
DISPID dispID;
HRESULT hr;
IDispatch *pTextRange = NULL;
if (!pText) return NULL;
hr = pText->lpVtbl->GetIDsOfNames(pText, &IID_NULL, &wString, 1, LOCALE_USER_DEFAULT, &dispID);
if (!hr) {
DISPPARAMS dp;
VARIANT vResult;
VariantInit(&vResult);
dp.cNamedArgs = 0;
dp.rgdispidNamedArgs = NULL;
dp.cArgs = 0;
dp.rgvarg = NULL;
hr = pText->lpVtbl->Invoke(pText, dispID, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &dp, &vResult, NULL, NULL);
if (!hr)
pTextRange = vResult.pdispVal;
}
return pTextRange;
}

HRESULT OOSetString(IDispatch *pText, WCHAR *wString)
{
DISPID dispID;
HRESULT hr = -1;
wchar_t *wszName = L"setString";
if (!pText) return hr;
hr = pText->lpVtbl->GetIDsOfNames(pText, &IID_NULL, &wszName, 1, LOCALE_USER_DEFAULT, &dispID);
if (!hr) {
DISPPARAMS dp;
VARIANT vResult;
VariantInit(&vResult);
VARIANT vParam1;
VariantInit(&vParam1);
vParam1.vt = VT_BSTR;
vParam1.bstrVal = SysAllocString(wString);
dp.cNamedArgs = 0;
dp.rgdispidNamedArgs = NULL;
dp.cArgs = 1;
dp.rgvarg = &vParam1;
hr = pText->lpVtbl->Invoke(pText, dispID, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &dp, &vResult, NULL, NULL);
SysFreeString(vParam1.bstrVal);
}
return hr;
}

HRESULT OOInsetertString(IDispatch *pText, IDispatch *pTextPos, WCHAR *wString, BOOL bAbsorb)
{
DISPID dispID;
HRESULT hr = -1;
wchar_t *wszName = L"insertString";
if (!pText) return hr;
hr = pText->lpVtbl->GetIDsOfNames(pText, &IID_NULL, &wszName, 1, LOCALE_USER_DEFAULT, &dispID);
if (!hr) {
DISPPARAMS dp;
VARIANT vResult;
VariantInit(&vResult);
VARIANT vParams[3];
VariantInit(&vParams[2]);
vParams[2].vt = VT_DISPATCH;
vParams[2].pdispVal = pTextPos;
VariantInit(&vParams[1]);
vParams[1].vt = VT_BSTR;
vParams[1].bstrVal = SysAllocString(wString);
VariantInit(&vParams[0]);
vParams[0].vt = VT_BOOL;
vParams[0].boolVal = bAbsorb;
dp.cNamedArgs = 0;
dp.rgdispidNamedArgs = NULL;
dp.cArgs = 3;
dp.rgvarg = &vParams[0];
hr = pText->lpVtbl->Invoke(pText, dispID, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &dp, &vResult, NULL, NULL);
SysFreeString(vParams[1].bstrVal);
}
return hr;
}
Code: [Select]
pTextPos = OOGetTextPos(pText, L"createTextCursor");
for (int i = 1; i < 5; i++) {
WCHAR wText[30];
wsprintfW(wText, L"Line %d\n", i);
OOInsetertString(pText, pTextPos, wText, FALSE);
}
pTextPos->lpVtbl->Release(pTextPos);
Have anyone else examples?