1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_desktop.hxx"
30 
31 #include "launcher.hxx"
32 
33 
34 #ifndef _WINDOWS_
35 #	define WIN32_LEAN_AND_MEAN
36 #if defined _MSC_VER
37 #pragma warning(push, 1)
38 #endif
39 #	include <windows.h>
40 #	include <shellapi.h>
41 #if defined _MSC_VER
42 #pragma warning(pop)
43 #endif
44 #endif
45 
46 
47 #include <stdlib.h>
48 #include <malloc.h>
49 
50 
51 #ifdef __MINGW32__
52 extern "C" int APIENTRY WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
53 #else
54 extern "C" int APIENTRY _tWinMain( HINSTANCE, HINSTANCE, LPTSTR, int )
55 #endif
56 {
57 	// Retreive startup info
58 
59 	STARTUPINFO	aStartupInfo;
60 
61 	ZeroMemory( &aStartupInfo, sizeof(aStartupInfo) );
62 	aStartupInfo.cb = sizeof( aStartupInfo );
63 	GetStartupInfo( &aStartupInfo );
64 
65 	// Retrieve command line
66 
67 	LPTSTR	lpCommandLine = GetCommandLine();
68 
69 	LPTSTR	*ppArguments = NULL;
70 	int		nArguments = 0;
71 
72 	ppArguments = GetArgv( &nArguments );
73 
74     // if ( 1 == nArguments )
75 	{
76 		lpCommandLine = (LPTSTR)_alloca( sizeof(_TCHAR) * (_tcslen(lpCommandLine) + _tcslen(APPLICATION_SWITCH) + 2) );
77 
78 		_tcscpy( lpCommandLine, GetCommandLine() );
79 		_tcscat( lpCommandLine, _T(" ") );
80 		_tcscat( lpCommandLine, APPLICATION_SWITCH );
81 	}
82 
83 
84 	// Calculate application name
85 
86 	TCHAR	szApplicationName[MAX_PATH];
87 	TCHAR	szDrive[MAX_PATH];
88 	TCHAR	szDir[MAX_PATH];
89 	TCHAR	szFileName[MAX_PATH];
90 	TCHAR	szExt[MAX_PATH];
91 
92 	GetModuleFileName( NULL, szApplicationName, MAX_PATH );
93 	_tsplitpath( szApplicationName, szDrive, szDir, szFileName, szExt );
94 	_tmakepath( szApplicationName, szDrive, szDir, OFFICE_IMAGE_NAME, _T(".exe") );
95 
96 	PROCESS_INFORMATION	aProcessInfo;
97 
98 	BOOL	fSuccess = CreateProcess(
99 		szApplicationName,
100 		lpCommandLine,
101 		NULL,
102 		NULL,
103 		TRUE,
104 		0,
105 		NULL,
106 		NULL,
107 		&aStartupInfo,
108 		&aProcessInfo );
109 
110 	if ( fSuccess )
111 	{
112 		// Wait for soffice process to be terminated to allow other applications
113 		// to wait for termination of started process
114 
115 		WaitForSingleObject( aProcessInfo.hProcess, INFINITE );
116 
117 		CloseHandle( aProcessInfo.hProcess );
118 		CloseHandle( aProcessInfo.hThread );
119 
120 		return 0;
121 	}
122 
123 	DWORD	dwError = GetLastError();
124 
125 	LPVOID lpMsgBuf;
126 
127 	FormatMessage(
128 		FORMAT_MESSAGE_ALLOCATE_BUFFER |
129 		FORMAT_MESSAGE_FROM_SYSTEM,
130 		NULL,
131 		dwError,
132 		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
133 		(LPTSTR)&lpMsgBuf,
134 		0,
135 		NULL
136 	);
137 
138 	// Display the string.
139 	MessageBox( NULL, (LPCTSTR)lpMsgBuf, NULL, MB_OK | MB_ICONERROR );
140 
141 	// Free the buffer.
142 	LocalFree( lpMsgBuf );
143 
144 	return GetLastError();
145 }
146 
147