Update ooo320-m1
[ooovba.git] / desktop / win32 / source / wrapper.h
blob9e525ca5a99301209c6f1d80417fc7cbcd48f4f9
1 #pragma once
2 #ifndef __cplusplus
3 #error Need C++ to compile
4 #endif
6 #include "main.h"
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
19 #ifndef _INC_TCHAR
20 # ifdef UNICODE
21 # define _UNICODE
22 # endif
23 # include <tchar.h>
24 #endif
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
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")
41 #if defined( REMOTE )
42 #define DEFAULT_LIBPATH TEXT("remote;")
43 #elif defined( LOCAL )
44 #define DEFAULT_LIBPATH TEXT("local;")
45 #endif
47 extern "C" int Main()
49 // Retreive startup info
51 STARTUPINFO aStartupInfo;
53 ZeroMemory( &aStartupInfo, sizeof(aStartupInfo) );
54 aStartupInfo.cb = sizeof( aStartupInfo );
55 GetStartupInfo( &aStartupInfo );
57 // Retrieve command line
59 LPTSTR lpCommandLine = GetCommandLine();
61 LPTSTR *ppArguments = NULL;
62 int nArguments = 0;
64 ppArguments = GetArgv( &nArguments );
66 // Calculate application name
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];
75 GetModuleFileName( NULL, szApplicationName, MAX_PATH );
76 _tsplitpath( szApplicationName, szDrive, szDir, szFileName, szExt );
77 _tmakepath( szApplicationName, szDrive, szDir, szFileName, BIN_EXT_STR );
79 // Retreive actual environment
81 TCHAR szBuffer[1024];
82 TCHAR szPathValue[1024] = TEXT("");
84 #ifdef DEFAULT_LIBPATH
85 _tmakepath( szPathValue, szDrive, szDir, DEFAULT_LIBPATH, TEXT("") );
86 #endif
88 for ( int argn = 1; argn < nArguments; argn++ )
90 if ( 0 == _tcscmp( ppArguments[argn], PARAM_REMOTE_STR ) )
92 _tmakepath( szPathValue, szDrive, szDir, TEXT("remote;"), TEXT("") );
93 break;
95 else if ( 0 == _tcscmp( ppArguments[argn], PARAM_LOCAL_STR ) )
97 _tmakepath( szPathValue, szDrive, szDir, TEXT("local;"), TEXT("") );
98 break;
100 else if ( 0 == _tcsncmp( ppArguments[argn], PARAM_LIBPATH_STR, _tcslen(PARAM_LIBPATH_STR) ) )
102 LPTSTR pFileSpec = NULL;
104 GetFullPathName( ppArguments[argn] + _tcslen(PARAM_LIBPATH_STR), sizeof(szPathValue) / sizeof(TCHAR), szPathValue, &pFileSpec );
105 _tcscat( szPathValue, TEXT(";") );
106 break;
110 GetEnvironmentVariable( TEXT("PATH"), szBuffer, sizeof(szBuffer) );
111 _tcscat( szPathValue, szBuffer );
112 SetEnvironmentVariable( TEXT("PATH"), szPathValue);
114 LPVOID lpEnvironment = GetEnvironmentStrings();
117 // Retrieve current directory
119 TCHAR szCurrentDirectory[MAX_PATH];
120 GetCurrentDirectory( MAX_PATH, szCurrentDirectory );
122 // Set the Flags
124 DWORD dwCreationFlags = PROCESS_CREATIONFLAGS;
126 PROCESS_INFORMATION aProcessInfo;
128 BOOL fSuccess = CreateProcess(
129 szApplicationName,
130 lpCommandLine,
131 NULL,
132 NULL,
133 TRUE,
134 dwCreationFlags,
135 lpEnvironment,
136 szCurrentDirectory,
137 &aStartupInfo,
138 &aProcessInfo );
140 if ( fSuccess )
142 DWORD dwExitCode;
144 WaitForSingleObject( aProcessInfo.hProcess, INFINITE );
145 fSuccess = GetExitCodeProcess( aProcessInfo.hProcess, &dwExitCode );
147 return dwExitCode;
150 DWORD dwError = GetLastError();
152 LPVOID lpMsgBuf;
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,
162 NULL
165 // Display the string.
166 MessageBox( NULL, (LPCTSTR)lpMsgBuf, NULL, MB_OK | MB_ICONERROR );
168 // Free the buffer.
169 LocalFree( lpMsgBuf );
171 return GetLastError();