summaryrefslogtreecommitdiff
path: root/cli_ure/source/native/native_bootstrap.cxx
blob: 60c5c783dc8dbaddcdb53097283bb6a4f8acf84c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: native_bootstrap.cxx,v $
 * $Revision: 1.14 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// We are using the Windows UNICODE API
#define _UNICODE
#define UNICODE

#ifdef _MSC_VER
#pragma warning(push, 1)
#endif
#include <windows.h>
#include "uno/environment.hxx"
#ifdef _MSC_VER
#pragma warning(pop)
#endif

#include <tchar.h>

#include "native_share.h"

#include "rtl/bootstrap.hxx"
#include "com/sun/star/uno/XComponentContext.hpp"
#include "cppuhelper/bootstrap.hxx"
#include <delayimp.h>
#include <stdio.h>

using namespace ::rtl;
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;

#define OFFICE_LOCATION_REGISTRY_KEY L"Software\\OpenOffice.org\\Layer\\URE\\1"
#define UREINSTALLLOCATION L"UREINSTALLLOCATION"
#define URE_BIN L"\\bin"

namespace
{


//Returns the path to the URE/bin folder.
//The caller must free the returned string with delete[]
wchar_t * getUnoPath()
{
    wchar_t *  theUnoPath = NULL;
    bool failed = false;
    HKEY    hKey = 0;
    if (RegOpenKeyEx(HKEY_CURRENT_USER,OFFICE_LOCATION_REGISTRY_KEY,
        0, KEY_READ, &hKey) != ERROR_SUCCESS)
    {
        if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, OFFICE_LOCATION_REGISTRY_KEY,
            0, KEY_READ, &hKey) != ERROR_SUCCESS)
        {
#if OSL_DEBUG_LEVEL >= 2
            fprintf(stderr, "cli_cppuhelper: Office not properly installed. "
                "Could not open registry keys.");
#endif
            failed = true;
        }
    }
    if (! failed)
    {
        DWORD   dwType = 0;
        DWORD   dwLen = 0;
        wchar_t *arData = NULL;
        //get the length for the path to office
        if (RegQueryValueEx(hKey, UREINSTALLLOCATION, NULL, &dwType, NULL,
            &dwLen) == ERROR_SUCCESS)
        {
            arData = new  wchar_t[dwLen];
            arData[0] = '\0';
            if (RegQueryValueEx(hKey, UREINSTALLLOCATION, NULL, &dwType, (LPBYTE) arData,
                & dwLen) == ERROR_SUCCESS)
            {
                int test = lstrlen(URE_BIN);
                //attach the bin directory to the URE path
                int sizePath = lstrlen(arData) + lstrlen(URE_BIN) + 1;
                theUnoPath = new wchar_t[sizePath];
                 theUnoPath[0] = '\0';
                 lstrcat(theUnoPath, arData);
                 lstrcat(theUnoPath, URE_BIN);
                delete[] arData;
#if OSL_DEBUG_LEVEL >=2
                fprintf(stdout,"[cli_cppuhelper]: Using path %S to load office libraries.", theUnoPath);
#endif
            }
        }
        RegCloseKey(hKey);
    }
    return theUnoPath;
}


//Returns the path to the Ure/bin directory and expands the PATH by inserting the
// ure/bin path at the front.
wchar_t const * getUreBinPathAndSetPath()
{
    static wchar_t * theBinPath = NULL;

    if (theBinPath)
        return theBinPath;

    wchar_t * unoPath = getUnoPath();
    if (!unoPath)
        return NULL;

    //We extend the path to contain the program directory of the office,
    //so that components can use osl_loadModule with arguments, such as
    //"reg3.dll". That is, the arguments are only the library names.

    wchar_t * sEnvPath = NULL;
    DWORD  cChars = GetEnvironmentVariable(L"PATH", sEnvPath, 0);
    if (cChars > 0)
    {
        sEnvPath = new wchar_t[cChars];
        cChars = GetEnvironmentVariable(L"PATH", sEnvPath, cChars);
        //If PATH is not set then it is no error
        if (cChars == 0 && GetLastError() != ERROR_ENVVAR_NOT_FOUND)
        {
            delete[] sEnvPath;
            return NULL;
        }
    }
    //prepare the new PATH. Add the Ure/bin directory at the front
    wchar_t * sNewPath = new wchar_t[lstrlen(sEnvPath) + lstrlen(unoPath) + 2];
    sNewPath[0] = '\0';
    lstrcat(sNewPath, unoPath);
    if (lstrlen(sEnvPath))
    {
        lstrcat(sNewPath, L";");
        lstrcat(sNewPath, sEnvPath);
    }

    BOOL bSet = SetEnvironmentVariable(L"PATH", sNewPath);

    theBinPath = unoPath;
    delete[] sEnvPath;
    delete[] sNewPath;

    return theBinPath;
}

