1*2722ceddSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2722ceddSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2722ceddSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2722ceddSAndrew Rist  * distributed with this work for additional information
6*2722ceddSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2722ceddSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2722ceddSAndrew Rist  * "License"); you may not use this file except in compliance
9*2722ceddSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2722ceddSAndrew Rist  *
11*2722ceddSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2722ceddSAndrew Rist  *
13*2722ceddSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2722ceddSAndrew Rist  * software distributed under the License is distributed on an
15*2722ceddSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2722ceddSAndrew Rist  * KIND, either express or implied.  See the License for the
17*2722ceddSAndrew Rist  * specific language governing permissions and limitations
18*2722ceddSAndrew Rist  * under the License.
19*2722ceddSAndrew Rist  *
20*2722ceddSAndrew Rist  *************************************************************/
21*2722ceddSAndrew Rist 
22*2722ceddSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_desktop.hxx"
26cdf0e10cSrcweir #define UNICODE
27cdf0e10cSrcweir #define _UNICODE
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #define WIN32_LEAN_AND_MEAN
30cdf0e10cSrcweir #if defined _MSC_VER
31cdf0e10cSrcweir #pragma warning(push, 1)
32cdf0e10cSrcweir #endif
33cdf0e10cSrcweir #include <windows.h>
34cdf0e10cSrcweir #include <shellapi.h>
35cdf0e10cSrcweir #if defined _MSC_VER
36cdf0e10cSrcweir #pragma warning(pop)
37cdf0e10cSrcweir #endif
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <tchar.h>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include <malloc.h>
42cdf0e10cSrcweir #include <string.h>
43cdf0e10cSrcweir #include <stdlib.h>
44cdf0e10cSrcweir #include <systools/win32/uwinapi.h>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #include "tools/pathutils.hxx"
47cdf0e10cSrcweir #include "../extendloaderenvironment.hxx"
48cdf0e10cSrcweir 
49cdf0e10cSrcweir //---------------------------------------------------------------------------
50cdf0e10cSrcweir 
GenericMain()51cdf0e10cSrcweir static int GenericMain()
52cdf0e10cSrcweir {
53cdf0e10cSrcweir 	TCHAR				szTargetFileName[MAX_PATH];
54cdf0e10cSrcweir     TCHAR               szIniDirectory[MAX_PATH];
55cdf0e10cSrcweir 	STARTUPINFO			aStartupInfo;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     desktop_win32::extendLoaderEnvironment(szTargetFileName, szIniDirectory);
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 	ZeroMemory( &aStartupInfo, sizeof(aStartupInfo) );
60cdf0e10cSrcweir 	aStartupInfo.cb = sizeof(aStartupInfo);
61cdf0e10cSrcweir 
62cdf0e10cSrcweir 	GetStartupInfo( &aStartupInfo );
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 	DWORD	dwExitCode = (DWORD)-1;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 	PROCESS_INFORMATION	aProcessInfo;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir     size_t iniDirLen = wcslen(szIniDirectory);
69cdf0e10cSrcweir     WCHAR cwd[MAX_PATH];
70cdf0e10cSrcweir     DWORD cwdLen = GetCurrentDirectoryW(MAX_PATH, cwd);
71cdf0e10cSrcweir     if (cwdLen >= MAX_PATH) {
72cdf0e10cSrcweir         cwdLen = 0;
73cdf0e10cSrcweir     }
74cdf0e10cSrcweir     WCHAR redirect[MAX_PATH];
75cdf0e10cSrcweir     DWORD dummy;
76cdf0e10cSrcweir     bool hasRedirect =
77cdf0e10cSrcweir         tools::buildPath(
78cdf0e10cSrcweir             redirect, szIniDirectory, szIniDirectory + iniDirLen,
79cdf0e10cSrcweir             MY_STRING(L"redirect.ini")) != NULL &&
80cdf0e10cSrcweir         (GetBinaryType(redirect, &dummy) || // cheaper check for file existence?
81cdf0e10cSrcweir          GetLastError() != ERROR_FILE_NOT_FOUND);
82cdf0e10cSrcweir     LPTSTR cl1 = GetCommandLine();
83cdf0e10cSrcweir     WCHAR * cl2 = new WCHAR[
84cdf0e10cSrcweir         wcslen(cl1) +
85cdf0e10cSrcweir         (hasRedirect
86cdf0e10cSrcweir          ? (MY_LENGTH(L" \"-env:INIFILENAME=vnd.sun.star.pathname:") +
87cdf0e10cSrcweir             iniDirLen + MY_LENGTH(L"redirect.ini\""))
88cdf0e10cSrcweir          : 0) +
89cdf0e10cSrcweir         MY_LENGTH(L" \"-env:OOO_CWD=2") + 4 * cwdLen + MY_LENGTH(L"\"") + 1];
90cdf0e10cSrcweir         // 4 * cwdLen: each char preceded by backslash, each trailing backslash
91cdf0e10cSrcweir         // doubled
92cdf0e10cSrcweir     WCHAR * p = desktop_win32::commandLineAppend(cl2, cl1);
93cdf0e10cSrcweir     if (hasRedirect) {
94cdf0e10cSrcweir         p = desktop_win32::commandLineAppend(
95cdf0e10cSrcweir             p, MY_STRING(L" \"-env:INIFILENAME=vnd.sun.star.pathname:"));
96cdf0e10cSrcweir         p = desktop_win32::commandLineAppend(p, szIniDirectory);
97cdf0e10cSrcweir         p = desktop_win32::commandLineAppend(p, MY_STRING(L"redirect.ini\""));
98cdf0e10cSrcweir     }
99cdf0e10cSrcweir     p = desktop_win32::commandLineAppend(p, MY_STRING(L" \"-env:OOO_CWD="));
100cdf0e10cSrcweir     if (cwdLen == 0) {
101cdf0e10cSrcweir         p = desktop_win32::commandLineAppend(p, MY_STRING(L"0"));
102cdf0e10cSrcweir     } else {
103cdf0e10cSrcweir         p = desktop_win32::commandLineAppend(p, MY_STRING(L"2"));
104cdf0e10cSrcweir         p = desktop_win32::commandLineAppendEncoded(p, cwd);
105cdf0e10cSrcweir     }
106cdf0e10cSrcweir     desktop_win32::commandLineAppend(p, MY_STRING(L"\""));
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 	BOOL fSuccess = CreateProcess(
109cdf0e10cSrcweir 		szTargetFileName,
110cdf0e10cSrcweir 		cl2,
111cdf0e10cSrcweir 		NULL,
112cdf0e10cSrcweir 		NULL,
113cdf0e10cSrcweir 		TRUE,
114cdf0e10cSrcweir 		0,
115cdf0e10cSrcweir 		NULL,
116cdf0e10cSrcweir 		szIniDirectory,
117cdf0e10cSrcweir 		&aStartupInfo,
118cdf0e10cSrcweir 		&aProcessInfo );
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     delete[] cl2;
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 	if ( fSuccess )
123cdf0e10cSrcweir 	{
124cdf0e10cSrcweir 		DWORD	dwWaitResult;
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 		do
127cdf0e10cSrcweir 		{
128cdf0e10cSrcweir 			// On Windows XP it seems as the desktop calls WaitForInputIdle after "OpenWidth" so we have to do so
129cdf0e10cSrcweir 			// as if we where processing any messages
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 			dwWaitResult = MsgWaitForMultipleObjects( 1, &aProcessInfo.hProcess, FALSE, INFINITE, QS_ALLEVENTS );
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 			if (  WAIT_OBJECT_0 + 1 == dwWaitResult )
134cdf0e10cSrcweir 			{
135cdf0e10cSrcweir 				MSG	msg;
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 				PeekMessage( &msg, NULL, 0, 0, PM_REMOVE );
138cdf0e10cSrcweir 			}
139cdf0e10cSrcweir 		} while ( WAIT_OBJECT_0 + 1 == dwWaitResult );
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 		dwExitCode = 0;
142cdf0e10cSrcweir 		GetExitCodeProcess( aProcessInfo.hProcess, &dwExitCode );
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 		CloseHandle( aProcessInfo.hProcess );
145cdf0e10cSrcweir 		CloseHandle( aProcessInfo.hThread );
146cdf0e10cSrcweir 	}
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 	return dwExitCode;
149cdf0e10cSrcweir }
150cdf0e10cSrcweir 
151cdf0e10cSrcweir //---------------------------------------------------------------------------
152cdf0e10cSrcweir 
153cdf0e10cSrcweir #ifdef __MINGW32__
WinMain(HINSTANCE,HINSTANCE,LPSTR,int)154cdf0e10cSrcweir int WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
155cdf0e10cSrcweir #else
156cdf0e10cSrcweir int WINAPI _tWinMain( HINSTANCE, HINSTANCE, LPTSTR, int )
157cdf0e10cSrcweir #endif
158cdf0e10cSrcweir {
159cdf0e10cSrcweir 	return GenericMain();
160cdf0e10cSrcweir }
161cdf0e10cSrcweir 
162cdf0e10cSrcweir //---------------------------------------------------------------------------
163cdf0e10cSrcweir 
164cdf0e10cSrcweir #ifdef __MINGW32__
main()165cdf0e10cSrcweir int __cdecl main()
166cdf0e10cSrcweir #else
167cdf0e10cSrcweir int __cdecl _tmain()
168cdf0e10cSrcweir #endif
169cdf0e10cSrcweir {
170cdf0e10cSrcweir 	return GenericMain();
171cdf0e10cSrcweir }
172cdf0e10cSrcweir 
173