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