xref: /aoo4110/main/desktop/win32/source/wrapper.h (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #pragma once
23 #ifndef __cplusplus
24 #error Need C++ to compile
25 #endif
26 
27 #include "main.h"
28 
29 #ifndef _WINDOWS_
30 #if defined _MSC_VER
31 #pragma warning(push, 1)
32 #endif
33 #	include <windows.h>
34 #if defined _MSC_VER
35 #pragma warning(pop)
36 #endif
37 #endif
38 
39 
40 #ifndef _INC_TCHAR
41 #	ifdef UNICODE
42 #		define _UNICODE
43 #	endif
44 #	include <tchar.h>
45 #endif
46 
47 #ifdef UNICODE
48 #	define Main	MainW
49 #	define GetArgv( pArgc )			CommandLineToArgvW( GetCommandLine(), pArgc )
50 #	define PROCESS_CREATIONFLAGS	CREATE_UNICODE_ENVIRONMENT
51 #else
52 #	define GetArgv( pArgc )			(*pArgc = __argc, __argv)
53 #	define PROCESS_CREATIONFLAGS	0
54 #	define Main	MainA
55 #endif
56 
57 #define BIN_EXT_STR			TEXT(".bin")
58 #define PARAM_LIBPATH_STR	TEXT("-libpath=")
59 #define PARAM_LOCAL_STR		TEXT("-local")
60 #define PARAM_REMOTE_STR	TEXT("-remote")
61 
62 #if defined( REMOTE )
63 #define DEFAULT_LIBPATH		TEXT("remote;")
64 #elif defined( LOCAL )
65 #define DEFAULT_LIBPATH		TEXT("local;")
66 #endif
67 
Main()68 extern "C" int Main()
69 {
70 	// Retreive startup info
71 
72 	STARTUPINFO	aStartupInfo;
73 
74 	ZeroMemory( &aStartupInfo, sizeof(aStartupInfo) );
75 	aStartupInfo.cb = sizeof( aStartupInfo );
76 	GetStartupInfo( &aStartupInfo );
77 
78 	// Retrieve command line
79 
80 	LPTSTR	lpCommandLine = GetCommandLine();
81 
82 	LPTSTR	*ppArguments = NULL;
83 	int		nArguments = 0;
84 
85 	ppArguments = GetArgv( &nArguments );
86 
87 	// Calculate application name
88 
89 
90 	TCHAR	szApplicationName[MAX_PATH];
91 	TCHAR	szDrive[MAX_PATH];
92 	TCHAR	szDir[MAX_PATH];
93 	TCHAR	szFileName[MAX_PATH];
94 	TCHAR	szExt[MAX_PATH];
95 
96 	GetModuleFileName( NULL, szApplicationName, MAX_PATH );
97 	_tsplitpath( szApplicationName, szDrive, szDir, szFileName, szExt );
98 	_tmakepath( szApplicationName, szDrive, szDir, szFileName, BIN_EXT_STR );
99 
100 	// Retreive actual environment
101 
102 	TCHAR	szBuffer[1024];
103 	TCHAR	szPathValue[1024] = TEXT("");
104 
105 #ifdef DEFAULT_LIBPATH
106 	_tmakepath( szPathValue, szDrive, szDir, DEFAULT_LIBPATH, TEXT("") );
107 #endif
108 
109 	for ( int argn = 1; argn < nArguments; argn++ )
110 	{
111 		if ( 0 == _tcscmp( ppArguments[argn], PARAM_REMOTE_STR ) )
112 		{
113 			_tmakepath( szPathValue, szDrive, szDir, TEXT("remote;"), TEXT("") );
114 			break;
115 		}
116 		else if ( 0 == _tcscmp( ppArguments[argn], PARAM_LOCAL_STR ) )
117 		{
118 			_tmakepath( szPathValue, szDrive, szDir, TEXT("local;"), TEXT("") );
119 			break;
120 		}
121 		else if ( 0 == _tcsncmp( ppArguments[argn], PARAM_LIBPATH_STR, _tcslen(PARAM_LIBPATH_STR) ) )
122 		{
123 			LPTSTR	pFileSpec = NULL;
124 
125 			GetFullPathName( ppArguments[argn] + _tcslen(PARAM_LIBPATH_STR), sizeof(szPathValue) / sizeof(TCHAR), szPathValue, &pFileSpec );
126 			_tcscat( szPathValue, TEXT(";") );
127 			break;
128 		}
129 	}
130 
131 	GetEnvironmentVariable( TEXT("PATH"), szBuffer, sizeof(szBuffer) );
132 	_tcscat( szPathValue, szBuffer );
133 	SetEnvironmentVariable( TEXT("PATH"), szPathValue);
134 
135 	LPVOID	lpEnvironment = GetEnvironmentStrings();
136 
137 
138 	// Retrieve current directory
139 
140 	TCHAR				szCurrentDirectory[MAX_PATH];
141 	GetCurrentDirectory( MAX_PATH, szCurrentDirectory );
142 
143 	// Set the Flags
144 
145 	DWORD	dwCreationFlags = PROCESS_CREATIONFLAGS;
146 
147 	PROCESS_INFORMATION	aProcessInfo;
148 
149 	BOOL	fSuccess = CreateProcess(
150 		szApplicationName,
151 		lpCommandLine,
152 		NULL,
153 		NULL,
154 		TRUE,
155 		dwCreationFlags,
156 		lpEnvironment,
157 		szCurrentDirectory,
158 		&aStartupInfo,
159 		&aProcessInfo );
160 
161 	if ( fSuccess )
162 	{
163 		DWORD	dwExitCode;
164 
165 		WaitForSingleObject( aProcessInfo.hProcess, INFINITE );
166 		fSuccess = GetExitCodeProcess( aProcessInfo.hProcess, &dwExitCode );
167 
168 		return dwExitCode;
169 	}
170 
171 	DWORD	dwError = GetLastError();
172 
173 	LPVOID lpMsgBuf;
174 
175 	FormatMessage(
176 		FORMAT_MESSAGE_ALLOCATE_BUFFER |
177 		FORMAT_MESSAGE_FROM_SYSTEM,
178 		NULL,
179 		dwError,
180 		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
181 		(LPTSTR)&lpMsgBuf,
182 		0,
183 		NULL
184 	);
185 
186 	// Display the string.
187 	MessageBox( NULL, (LPCTSTR)lpMsgBuf, NULL, MB_OK | MB_ICONERROR );
188 
189 	// Free the buffer.
190 	LocalFree( lpMsgBuf );
191 
192 	return GetLastError();
193 }
194 
195