HMODULE loadFromPath(wchar_t const * sLibName)
{
    if (sLibName == NULL)
        return NULL;

    wchar_t const * binPath =  getUreBinPathAndSetPath();
    if (!binPath)
        return NULL;


    wchar_t*  sFullPath = new wchar_t[lstrlen(sLibName) + lstrlen(binPath) + 2];
    sFullPath[0] = '\0';
    sFullPath = lstrcat(sFullPath, binPath);
    sFullPath = lstrcat(sFullPath, L"\\");
    sFullPath = lstrcat(sFullPath, sLibName);
    HMODULE handle = LoadLibraryEx(sFullPath, NULL,
        LOAD_WITH_ALTERED_SEARCH_PATH);
    delete[] sFullPath;
    return handle;

}

//Hook for delayed loading of libraries which this library is linked with.
extern "C"  FARPROC WINAPI delayLoadHook(
    unsigned        dliNotify,
    PDelayLoadInfo  pdli
    )
{
    if (dliNotify == dliFailLoadLib)
    {
        //Convert the ansi file name to wchar_t*
        int size = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, pdli->szDll, -1, NULL, 0);
        if (size > 0)
        {
            wchar_t * buf = new wchar_t[size];
            if (MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, pdli->szDll, -1, buf, size))
            {
                HMODULE handle = NULL;
                return (FARPROC) loadFromPath(buf);
            }
        }
    }
    return 0;
}
}

ExternC
PfnDliHook   __pfnDliFailureHook2 = delayLoadHook;

namespace uno
{
namespace util
{

/** Bootstrapping native UNO.

    Bootstrapping requires the existence of many libraries which are contained
    in an URE installation. To find and load these libraries the Windows
    registry keys HKEY_CURRENT_USER\Software\OpenOffice.org\Layer\URE\1
    and HKEY_LOCAL_MACHINE\Software\OpenOffice.org\Layer\URE\1 are examined.
    These contain a named value UREINSTALLLOCATION which holds a path to the URE
    installation folder.
*/
public __sealed __gc class Bootstrap
{
    inline Bootstrap() {}

public:

    /** Bootstraps the initial component context from a native UNO installation.

        @see cppuhelper/bootstrap.hxx:defaultBootstrap_InitialComponentContext()
    */
    static ::unoidl::com::sun::star::uno::XComponentContext *
        defaultBootstrap_InitialComponentContext();

    /** Bootstraps the initial component context from a native UNO installation.

        @param ini_file
               a file URL of an ini file, e.g. uno.ini/unorc. (The ini file must
               reside next to the cppuhelper library)
        @param bootstrap_parameters
               bootstrap parameters (maybe null)

        @see cppuhelper/bootstrap.hxx:defaultBootstrap_InitialComponentContext()
    */
    static ::unoidl::com::sun::star::uno::XComponentContext *
        defaultBootstrap_InitialComponentContext(
            ::System::String * ini_file,
            ::System::Collections::IDictionaryEnumerator *
              bootstrap_parameters );

    /** Bootstraps the initial component context from a native UNO installation.

    @see cppuhelper/bootstrap.hxx:bootstrap()
     */
    static ::unoidl::com::sun::star::uno::XComponentContext *
    bootstrap();
};

//______________________________________________________________________________
::unoidl::com::sun::star::uno::XComponentContext *
Bootstrap::defaultBootstrap_InitialComponentContext(
    ::System::String * ini_file,
    ::System::Collections::IDictionaryEnumerator * bootstrap_parameters )
{
    if (0 != bootstrap_parameters)
    {
        bootstrap_parameters->Reset();
        while (bootstrap_parameters->MoveNext())
        {
            OUString key(
                String_to_ustring( __try_cast< ::System::String * >(
                                       bootstrap_parameters->get_Key() ) ) );
            OUString value(
                String_to_ustring( __try_cast< ::System::String * >(
                                       bootstrap_parameters->get_Value() ) ) );

            ::rtl::Bootstrap::set( key, value );
        }
    }

    // bootstrap native uno
    Reference< XComponentContext > xContext;
    if (0 == ini_file)
    {
        xContext = ::cppu::defaultBootstrap_InitialComponentContext();
    }
    else
    {
        xContext = ::cppu::defaultBootstrap_InitialComponentContext(
            String_to_ustring( __try_cast< ::System::String * >( ini_file ) ) );
    }

    return __try_cast< ::unoidl::com::sun::star::uno::XComponentContext * >(
        to_cli( xContext ) );
}

//______________________________________________________________________________
::unoidl::com::sun::star::uno::XComponentContext *
Bootstrap::defaultBootstrap_InitialComponentContext()
{
    return defaultBootstrap_InitialComponentContext( 0, 0 );
}

::unoidl::com::sun::star::uno::XComponentContext * Bootstrap::bootstrap()
{
    Reference<XComponentContext> xContext = ::cppu::bootstrap();
    return __try_cast< ::unoidl::com::sun::star::uno::XComponentContext * >(
        to_cli( xContext ) );

}

}
}