xref: /trunk/main/desktop/win32/source/wrapper.h (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 #pragma once
2 #ifndef __cplusplus
3 #error Need C++ to compile
4 #endif
5 
6 #include "main.h"
7 
8 #ifndef _WINDOWS_
9 #if defined _MSC_VER
10 #pragma warning(push, 1)
11 #endif
12 #   include <windows.h>
13 #if defined _MSC_VER
14 #pragma warning(pop)
15 #endif
16 #endif
17 
18 
19 #ifndef _INC_TCHAR
20 #   ifdef UNICODE
21 #       define _UNICODE
22 #   endif
23 #   include <tchar.h>
24 #endif
25 
26 #ifdef UNICODE
27 #   define Main MainW
28 #   define GetArgv( pArgc )         CommandLineToArgvW( GetCommandLine(), pArgc )
29 #   define PROCESS_CREATIONFLAGS    CREATE_UNICODE_ENVIRONMENT
30 #else
31 #   define GetArgv( pArgc )         (*pArgc = __argc, __argv)
32 #   define PROCESS_CREATIONFLAGS    0
33 #   define Main MainA
34 #endif
35 
36 #define BIN_EXT_STR         TEXT(".bin")
37 #define PARAM_LIBPATH_STR   TEXT("-libpath=")
38 #define PARAM_LOCAL_STR     TEXT("-local")
39 #define PARAM_REMOTE_STR    TEXT("-remote")
40 
41 #if defined( REMOTE )
42 #define DEFAULT_LIBPATH     TEXT("remote;")
43 #elif defined( LOCAL )
44 #define DEFAULT_LIBPATH     TEXT("local;")
45 #endif
46 
47 extern "C" int Main()
48 {
49     // Retreive startup info
50 
51     STARTUPINFO aStartupInfo;
52 
53     ZeroMemory( &aStartupInfo, sizeof(aStartupInfo) );
54     aStartupInfo.cb = sizeof( aStartupInfo );
55     GetStartupInfo( &aStartupInfo );
56 
57     // Retrieve command line
58 
59     LPTSTR  lpCommandLine = GetCommandLine();
60 
61     LPTSTR  *ppArguments = NULL;
62     int     nArguments = 0;
63 
64     ppArguments = GetArgv( &nArguments );
65 
66     // Calculate application name
67 
68 
69     TCHAR   szApplicationName[MAX_PATH];
70     TCHAR   szDrive[MAX_PATH];
71     TCHAR   szDir[MAX_PATH];
72     TCHAR   szFileName[MAX_PATH];
73     TCHAR   szExt[MAX_PATH];
74 
75     GetModuleFileName( NULL, szApplicationName, MAX_PATH );
76     _tsplitpath( szApplicationName, szDrive, szDir, szFileName, szExt );
77     _tmakepath( szApplicationName, szDrive, szDir, szFileName, BIN_EXT_STR );
78 
79     // Retreive actual environment
80 
81     TCHAR   szBuffer[1024];
82     TCHAR   szPathValue[1024] = TEXT("");
83 
84 #ifdef DEFAULT_LIBPATH
85     _tmakepath( szPathValue, szDrive, szDir, DEFAULT_LIBPATH, TEXT("") );
86 #endif
87 
88     for ( int argn = 1; argn < nArguments; argn++ )
89     {
90         if ( 0 == _tcscmp( ppArguments[argn], PARAM_REMOTE_STR ) )
91         {
92             _tmakepath( szPathValue, szDrive, szDir, TEXT("remote;"), TEXT("") );
93             break;
94         }
95         else if ( 0 == _tcscmp( ppArguments[argn], PARAM_LOCAL_STR ) )
96         {
97             _tmakepath( szPathValue, szDrive, szDir, TEXT("local;"), TEXT("") );
98             break;
99         }
100         else if ( 0 == _tcsncmp( ppArguments[argn], PARAM_LIBPATH_STR, _tcslen(PARAM_LIBPATH_STR) ) )
101         {
102             LPTSTR  pFileSpec = NULL;
103 
104             GetFullPathName( ppArguments[argn] + _tcslen(PARAM_LIBPATH_STR), sizeof(szPathValue) / sizeof(TCHAR), szPathValue, &pFileSpec );
105             _tcscat( szPathValue, TEXT(";") );
106             break;
107         }
108     }
109 
110     GetEnvironmentVariable( TEXT("PATH"), szBuffer, sizeof(szBuffer) );
111     _tcscat( szPathValue, szBuffer );
112     SetEnvironmentVariable( TEXT("PATH"), szPathValue);
113 
114     LPVOID  lpEnvironment = GetEnvironmentStrings();
115 
116 
117     // Retrieve current directory
118 
119     TCHAR               szCurrentDirectory[MAX_PATH];
120     GetCurrentDirectory( MAX_PATH, szCurrentDirectory );
121 
122     // Set the Flags
123 
124     DWORD   dwCreationFlags = PROCESS_CREATIONFLAGS;
125 
126     PROCESS_INFORMATION aProcessInfo;
127 
128     BOOL    fSuccess = CreateProcess(
129         szApplicationName,
130         lpCommandLine,
131         NULL,
132         NULL,
133         TRUE,
134         dwCreationFlags,
135         lpEnvironment,
136         szCurrentDirectory,
137         &aStartupInfo,
138         &aProcessInfo );
139 
140     if ( fSuccess )
141     {
142         DWORD   dwExitCode;
143 
144         WaitForSingleObject( aProcessInfo.hProcess, INFINITE );
145         fSuccess = GetExitCodeProcess( aProcessInfo.hProcess, &dwExitCode );
146 
147         return dwExitCode;
148     }
149 
150     DWORD   dwError = GetLastError();
151 
152     LPVOID lpMsgBuf;
153 
154     FormatMessage(
155         FORMAT_MESSAGE_ALLOCATE_BUFFER |
156         FORMAT_MESSAGE_FROM_SYSTEM,
157         NULL,
158         dwError,
159         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
160         (LPTSTR)&lpMsgBuf,
161         0,
162         NULL
163     );
164 
165     // Display the string.
166     MessageBox( NULL, (LPCTSTR)lpMsgBuf, NULL, MB_OK | MB_ICONERROR );
167 
168     // Free the buffer.
169     LocalFree( lpMsgBuf );
170 
171     return GetLastError();
172 }
173 
174