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