12e82054bSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
32e82054bSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
42e82054bSAndrew Rist * or more contributor license agreements. See the NOTICE file
52e82054bSAndrew Rist * distributed with this work for additional information
62e82054bSAndrew Rist * regarding copyright ownership. The ASF licenses this file
72e82054bSAndrew Rist * to you under the Apache License, Version 2.0 (the
82e82054bSAndrew Rist * "License"); you may not use this file except in compliance
92e82054bSAndrew Rist * with the License. You may obtain a copy of the License at
102e82054bSAndrew Rist *
112e82054bSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
122e82054bSAndrew Rist *
132e82054bSAndrew Rist * Unless required by applicable law or agreed to in writing,
142e82054bSAndrew Rist * software distributed under the License is distributed on an
152e82054bSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162e82054bSAndrew Rist * KIND, either express or implied. See the License for the
172e82054bSAndrew Rist * specific language governing permissions and limitations
182e82054bSAndrew Rist * under the License.
192e82054bSAndrew Rist *
202e82054bSAndrew Rist *************************************************************/
212e82054bSAndrew Rist
222e82054bSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include <stdlib.h>
25cdf0e10cSrcweir #include <stdio.h>
26cdf0e10cSrcweir #include <string.h>
27cdf0e10cSrcweir #include <process.h>
28cdf0e10cSrcweir
29cdf0e10cSrcweir #if defined _MSC_VER
30cdf0e10cSrcweir #pragma warning(push, 1)
31cdf0e10cSrcweir #endif
32cdf0e10cSrcweir #include <windows.h>
33cdf0e10cSrcweir #if defined _MSC_VER
34cdf0e10cSrcweir #pragma warning(pop)
35cdf0e10cSrcweir #endif
36cdf0e10cSrcweir
37cdf0e10cSrcweir #include "cppuhelper/findsofficepath.h"
38cdf0e10cSrcweir #include "sal/types.h"
39cdf0e10cSrcweir
40cdf0e10cSrcweir #define MY_LENGTH(s) (sizeof (s) / sizeof *(s) - 1)
41cdf0e10cSrcweir
42cdf0e10cSrcweir char const* getPath();
43cdf0e10cSrcweir char* createCommandLine( char* lpCmdLine );
44cdf0e10cSrcweir FILE* getErrorFile( int create );
45cdf0e10cSrcweir void writeError( const char* errstr );
46cdf0e10cSrcweir void closeErrorFile();
47cdf0e10cSrcweir
48cdf0e10cSrcweir /*
49cdf0e10cSrcweir * The main function implements a loader for applications which use UNO.
50cdf0e10cSrcweir *
51cdf0e10cSrcweir * <p>This code runs on the Windows platform only.</p>
52cdf0e10cSrcweir *
53cdf0e10cSrcweir * <p>The main function detects a UNO installation on the system and adds the
54cdf0e10cSrcweir * program directory of the UNO installation to the PATH environment variable.
55cdf0e10cSrcweir * After that, the application process is loaded and started, whereby the
56cdf0e10cSrcweir * new process inherits the environment of the calling process, including
57cdf0e10cSrcweir * the modified PATH environment variable. The application's executable name
58cdf0e10cSrcweir * must be the same as the name of this executable, prefixed by '_'.</p>
59cdf0e10cSrcweir *
60cdf0e10cSrcweir * <p>A UNO installation can be specified by the user by setting the UNO_PATH
61cdf0e10cSrcweir * environment variable to the program directory of the UNO installation.
62cdf0e10cSrcweir * If no installation is specified by the user, the default installation on
63cdf0e10cSrcweir * the system will be taken. The default installation is read from the
64599cc5b4SOliver-Rainer Wittmann * default value of the key "Software\OpenOffice\UNO\InstallPath" from the
65cdf0e10cSrcweir * root key HKEY_CURRENT_USER in the Windows Registry. If this key is missing,
66cdf0e10cSrcweir * the key is read from the root key HKEY_LOCAL_MACHINE.</p>
67cdf0e10cSrcweir */
WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)68cdf0e10cSrcweir int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
69cdf0e10cSrcweir LPSTR lpCmdLine, int nCmdShow )
70cdf0e10cSrcweir {
71cdf0e10cSrcweir const char* ENVVARNAME = "PATH";
72cdf0e10cSrcweir const char* PATHSEPARATOR = ";";
73cdf0e10cSrcweir
74cdf0e10cSrcweir char const* path = NULL;
75cdf0e10cSrcweir char* value = NULL;
76cdf0e10cSrcweir char* envstr = NULL;
77cdf0e10cSrcweir char* cmdline = NULL;
78cdf0e10cSrcweir int size;
79cdf0e10cSrcweir STARTUPINFO startup_info;
80cdf0e10cSrcweir PROCESS_INFORMATION process_info;
812e9bc605SJürgen Schmidt BOOL bCreate;
822e9bc605SJürgen Schmidt
83cdf0e10cSrcweir (void) hInstance; /* unused */
84cdf0e10cSrcweir (void) hPrevInstance; /* unused */
85cdf0e10cSrcweir (void) nCmdShow; /* unused */
86cdf0e10cSrcweir
87cdf0e10cSrcweir /* get the path of the UNO installation */
88cdf0e10cSrcweir path = getPath();
892e9bc605SJürgen Schmidt
90cdf0e10cSrcweir if ( path != NULL )
91cdf0e10cSrcweir {
922e9bc605SJürgen Schmidt /* The former code to call unoinfo first is removed because we can use the office path
932e9bc605SJürgen Schmidt from the registry or from the UNO_PATH variable directly.
942e9bc605SJürgen Schmidt Further cleanup can remove unoinfo from the installation when all places where it is
952e9bc605SJürgen Schmidt used are checked.
962e9bc605SJürgen Schmidt */
972e9bc605SJürgen Schmidt
98cdf0e10cSrcweir /* get the value of the PATH environment variable */
99cdf0e10cSrcweir value = getenv( ENVVARNAME );
100cdf0e10cSrcweir
101cdf0e10cSrcweir /*
102cdf0e10cSrcweir * add the UNO installation path to the PATH environment variable;
103cdf0e10cSrcweir * note that this only affects the environment variable of the current
104cdf0e10cSrcweir * process, the command processor's environment is not changed
105cdf0e10cSrcweir */
106cdf0e10cSrcweir size = strlen( ENVVARNAME ) + strlen( "=" ) + strlen( path ) + 1;
107cdf0e10cSrcweir if ( value != NULL )
108cdf0e10cSrcweir size += strlen( PATHSEPARATOR ) + strlen( value );
109cdf0e10cSrcweir envstr = (char*) malloc( size );
110cdf0e10cSrcweir strcpy( envstr, ENVVARNAME );
111cdf0e10cSrcweir strcat( envstr, "=" );
112cdf0e10cSrcweir strcat( envstr, path );
113cdf0e10cSrcweir if ( value != NULL )
114cdf0e10cSrcweir {
115cdf0e10cSrcweir strcat( envstr, PATHSEPARATOR );
116cdf0e10cSrcweir strcat( envstr, value );
117cdf0e10cSrcweir }
118cdf0e10cSrcweir _putenv( envstr );
119cdf0e10cSrcweir free( envstr );
120cdf0e10cSrcweir }
121cdf0e10cSrcweir else
122cdf0e10cSrcweir {
123cdf0e10cSrcweir writeError( "Warning: no UNO installation found!\n" );
124cdf0e10cSrcweir }
125cdf0e10cSrcweir
126cdf0e10cSrcweir /* create the command line for the application process */
127cdf0e10cSrcweir cmdline = createCommandLine( lpCmdLine );
128cdf0e10cSrcweir if ( cmdline == NULL )
129cdf0e10cSrcweir {
130cdf0e10cSrcweir writeError( "Error: cannot create command line!\n" );
131cdf0e10cSrcweir closeErrorFile();
132cdf0e10cSrcweir return 1;
133cdf0e10cSrcweir }
134cdf0e10cSrcweir
135cdf0e10cSrcweir /* create the application process */
136cdf0e10cSrcweir memset( &startup_info, 0, sizeof( STARTUPINFO ) );
137cdf0e10cSrcweir startup_info.cb = sizeof( STARTUPINFO );
138cdf0e10cSrcweir bCreate = CreateProcess( NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL,
139cdf0e10cSrcweir &startup_info, &process_info );
140cdf0e10cSrcweir free( cmdline );
141cdf0e10cSrcweir if ( !bCreate )
142cdf0e10cSrcweir {
143cdf0e10cSrcweir writeError( "Error: cannot create process!\n" );
144cdf0e10cSrcweir closeErrorFile();
145cdf0e10cSrcweir return 1;
146cdf0e10cSrcweir }
147cdf0e10cSrcweir
148cdf0e10cSrcweir /* close the error file */
149cdf0e10cSrcweir closeErrorFile();
150cdf0e10cSrcweir
151cdf0e10cSrcweir return 0;
152cdf0e10cSrcweir }
153cdf0e10cSrcweir
154cdf0e10cSrcweir /*
155cdf0e10cSrcweir * Gets the path of a UNO installation.
156cdf0e10cSrcweir *
157cdf0e10cSrcweir * @return the installation path or NULL, if no installation was specified or
158*a893be29SPedro Giffuni * found, or if an error occurred
159cdf0e10cSrcweir */
getPath()160cdf0e10cSrcweir char const* getPath()
161cdf0e10cSrcweir {
162cdf0e10cSrcweir char const* path = cppuhelper_detail_findSofficePath();
1632e9bc605SJürgen Schmidt
164cdf0e10cSrcweir if ( path == NULL )
165cdf0e10cSrcweir writeError( "Warning: getting path from Windows Registry failed!\n" );
166cdf0e10cSrcweir
167cdf0e10cSrcweir return path;
168cdf0e10cSrcweir }
169cdf0e10cSrcweir
170cdf0e10cSrcweir /*
171cdf0e10cSrcweir * Creates the command line for the application process including the absolute
172cdf0e10cSrcweir * path of the executable.
173cdf0e10cSrcweir *
174cdf0e10cSrcweir * <p>The application's executable file name is the name of this executable
175cdf0e10cSrcweir * prefixed by '_'.</p>
176cdf0e10cSrcweir *
177cdf0e10cSrcweir * @param appendix specifies the command line for the application excluding
178cdf0e10cSrcweir * the executable name
179cdf0e10cSrcweir *
180cdf0e10cSrcweir * @return the command line for the application process or NULL, if an error
181*a893be29SPedro Giffuni * occurred
182cdf0e10cSrcweir */
createCommandLine(char * appendix)183cdf0e10cSrcweir char* createCommandLine( char* appendix )
184cdf0e10cSrcweir {
185cdf0e10cSrcweir const char* CMDPREFIX = "_";
186cdf0e10cSrcweir const char* DQUOTE = "\"";
187cdf0e10cSrcweir const char* SPACE = " ";
188cdf0e10cSrcweir
189cdf0e10cSrcweir char* cmdline = NULL;
190cdf0e10cSrcweir
191cdf0e10cSrcweir char cmdname[ _MAX_PATH ];
192cdf0e10cSrcweir char drive[ _MAX_DRIVE ];
193cdf0e10cSrcweir char dir[ _MAX_PATH ];
194cdf0e10cSrcweir char base[ _MAX_FNAME ];
195cdf0e10cSrcweir char newbase[ _MAX_FNAME ];
196cdf0e10cSrcweir char ext[ _MAX_EXT ];
197cdf0e10cSrcweir
198cdf0e10cSrcweir /* get the absolute path of the executable file */
199cdf0e10cSrcweir if ( GetModuleFileName( NULL, cmdname, sizeof( cmdname ) ) )
200cdf0e10cSrcweir {
201cdf0e10cSrcweir /* prefix the executable file name by '_' */
202cdf0e10cSrcweir _splitpath( cmdname, drive, dir, base, ext );
203cdf0e10cSrcweir strcpy( newbase, CMDPREFIX );
204cdf0e10cSrcweir strcat( newbase, base );
205cdf0e10cSrcweir _makepath( cmdname, drive, dir, newbase, ext );
206cdf0e10cSrcweir
207cdf0e10cSrcweir /* create the command line */
208cdf0e10cSrcweir cmdline = (char*) malloc( strlen( DQUOTE ) + strlen( cmdname ) +
209cdf0e10cSrcweir strlen ( DQUOTE ) + strlen( SPACE ) + strlen( appendix ) + 1 );
210cdf0e10cSrcweir strcpy( cmdline, DQUOTE );
211cdf0e10cSrcweir strcat( cmdline, cmdname );
212cdf0e10cSrcweir strcat( cmdline, DQUOTE );
213cdf0e10cSrcweir strcat( cmdline, SPACE );
214cdf0e10cSrcweir strcat( cmdline, appendix );
215cdf0e10cSrcweir }
216cdf0e10cSrcweir
217cdf0e10cSrcweir return cmdline;
218cdf0e10cSrcweir }
219cdf0e10cSrcweir
220cdf0e10cSrcweir /*
221cdf0e10cSrcweir * Gets the pointer to the error file.
222cdf0e10cSrcweir *
223cdf0e10cSrcweir * <p>The error file will only be created, if create != 0.</p>
224cdf0e10cSrcweir *
225cdf0e10cSrcweir * <p>The error file has the name <executable file name>-error.log and is
226cdf0e10cSrcweir * created in the same directory as the executable file. If this fails,
227cdf0e10cSrcweir * the error file is created in the directory designated for temporary files.
228cdf0e10cSrcweir * </p>
229cdf0e10cSrcweir
230cdf0e10cSrcweir * @param create specifies, if the error file should be created (create != 0)
231cdf0e10cSrcweir *
232cdf0e10cSrcweir * @return the pointer to the open error file or NULL, if no error file is
233cdf0e10cSrcweir * open or can be created
234cdf0e10cSrcweir */
getErrorFile(int create)235cdf0e10cSrcweir FILE* getErrorFile( int create )
236cdf0e10cSrcweir {
237cdf0e10cSrcweir const char* MODE = "w";
238cdf0e10cSrcweir const char* BASEPOSTFIX = "-error";
239cdf0e10cSrcweir const char* EXTENSION = ".log";
240cdf0e10cSrcweir
241cdf0e10cSrcweir static FILE* ferr = NULL;
242cdf0e10cSrcweir
243cdf0e10cSrcweir char fname[ _MAX_PATH ];
244cdf0e10cSrcweir char drive[ _MAX_DRIVE ];
245cdf0e10cSrcweir char dir[ _MAX_PATH ];
246cdf0e10cSrcweir char base[ _MAX_FNAME ];
247cdf0e10cSrcweir char newbase[ _MAX_FNAME ];
248cdf0e10cSrcweir char ext[ _MAX_EXT ];
249cdf0e10cSrcweir
250cdf0e10cSrcweir if ( ferr == NULL && create )
251cdf0e10cSrcweir {
252cdf0e10cSrcweir /* get the absolute path of the executable file */
253cdf0e10cSrcweir if ( GetModuleFileName( NULL, fname, sizeof( fname ) ) )
254cdf0e10cSrcweir {
255cdf0e10cSrcweir /* create error file in the directory of the executable file */
256cdf0e10cSrcweir _splitpath( fname, drive, dir, base, ext );
257cdf0e10cSrcweir strcpy( newbase, base );
258cdf0e10cSrcweir strcat( newbase, BASEPOSTFIX );
259cdf0e10cSrcweir _makepath( fname, drive, dir, newbase, EXTENSION );
260cdf0e10cSrcweir ferr = fopen( fname, MODE );
261cdf0e10cSrcweir
262cdf0e10cSrcweir if ( ferr == NULL )
263cdf0e10cSrcweir {
264cdf0e10cSrcweir /* create error file in the temp directory */
265cdf0e10cSrcweir GetTempPath( sizeof( fname ), fname );
266cdf0e10cSrcweir strcat( fname, newbase );
267cdf0e10cSrcweir strcat( fname, EXTENSION );
268cdf0e10cSrcweir ferr = fopen( fname, MODE );
269cdf0e10cSrcweir }
270cdf0e10cSrcweir }
271cdf0e10cSrcweir }
272cdf0e10cSrcweir
273cdf0e10cSrcweir return ferr;
274cdf0e10cSrcweir }
275cdf0e10cSrcweir
276cdf0e10cSrcweir /*
277cdf0e10cSrcweir * Writes an error message to the error file.
278cdf0e10cSrcweir *
279cdf0e10cSrcweir * @param errstr specifies the error message
280cdf0e10cSrcweir */
writeError(const char * errstr)281cdf0e10cSrcweir void writeError( const char* errstr )
282cdf0e10cSrcweir {
283cdf0e10cSrcweir FILE* ferr = getErrorFile( 1 );
284cdf0e10cSrcweir if ( ferr != NULL )
285cdf0e10cSrcweir {
286cdf0e10cSrcweir fprintf( ferr, errstr );
287cdf0e10cSrcweir fflush( ferr );
288cdf0e10cSrcweir }
289cdf0e10cSrcweir }
290cdf0e10cSrcweir
291cdf0e10cSrcweir /*
292cdf0e10cSrcweir * Closes the error file.
293cdf0e10cSrcweir */
closeErrorFile()294cdf0e10cSrcweir void closeErrorFile()
295cdf0e10cSrcweir {
296cdf0e10cSrcweir FILE* ferr = getErrorFile( 0 );
297cdf0e10cSrcweir if ( ferr != NULL )
298cdf0e10cSrcweir fclose( ferr );
299cdf0e10cSrcweir }
300