1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #include <stdlib.h> 29 #include <stdio.h> 30 #include <string.h> 31 #include <process.h> 32 33 #if defined _MSC_VER 34 #pragma warning(push, 1) 35 #endif 36 #include <windows.h> 37 #if defined _MSC_VER 38 #pragma warning(pop) 39 #endif 40 41 #include "cppuhelper/findsofficepath.h" 42 #include "sal/types.h" 43 44 #define MY_LENGTH(s) (sizeof (s) / sizeof *(s) - 1) 45 46 char const* getPath(); 47 char* createCommandLine( char* lpCmdLine ); 48 FILE* getErrorFile( int create ); 49 void writeError( const char* errstr ); 50 void closeErrorFile(); 51 52 /* 53 * The main function implements a loader for applications which use UNO. 54 * 55 * <p>This code runs on the Windows platform only.</p> 56 * 57 * <p>The main function detects a UNO installation on the system and adds the 58 * program directory of the UNO installation to the PATH environment variable. 59 * After that, the application process is loaded and started, whereby the 60 * new process inherits the environment of the calling process, including 61 * the modified PATH environment variable. The application's executable name 62 * must be the same as the name of this executable, prefixed by '_'.</p> 63 * 64 * <p>A UNO installation can be specified by the user by setting the UNO_PATH 65 * environment variable to the program directory of the UNO installation. 66 * If no installation is specified by the user, the default installation on 67 * the system will be taken. The default installation is read from the 68 * default value of the key "Software\OpenOffice.org\UNO\InstallPath" from the 69 * root key HKEY_CURRENT_USER in the Windows Registry. If this key is missing, 70 * the key is read from the root key HKEY_LOCAL_MACHINE.</p> 71 */ 72 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, 73 LPSTR lpCmdLine, int nCmdShow ) 74 { 75 const char* ENVVARNAME = "PATH"; 76 const char* PATHSEPARATOR = ";"; 77 78 char const* path = NULL; 79 char path2[MAX_PATH]; 80 char* value = NULL; 81 char* envstr = NULL; 82 char* cmdline = NULL; 83 int size; 84 STARTUPINFO startup_info; 85 PROCESS_INFORMATION process_info; 86 BOOL bCreate; 87 88 (void) hInstance; /* unused */ 89 (void) hPrevInstance; /* unused */ 90 (void) nCmdShow; /* unused */ 91 92 /* get the path of the UNO installation */ 93 path = getPath(); 94 95 if ( path != NULL ) 96 { 97 wchar_t cmd[ 98 MY_LENGTH(L"\"") + MAX_PATH + 99 MY_LENGTH(L"\\unoinfo.exe\" c++")]; 100 /* hopefully does not overflow */ 101 int pathsize; 102 SECURITY_ATTRIBUTES sec; 103 HANDLE temp; 104 HANDLE stdoutRead; 105 HANDLE stdoutWrite; 106 STARTUPINFOW startinfo; 107 PROCESS_INFORMATION procinfo; 108 int ret; 109 cmd[0] = L'"'; 110 pathsize = MultiByteToWideChar(CP_ACP, 0, path, -1, cmd + 1, MAX_PATH); 111 if (pathsize == 0) { 112 writeError("Error: MultiByteToWideChar failed!\n"); 113 closeErrorFile(); 114 return 1; 115 } 116 if (wcschr(cmd + 1, L'"') != NULL) { 117 writeError("Error: bad characters in UNO installation path!\n"); 118 closeErrorFile(); 119 return 1; 120 } 121 wcscpy( 122 cmd + pathsize, 123 (L"\\unoinfo.exe\" c++" + 124 (pathsize == 1 || cmd[pathsize - 1] != L'\\' ? 0 : 1))); 125 sec.nLength = sizeof (SECURITY_ATTRIBUTES); 126 sec.lpSecurityDescriptor = NULL; 127 sec.bInheritHandle = TRUE; 128 if (CreatePipe(&temp, &stdoutWrite, &sec, 0) == 0 || 129 DuplicateHandle( 130 GetCurrentProcess(), temp, GetCurrentProcess(), &stdoutRead, 0, 131 FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS) == 0) 132 { 133 writeError("Error: CreatePipe/DuplicateHandle failed!\n"); 134 closeErrorFile(); 135 return 1; 136 } 137 memset(&startinfo, 0, sizeof (STARTUPINFOW)); 138 startinfo.cb = sizeof (STARTUPINFOW); 139 startinfo.lpDesktop = L""; 140 startinfo.dwFlags = STARTF_USESTDHANDLES; 141 startinfo.hStdOutput = stdoutWrite; 142 ret = CreateProcessW( 143 NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &startinfo, &procinfo); 144 if (ret != 0) { 145 char * buf = NULL; 146 size_t n = 1000; 147 size_t k = 0; 148 DWORD exitcode; 149 int path2size; 150 CloseHandle(stdoutWrite); 151 CloseHandle(procinfo.hThread); 152 for (;;) { 153 DWORD m; 154 buf = realloc(buf, n); 155 if (buf == NULL) { 156 writeError( 157 "Error: out of memory reading unoinfo output!\n"); 158 closeErrorFile(); 159 return 1; 160 } 161 if (!ReadFile(stdoutRead, buf + k, n - k, &m, NULL)) 162 { 163 DWORD err = GetLastError(); 164 if (err == ERROR_HANDLE_EOF || err == ERROR_BROKEN_PIPE) { 165 break; 166 } 167 writeError("Error: cannot read unoinfo output!\n"); 168 closeErrorFile(); 169 return 1; 170 } 171 if (m == 0) { 172 break; 173 } 174 k += m; 175 if (k >= n) { 176 if (n >= SAL_MAX_SIZE / 2) { 177 writeError( 178 "Error: out of memory reading unoinfo output!\n"); 179 closeErrorFile(); 180 return 1; 181 } 182 n *= 2; 183 } 184 } 185 if ((k & 1) == 1) { 186 writeError("Error: bad unoinfo output!\n"); 187 closeErrorFile(); 188 return 1; 189 } 190 CloseHandle(stdoutRead); 191 if (!GetExitCodeProcess(procinfo.hProcess, &exitcode) || 192 exitcode != 0) 193 { 194 writeError("Error: executing unoinfo failed!\n"); 195 closeErrorFile(); 196 return 1; 197 } 198 if (k == 0) { 199 path2size = 0; 200 } else { 201 path2size = WideCharToMultiByte( 202 CP_ACP, 0, (wchar_t *) buf, k / 2, path2, MAX_PATH - 1, 203 NULL, NULL); 204 if (path2size == 0) { 205 writeError("Error: converting unoinfo output failed!\n"); 206 closeErrorFile(); 207 return 1; 208 } 209 } 210 path2[path2size] = '\0'; 211 path = path2; 212 } else { 213 if (GetLastError() != ERROR_FILE_NOT_FOUND) { 214 writeError("Error: calling unoinfo failed!\n"); 215 closeErrorFile(); 216 return 1; 217 } 218 CloseHandle(stdoutRead); 219 CloseHandle(stdoutWrite); 220 } 221 222 /* get the value of the PATH environment variable */ 223 value = getenv( ENVVARNAME ); 224 225 /* 226 * add the UNO installation path to the PATH environment variable; 227 * note that this only affects the environment variable of the current 228 * process, the command processor's environment is not changed 229 */ 230 size = strlen( ENVVARNAME ) + strlen( "=" ) + strlen( path ) + 1; 231 if ( value != NULL ) 232 size += strlen( PATHSEPARATOR ) + strlen( value ); 233 envstr = (char*) malloc( size ); 234 strcpy( envstr, ENVVARNAME ); 235 strcat( envstr, "=" ); 236 strcat( envstr, path ); 237 if ( value != NULL ) 238 { 239 strcat( envstr, PATHSEPARATOR ); 240 strcat( envstr, value ); 241 } 242 _putenv( envstr ); 243 free( envstr ); 244 } 245 else 246 { 247 writeError( "Warning: no UNO installation found!\n" ); 248 } 249 250 /* create the command line for the application process */ 251 cmdline = createCommandLine( lpCmdLine ); 252 if ( cmdline == NULL ) 253 { 254 writeError( "Error: cannot create command line!\n" ); 255 closeErrorFile(); 256 return 1; 257 } 258 259 /* create the application process */ 260 memset( &startup_info, 0, sizeof( STARTUPINFO ) ); 261 startup_info.cb = sizeof( STARTUPINFO ); 262 bCreate = CreateProcess( NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, 263 &startup_info, &process_info ); 264 free( cmdline ); 265 if ( !bCreate ) 266 { 267 writeError( "Error: cannot create process!\n" ); 268 closeErrorFile(); 269 return 1; 270 } 271 272 /* close the error file */ 273 closeErrorFile(); 274 275 return 0; 276 } 277 278 /* 279 * Gets the path of a UNO installation. 280 * 281 * @return the installation path or NULL, if no installation was specified or 282 * found, or if an error occured 283 */ 284 char const* getPath() 285 { 286 char const* path = cppuhelper_detail_findSofficePath(); 287 288 if ( path == NULL ) 289 writeError( "Warning: getting path from Windows Registry failed!\n" ); 290 291 return path; 292 } 293 294 /* 295 * Creates the command line for the application process including the absolute 296 * path of the executable. 297 * 298 * <p>The application's executable file name is the name of this executable 299 * prefixed by '_'.</p> 300 * 301 * @param appendix specifies the command line for the application excluding 302 * the executable name 303 * 304 * @return the command line for the application process or NULL, if an error 305 * occured 306 */ 307 char* createCommandLine( char* appendix ) 308 { 309 const char* CMDPREFIX = "_"; 310 const char* DQUOTE = "\""; 311 const char* SPACE = " "; 312 313 char* cmdline = NULL; 314 315 char cmdname[ _MAX_PATH ]; 316 char drive[ _MAX_DRIVE ]; 317 char dir[ _MAX_PATH ]; 318 char base[ _MAX_FNAME ]; 319 char newbase[ _MAX_FNAME ]; 320 char ext[ _MAX_EXT ]; 321 322 /* get the absolute path of the executable file */ 323 if ( GetModuleFileName( NULL, cmdname, sizeof( cmdname ) ) ) 324 { 325 /* prefix the executable file name by '_' */ 326 _splitpath( cmdname, drive, dir, base, ext ); 327 strcpy( newbase, CMDPREFIX ); 328 strcat( newbase, base ); 329 _makepath( cmdname, drive, dir, newbase, ext ); 330 331 /* create the command line */ 332 cmdline = (char*) malloc( strlen( DQUOTE ) + strlen( cmdname ) + 333 strlen ( DQUOTE ) + strlen( SPACE ) + strlen( appendix ) + 1 ); 334 strcpy( cmdline, DQUOTE ); 335 strcat( cmdline, cmdname ); 336 strcat( cmdline, DQUOTE ); 337 strcat( cmdline, SPACE ); 338 strcat( cmdline, appendix ); 339 } 340 341 return cmdline; 342 } 343 344 /* 345 * Gets the pointer to the error file. 346 * 347 * <p>The error file will only be created, if create != 0.</p> 348 * 349 * <p>The error file has the name <executable file name>-error.log and is 350 * created in the same directory as the executable file. If this fails, 351 * the error file is created in the directory designated for temporary files. 352 * </p> 353 354 * @param create specifies, if the error file should be created (create != 0) 355 * 356 * @return the pointer to the open error file or NULL, if no error file is 357 * open or can be created 358 */ 359 FILE* getErrorFile( int create ) 360 { 361 const char* MODE = "w"; 362 const char* BASEPOSTFIX = "-error"; 363 const char* EXTENSION = ".log"; 364 365 static FILE* ferr = NULL; 366 367 char fname[ _MAX_PATH ]; 368 char drive[ _MAX_DRIVE ]; 369 char dir[ _MAX_PATH ]; 370 char base[ _MAX_FNAME ]; 371 char newbase[ _MAX_FNAME ]; 372 char ext[ _MAX_EXT ]; 373 374 if ( ferr == NULL && create ) 375 { 376 /* get the absolute path of the executable file */ 377 if ( GetModuleFileName( NULL, fname, sizeof( fname ) ) ) 378 { 379 /* create error file in the directory of the executable file */ 380 _splitpath( fname, drive, dir, base, ext ); 381 strcpy( newbase, base ); 382 strcat( newbase, BASEPOSTFIX ); 383 _makepath( fname, drive, dir, newbase, EXTENSION ); 384 ferr = fopen( fname, MODE ); 385 386 if ( ferr == NULL ) 387 { 388 /* create error file in the temp directory */ 389 GetTempPath( sizeof( fname ), fname ); 390 strcat( fname, newbase ); 391 strcat( fname, EXTENSION ); 392 ferr = fopen( fname, MODE ); 393 } 394 } 395 } 396 397 return ferr; 398 } 399 400 /* 401 * Writes an error message to the error file. 402 * 403 * @param errstr specifies the error message 404 */ 405 void writeError( const char* errstr ) 406 { 407 FILE* ferr = getErrorFile( 1 ); 408 if ( ferr != NULL ) 409 { 410 fprintf( ferr, errstr ); 411 fflush( ferr ); 412 } 413 } 414 415 /* 416 * Closes the error file. 417 */ 418 void closeErrorFile() 419 { 420 FILE* ferr = getErrorFile( 0 ); 421 if ( ferr != NULL ) 422 fclose( ferr ); 423 } 424