merge the formfield patch from ooo-build
[ooovba.git] / desktop / win32 / source / guiloader / genericloader.cxx
blob6d3002dd23aff628db395a7d9c27cbca819f71ca
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: genericloader.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_desktop.hxx"
33 #define UNICODE
34 #define _UNICODE
36 #define WIN32_LEAN_AND_MEAN
37 #if defined _MSC_VER
38 #pragma warning(push, 1)
39 #endif
40 #include <windows.h>
41 #include <shellapi.h>
42 #if defined _MSC_VER
43 #pragma warning(pop)
44 #endif
46 #include <tchar.h>
48 #include <malloc.h>
49 #include <string.h>
50 #include <stdlib.h>
51 #include <systools/win32/uwinapi.h>
53 #include "tools/pathutils.hxx"
54 #include "../extendloaderenvironment.hxx"
56 //---------------------------------------------------------------------------
58 static int GenericMain()
60 TCHAR szTargetFileName[MAX_PATH];
61 TCHAR szIniDirectory[MAX_PATH];
62 STARTUPINFO aStartupInfo;
64 desktop_win32::extendLoaderEnvironment(szTargetFileName, szIniDirectory);
66 ZeroMemory( &aStartupInfo, sizeof(aStartupInfo) );
67 aStartupInfo.cb = sizeof(aStartupInfo);
69 GetStartupInfo( &aStartupInfo );
71 DWORD dwExitCode = (DWORD)-1;
73 PROCESS_INFORMATION aProcessInfo;
75 size_t iniDirLen = wcslen(szIniDirectory);
76 WCHAR cwd[MAX_PATH];
77 DWORD cwdLen = GetCurrentDirectoryW(MAX_PATH, cwd);
78 if (cwdLen >= MAX_PATH) {
79 cwdLen = 0;
81 WCHAR redirect[MAX_PATH];
82 DWORD dummy;
83 bool hasRedirect =
84 tools::buildPath(
85 redirect, szIniDirectory, szIniDirectory + iniDirLen,
86 MY_STRING(L"redirect.ini")) != NULL &&
87 (GetBinaryType(redirect, &dummy) || // cheaper check for file existence?
88 GetLastError() != ERROR_FILE_NOT_FOUND);
89 LPTSTR cl1 = GetCommandLine();
90 WCHAR * cl2 = new WCHAR[
91 wcslen(cl1) +
92 (hasRedirect
93 ? (MY_LENGTH(L" \"-env:INIFILENAME=vnd.sun.star.pathname:") +
94 iniDirLen + MY_LENGTH(L"redirect.ini\""))
95 : 0) +
96 MY_LENGTH(L" \"-env:OOO_CWD=2") + 4 * cwdLen + MY_LENGTH(L"\"") + 1];
97 // 4 * cwdLen: each char preceded by backslash, each trailing backslash
98 // doubled
99 WCHAR * p = desktop_win32::commandLineAppend(cl2, cl1);
100 if (hasRedirect) {
101 p = desktop_win32::commandLineAppend(
102 p, MY_STRING(L" \"-env:INIFILENAME=vnd.sun.star.pathname:"));
103 p = desktop_win32::commandLineAppend(p, szIniDirectory);
104 p = desktop_win32::commandLineAppend(p, MY_STRING(L"redirect.ini\""));
106 p = desktop_win32::commandLineAppend(p, MY_STRING(L" \"-env:OOO_CWD="));
107 if (cwdLen == 0) {
108 p = desktop_win32::commandLineAppend(p, MY_STRING(L"0"));
109 } else {
110 p = desktop_win32::commandLineAppend(p, MY_STRING(L"2"));
111 p = desktop_win32::commandLineAppendEncoded(p, cwd);
113 desktop_win32::commandLineAppend(p, MY_STRING(L"\""));
115 BOOL fSuccess = CreateProcess(
116 szTargetFileName,
117 cl2,
118 NULL,
119 NULL,
120 TRUE,
122 NULL,
123 szIniDirectory,
124 &aStartupInfo,
125 &aProcessInfo );
127 delete[] cl2;
129 if ( fSuccess )
131 DWORD dwWaitResult;
135 // On Windows XP it seems as the desktop calls WaitForInputIdle after "OpenWidth" so we have to do so
136 // as if we where processing any messages
138 dwWaitResult = MsgWaitForMultipleObjects( 1, &aProcessInfo.hProcess, FALSE, INFINITE, QS_ALLEVENTS );
140 if ( WAIT_OBJECT_0 + 1 == dwWaitResult )
142 MSG msg;
144 PeekMessage( &msg, NULL, 0, 0, PM_REMOVE );
146 } while ( WAIT_OBJECT_0 + 1 == dwWaitResult );
148 dwExitCode = 0;
149 GetExitCodeProcess( aProcessInfo.hProcess, &dwExitCode );
151 CloseHandle( aProcessInfo.hProcess );
152 CloseHandle( aProcessInfo.hThread );
155 return dwExitCode;
158 //---------------------------------------------------------------------------
160 #ifdef __MINGW32__
161 int WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
162 #else
163 int WINAPI _tWinMain( HINSTANCE, HINSTANCE, LPTSTR, int )
164 #endif
166 return GenericMain();
169 //---------------------------------------------------------------------------
171 #ifdef __MINGW32__
172 int __cdecl main()
173 #else
174 int __cdecl _tmain()
175 #endif
177 return GenericMain();