LibreOffice Module odk (master) 1
win/unoapploader.c
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <assert.h>
21#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
24#include <process.h>
25
26#if !defined WIN32_LEAN_AND_MEAN
27# define WIN32_LEAN_AND_MEAN
28#endif
29#include <windows.h>
30
32#include <sal/types.h>
33
34#define MY_SIZE(s) (sizeof (s) / sizeof *(s))
35#define MY_LENGTH(s) (MY_SIZE(s) - 1)
36
37static wchar_t* getPath(void);
38static wchar_t* createCommandLine( wchar_t const * lpCmdLine );
39static FILE* getErrorFile( int create );
40static void writeError( const char* errstr );
41static void closeErrorFile(void);
42
43/*
44 * The main function implements a loader for applications which use UNO.
45 *
46 * <p>This code runs on the Windows platform only.</p>
47 *
48 * <p>The main function detects a UNO installation on the system and adds the
49 * program directory of the UNO installation to the PATH environment variable.
50 * After that, the application process is loaded and started, whereby the
51 * new process inherits the environment of the calling process, including
52 * the modified PATH environment variable. The application's executable name
53 * must be the same as the name of this executable, prefixed by '_'.</p>
54 *
55 * <p>A UNO installation can be specified by the user by setting the UNO_PATH
56 * environment variable to the program directory of the UNO installation.
57 * If no installation is specified by the user, the default installation on
58 * the system will be taken. The default installation is read from the
59 * default value of the key "Software\LibreOffice\UNO\InstallPath" from the
60 * root key HKEY_CURRENT_USER in the Windows Registry. If this key is missing,
61 * the key is read from the root key HKEY_LOCAL_MACHINE.</p>
62 */
63int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
64 LPWSTR lpCmdLine, int nCmdShow )
65{
66 (void) hInstance; /* unused */
67 (void) hPrevInstance; /* unused */
68 (void) nCmdShow; /* unused */
69
70 /* get the path of the UNO installation */
71 wchar_t* path = getPath();
72
73 if ( path != NULL )
74 {
75 wchar_t cmd[
76 MY_LENGTH(L"\"") + MAX_PATH +
77 MY_LENGTH(L"\\unoinfo.exe\" c++")];
78 /* hopefully does not overflow */
79 cmd[0] = L'"';
80 wcscpy(cmd + 1, path);
81 if (wcschr(cmd + 1, L'"') != NULL) {
82 free(path);
83 writeError("Error: bad characters in UNO installation path!\n");
85 return 1;
86 }
87 size_t pathsize = wcslen(cmd);
88 wcscpy(
89 cmd + pathsize,
90 &L"\\unoinfo.exe\" c++"[
91 pathsize == 1 || cmd[pathsize - 1] != L'\\' ? 0 : 1]);
92 SECURITY_ATTRIBUTES sec;
93 sec.nLength = sizeof (SECURITY_ATTRIBUTES);
94 sec.lpSecurityDescriptor = NULL;
95 sec.bInheritHandle = TRUE;
96 HANDLE stdoutRead;
97 HANDLE stdoutWrite;
98 HANDLE temp;
99 if (CreatePipe(&temp, &stdoutWrite, &sec, 0) == 0 ||
100 DuplicateHandle(
101 GetCurrentProcess(), temp, GetCurrentProcess(), &stdoutRead, 0,
102 FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS) == 0)
103 {
104 free(path);
105 writeError("Error: CreatePipe/DuplicateHandle failed!\n");
107 return 1;
108 }
109 STARTUPINFOW startinfo;
110 PROCESS_INFORMATION procinfo;
111 memset(&startinfo, 0, sizeof(startinfo));
112 startinfo.cb = sizeof(startinfo);
113 startinfo.lpDesktop = L"";
114 startinfo.dwFlags = STARTF_USESTDHANDLES;
115 startinfo.hStdOutput = stdoutWrite;
116 BOOL ret = CreateProcessW(
117 NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &startinfo, &procinfo);
118 if (ret != FALSE) {
119 // Release result of GetPath()
120 free(path);
121
122 char * buf = NULL;
123 char * tmp;
124 DWORD n = 1000;
125 DWORD k = 0;
126 DWORD exitcode;
127 CloseHandle(stdoutWrite);
128 CloseHandle(procinfo.hThread);
129 for (;;) {
130 DWORD m;
131 tmp = realloc(buf, n);
132 if (tmp == NULL) {
133 free(buf);
135 "Error: out of memory reading unoinfo output!\n");
137 return 1;
138 }
139 buf = tmp;
140 if (!ReadFile(stdoutRead, buf + k, n - k, &m, NULL))
141 {
142 DWORD err = GetLastError();
143 if (err == ERROR_HANDLE_EOF || err == ERROR_BROKEN_PIPE) {
144 break;
145 }
146 writeError("Error: cannot read unoinfo output!\n");
148 return 1;
149 }
150 if (m == 0) {
151 break;
152 }
153 k += m;
154 if (k >= n) {
155 if (n >= MAXDWORD / 2) {
157 "Error: out of memory reading unoinfo output!\n");
159 return 1;
160 }
161 n *= 2;
162 }
163 }
164 if ((k & 1) == 1) {
165 writeError("Error: bad unoinfo output!\n");
167 return 1;
168 }
169 CloseHandle(stdoutRead);
170 if (!GetExitCodeProcess(procinfo.hProcess, &exitcode) ||
171 exitcode != 0)
172 {
173 writeError("Error: executing unoinfo failed!\n");
175 return 1;
176 }
177 path = (wchar_t*)realloc(buf, k + sizeof(wchar_t));
178 if (path == NULL)
179 {
180 free(buf);
182 "Error: out of memory zero-terminating unoinfo output!\n");
184 return 1;
185 }
186 path[k / 2] = L'\0';
187 } else {
188 if (GetLastError() != ERROR_FILE_NOT_FOUND) {
189 free(path);
190 writeError("Error: calling unoinfo failed!\n");
192 return 1;
193 }
194 CloseHandle(stdoutRead);
195 CloseHandle(stdoutWrite);
196 }
197
198 /* get the value of the PATH environment variable */
199 const wchar_t* ENVVARNAME = L"PATH";
200 const wchar_t* PATHSEPARATOR = L";";
201 wchar_t* value = _wgetenv( ENVVARNAME );
202
203 /*
204 * add the UNO installation path to the PATH environment variable;
205 * note that this only affects the environment variable of the current
206 * process, the command processor's environment is not changed
207 */
208 size_t size = wcslen( ENVVARNAME ) + wcslen( L"=" ) + wcslen( path ) + 1;
209 if ( value != NULL )
210 size += wcslen( PATHSEPARATOR ) + wcslen( value );
211 wchar_t* envstr = (wchar_t*) malloc( size*sizeof(wchar_t) );
212 assert(envstr);
213 wcscpy( envstr, ENVVARNAME );
214 wcscat( envstr, L"=" );
215 wcscat( envstr, path );
216 if ( value != NULL )
217 {
218 wcscat( envstr, PATHSEPARATOR );
219 wcscat( envstr, value );
220 }
221 /* coverity[tainted_data : FALSE] */
222 _wputenv( envstr );
223 free( envstr );
224 free( path );
225 }
226 else
227 {
228 writeError( "Warning: no UNO installation found!\n" );
229 }
230
231 /* create the command line for the application process */
232 wchar_t* cmdline = createCommandLine( lpCmdLine );
233 if ( cmdline == NULL )
234 {
235 writeError( "Error: cannot create command line!\n" );
237 return 1;
238 }
239
240 /* create the application process */
241 STARTUPINFOW startup_info;
242 PROCESS_INFORMATION process_info;
243 memset( &startup_info, 0, sizeof(startup_info) );
244 startup_info.cb = sizeof(startup_info);
245 BOOL bCreate = CreateProcessW( NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL,
246 &startup_info, &process_info );
247 free( cmdline );
248 if ( !bCreate )
249 {
250 writeError( "Error: cannot create process!\n" );
252 return 1;
253 }
254
255 /* close the error file */
257
258 return 0;
259}
260
261/*
262 * Gets the path of a UNO installation.
263 *
264 * @return the installation path or NULL, if no installation was specified or
265 * found, or if an error occurred.
266 * Returned pointer must be released with free()
267 */
268wchar_t* getPath(void)
269{
270 wchar_t* path = cppuhelper_detail_findSofficePath();
271
272 if ( path == NULL )
273 writeError( "Warning: getting path from Windows Registry failed!\n" );
274
275 return path;
276}
277
278/*
279 * Creates the command line for the application process including the absolute
280 * path of the executable.
281 *
282 * <p>The application's executable file name is the name of this executable
283 * prefixed by '_'.</p>
284 *
285 * @param appendix specifies the command line for the application excluding
286 * the executable name
287 *
288 * @return the command line for the application process or NULL, if an error
289 * occurred
290 */
291wchar_t* createCommandLine( wchar_t const * appendix )
292{
293 const wchar_t* CMDPREFIX = L"_";
294 const wchar_t* DQUOTE = L"\"";
295 const wchar_t* SPACE = L" ";
296
297 wchar_t* cmdline = NULL;
298
299 wchar_t cmdname[ _MAX_PATH ];
300 wchar_t drive[ _MAX_DRIVE ];
301 wchar_t dir[ _MAX_PATH ];
302 wchar_t base[ _MAX_FNAME ];
303 wchar_t newbase[ _MAX_FNAME ];
304 wchar_t ext[ _MAX_EXT ];
305
306 /* get the absolute path of the executable file */
307 if ( GetModuleFileNameW( NULL, cmdname, MY_SIZE( cmdname ) ) )
308 {
309 /* prefix the executable file name by '_' */
310 _wsplitpath( cmdname, drive, dir, base, ext );
311 wcscpy( newbase, CMDPREFIX );
312 wcscat( newbase, base );
313 _wmakepath( cmdname, drive, dir, newbase, ext );
314
315 /* create the command line */
316 cmdline = (wchar_t*) malloc( (wcslen( DQUOTE ) + wcslen( cmdname ) +
317 wcslen ( DQUOTE ) + wcslen( SPACE ) + wcslen( appendix ) + 1) * sizeof(wchar_t) );
318 assert(cmdline);
319 wcscpy( cmdline, DQUOTE );
320 wcscat( cmdline, cmdname );
321 wcscat( cmdline, DQUOTE );
322 wcscat( cmdline, SPACE );
323 wcscat( cmdline, appendix );
324 }
325
326 return cmdline;
327}
328
329/*
330 * Gets the pointer to the error file.
331 *
332 * <p>The error file will only be created, if create != 0.</p>
333 *
334 * <p>The error file has the name <executable file name>-error.log and is
335 * created in the same directory as the executable file. If this fails,
336 * the error file is created in the directory designated for temporary files.
337 * </p>
338
339 * @param create specifies, if the error file should be created (create != 0)
340 *
341 * @return the pointer to the open error file or NULL, if no error file is
342 * open or can be created
343 */
344FILE* getErrorFile( int create )
345{
346 const wchar_t* MODE = L"w";
347 const wchar_t* BASEPOSTFIX = L"-error";
348 const wchar_t* EXTENSION = L".log";
349
350 static FILE* ferr = NULL;
351
352 wchar_t fname[ _MAX_PATH ];
353 wchar_t drive[ _MAX_DRIVE ];
354 wchar_t dir[ _MAX_PATH ];
355 wchar_t base[ _MAX_FNAME ];
356 wchar_t newbase[ _MAX_FNAME ];
357 wchar_t ext[ _MAX_EXT ];
358
359 if ( ferr == NULL && create )
360 {
361 /* get the absolute path of the executable file */
362 if ( GetModuleFileNameW( NULL, fname, MY_SIZE( fname ) ) )
363 {
364 /* create error file in the directory of the executable file */
365 _wsplitpath( fname, drive, dir, base, ext );
366 wcscpy( newbase, base );
367 wcscat( newbase, BASEPOSTFIX );
368 _wmakepath( fname, drive, dir, newbase, EXTENSION );
369 ferr = _wfopen( fname, MODE );
370
371 if ( ferr == NULL )
372 {
373 /* create error file in the temp directory */
374 GetTempPathW(MY_SIZE( fname ), fname );
375 wcscat( fname, newbase );
376 wcscat( fname, EXTENSION );
377 ferr = _wfopen( fname, MODE );
378 }
379 }
380 }
381
382 return ferr;
383}
384
385/*
386 * Writes an error message to the error file.
387 *
388 * @param errstr specifies the error message
389 */
390void writeError( const char* errstr )
391{
392 FILE* ferr = getErrorFile( 1 );
393 if ( ferr != NULL )
394 {
395 fputs( errstr, ferr );
396 fflush( ferr );
397 }
398}
399
400/*
401 * Closes the error file.
402 */
404{
405 FILE* ferr = getErrorFile( 0 );
406 if ( ferr != NULL )
407 fclose( ferr );
408}
409
410/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Any value
#define SPACE
char * cppuhelper_detail_findSofficePath(void)
void const * base
#define TRUE
#define FALSE
#define MAX_PATH
sal_Int64 n
#define _MAX_PATH
return NULL
err
size
css::uno::Reference< css::deployment::XPackageRegistry > create(css::uno::Reference< css::deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, css::uno::Reference< css::uno::XComponentContext > const &xComponentContext)
m
const wchar_t *typedef BOOL
static const char * PATHSEPARATOR
static void closeErrorFile(void)
#define MY_SIZE(s)
static void writeError(const char *errstr)
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
#define MY_LENGTH(s)
static wchar_t * createCommandLine(wchar_t const *lpCmdLine)
static wchar_t * getPath(void)
static FILE * getErrorFile(int create)