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 // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_desktop.hxx"
30*cdf0e10cSrcweir #define UNICODE
31*cdf0e10cSrcweir #define _UNICODE
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #define WIN32_LEAN_AND_MEAN
34*cdf0e10cSrcweir #if defined _MSC_VER
35*cdf0e10cSrcweir #pragma warning(push, 1)
36*cdf0e10cSrcweir #endif
37*cdf0e10cSrcweir #include <windows.h>
38*cdf0e10cSrcweir #include <shellapi.h>
39*cdf0e10cSrcweir #if defined _MSC_VER
40*cdf0e10cSrcweir #pragma warning(pop)
41*cdf0e10cSrcweir #endif
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include <tchar.h>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #include <malloc.h>
46*cdf0e10cSrcweir #include <string.h>
47*cdf0e10cSrcweir #include <stdlib.h>
48*cdf0e10cSrcweir #include <systools/win32/uwinapi.h>
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir #include "tools/pathutils.hxx"
51*cdf0e10cSrcweir #include "../extendloaderenvironment.hxx"
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir //---------------------------------------------------------------------------
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir static int GenericMain()
56*cdf0e10cSrcweir {
57*cdf0e10cSrcweir 	TCHAR				szTargetFileName[MAX_PATH];
58*cdf0e10cSrcweir     TCHAR               szIniDirectory[MAX_PATH];
59*cdf0e10cSrcweir 	STARTUPINFO			aStartupInfo;
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir     desktop_win32::extendLoaderEnvironment(szTargetFileName, szIniDirectory);
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir 	ZeroMemory( &aStartupInfo, sizeof(aStartupInfo) );
64*cdf0e10cSrcweir 	aStartupInfo.cb = sizeof(aStartupInfo);
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir 	GetStartupInfo( &aStartupInfo );
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir 	DWORD	dwExitCode = (DWORD)-1;
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 	PROCESS_INFORMATION	aProcessInfo;
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir     size_t iniDirLen = wcslen(szIniDirectory);
73*cdf0e10cSrcweir     WCHAR cwd[MAX_PATH];
74*cdf0e10cSrcweir     DWORD cwdLen = GetCurrentDirectoryW(MAX_PATH, cwd);
75*cdf0e10cSrcweir     if (cwdLen >= MAX_PATH) {
76*cdf0e10cSrcweir         cwdLen = 0;
77*cdf0e10cSrcweir     }
78*cdf0e10cSrcweir     WCHAR redirect[MAX_PATH];
79*cdf0e10cSrcweir     DWORD dummy;
80*cdf0e10cSrcweir     bool hasRedirect =
81*cdf0e10cSrcweir         tools::buildPath(
82*cdf0e10cSrcweir             redirect, szIniDirectory, szIniDirectory + iniDirLen,
83*cdf0e10cSrcweir             MY_STRING(L"redirect.ini")) != NULL &&
84*cdf0e10cSrcweir         (GetBinaryType(redirect, &dummy) || // cheaper check for file existence?
85*cdf0e10cSrcweir          GetLastError() != ERROR_FILE_NOT_FOUND);
86*cdf0e10cSrcweir     LPTSTR cl1 = GetCommandLine();
87*cdf0e10cSrcweir     WCHAR * cl2 = new WCHAR[
88*cdf0e10cSrcweir         wcslen(cl1) +
89*cdf0e10cSrcweir         (hasRedirect
90*cdf0e10cSrcweir          ? (MY_LENGTH(L" \"-env:INIFILENAME=vnd.sun.star.pathname:") +
91*cdf0e10cSrcweir             iniDirLen + MY_LENGTH(L"redirect.ini\""))
92*cdf0e10cSrcweir          : 0) +
93*cdf0e10cSrcweir         MY_LENGTH(L" \"-env:OOO_CWD=2") + 4 * cwdLen + MY_LENGTH(L"\"") + 1];
94*cdf0e10cSrcweir         // 4 * cwdLen: each char preceded by backslash, each trailing backslash
95*cdf0e10cSrcweir         // doubled
96*cdf0e10cSrcweir     WCHAR * p = desktop_win32::commandLineAppend(cl2, cl1);
97*cdf0e10cSrcweir     if (hasRedirect) {
98*cdf0e10cSrcweir         p = desktop_win32::commandLineAppend(
99*cdf0e10cSrcweir             p, MY_STRING(L" \"-env:INIFILENAME=vnd.sun.star.pathname:"));
100*cdf0e10cSrcweir         p = desktop_win32::commandLineAppend(p, szIniDirectory);
101*cdf0e10cSrcweir         p = desktop_win32::commandLineAppend(p, MY_STRING(L"redirect.ini\""));
102*cdf0e10cSrcweir     }
103*cdf0e10cSrcweir     p = desktop_win32::commandLineAppend(p, MY_STRING(L" \"-env:OOO_CWD="));
104*cdf0e10cSrcweir     if (cwdLen == 0) {
105*cdf0e10cSrcweir         p = desktop_win32::commandLineAppend(p, MY_STRING(L"0"));
106*cdf0e10cSrcweir     } else {
107*cdf0e10cSrcweir         p = desktop_win32::commandLineAppend(p, MY_STRING(L"2"));
108*cdf0e10cSrcweir         p = desktop_win32::commandLineAppendEncoded(p, cwd);
109*cdf0e10cSrcweir     }
110*cdf0e10cSrcweir     desktop_win32::commandLineAppend(p, MY_STRING(L"\""));
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 	BOOL fSuccess = CreateProcess(
113*cdf0e10cSrcweir 		szTargetFileName,
114*cdf0e10cSrcweir 		cl2,
115*cdf0e10cSrcweir 		NULL,
116*cdf0e10cSrcweir 		NULL,
117*cdf0e10cSrcweir 		TRUE,
118*cdf0e10cSrcweir 		0,
119*cdf0e10cSrcweir 		NULL,
120*cdf0e10cSrcweir 		szIniDirectory,
121*cdf0e10cSrcweir 		&aStartupInfo,
122*cdf0e10cSrcweir 		&aProcessInfo );
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir     delete[] cl2;
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 	if ( fSuccess )
127*cdf0e10cSrcweir 	{
128*cdf0e10cSrcweir 		DWORD	dwWaitResult;
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 		do
131*cdf0e10cSrcweir 		{
132*cdf0e10cSrcweir 			// On Windows XP it seems as the desktop calls WaitForInputIdle after "OpenWidth" so we have to do so
133*cdf0e10cSrcweir 			// as if we where processing any messages
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir 			dwWaitResult = MsgWaitForMultipleObjects( 1, &aProcessInfo.hProcess, FALSE, INFINITE, QS_ALLEVENTS );
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 			if (  WAIT_OBJECT_0 + 1 == dwWaitResult )
138*cdf0e10cSrcweir 			{
139*cdf0e10cSrcweir 				MSG	msg;
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir 				PeekMessage( &msg, NULL, 0, 0, PM_REMOVE );
142*cdf0e10cSrcweir 			}
143*cdf0e10cSrcweir 		} while ( WAIT_OBJECT_0 + 1 == dwWaitResult );
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 		dwExitCode = 0;
146*cdf0e10cSrcweir 		GetExitCodeProcess( aProcessInfo.hProcess, &dwExitCode );
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir 		CloseHandle( aProcessInfo.hProcess );
149*cdf0e10cSrcweir 		CloseHandle( aProcessInfo.hThread );
150*cdf0e10cSrcweir 	}
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 	return dwExitCode;
153*cdf0e10cSrcweir }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir //---------------------------------------------------------------------------
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir #ifdef __MINGW32__
158*cdf0e10cSrcweir int WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
159*cdf0e10cSrcweir #else
160*cdf0e10cSrcweir int WINAPI _tWinMain( HINSTANCE, HINSTANCE, LPTSTR, int )
161*cdf0e10cSrcweir #endif
162*cdf0e10cSrcweir {
163*cdf0e10cSrcweir 	return GenericMain();
164*cdf0e10cSrcweir }
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir //---------------------------------------------------------------------------
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir #ifdef __MINGW32__
169*cdf0e10cSrcweir int __cdecl main()
170*cdf0e10cSrcweir #else
171*cdf0e10cSrcweir int __cdecl _tmain()
172*cdf0e10cSrcweir #endif
173*cdf0e10cSrcweir {
174*cdf0e10cSrcweir 	return GenericMain();
175*cdf0e10cSrcweir }
176*cdf0e10cSrcweir 
177