bump product version to 4.1.6.2
[LibreOffice.git] / desktop / win32 / source / wrapper.h
blob17b81061df1af25cd1ebbd3135d44c82357a17a8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #pragma once
20 #ifndef __cplusplus
21 #error Need C++ to compile
22 #endif
24 #include "main.h"
26 #ifndef _WINDOWS_
27 #if defined _MSC_VER
28 #pragma warning(push, 1)
29 #endif
30 # include <windows.h>
31 #if defined _MSC_VER
32 #pragma warning(pop)
33 #endif
34 #endif
37 #ifndef _INC_TCHAR
38 # ifdef UNICODE
39 # define _UNICODE
40 # endif
41 # include <tchar.h>
42 #endif
44 #ifdef UNICODE
45 # define Main MainW
46 # define GetArgv( pArgc ) CommandLineToArgvW( GetCommandLine(), pArgc )
47 # define PROCESS_CREATIONFLAGS CREATE_UNICODE_ENVIRONMENT
48 #else
49 # define GetArgv( pArgc ) (*pArgc = __argc, __argv)
50 # define PROCESS_CREATIONFLAGS 0
51 # define Main MainA
52 #endif
54 #define BIN_EXT_STR TEXT(".bin")
55 #define PARAM_LIBPATH_STR TEXT("-libpath=")
56 #define PARAM_LOCAL_STR TEXT("-local")
57 #define PARAM_REMOTE_STR TEXT("-remote")
59 #if defined( REMOTE )
60 #define DEFAULT_LIBPATH TEXT("remote;")
61 #elif defined( LOCAL )
62 #define DEFAULT_LIBPATH TEXT("local;")
63 #endif
65 extern "C" int Main()
67 // Retrieve startup info
69 STARTUPINFO aStartupInfo;
71 ZeroMemory( &aStartupInfo, sizeof(aStartupInfo) );
72 aStartupInfo.cb = sizeof( aStartupInfo );
73 GetStartupInfo( &aStartupInfo );
75 // Retrieve command line
77 LPTSTR lpCommandLine = GetCommandLine();
79 LPTSTR *ppArguments = NULL;
80 int nArguments = 0;
82 ppArguments = GetArgv( &nArguments );
84 // Calculate application name
87 TCHAR szApplicationName[MAX_PATH];
88 TCHAR szDrive[MAX_PATH];
89 TCHAR szDir[MAX_PATH];
90 TCHAR szFileName[MAX_PATH];
91 TCHAR szExt[MAX_PATH];
93 GetModuleFileName( NULL, szApplicationName, MAX_PATH );
94 _tsplitpath( szApplicationName, szDrive, szDir, szFileName, szExt );
95 _tmakepath( szApplicationName, szDrive, szDir, szFileName, BIN_EXT_STR );
97 // Retrieve actual environment
99 TCHAR szBuffer[1024];
100 TCHAR szPathValue[1024] = TEXT("");
102 #ifdef DEFAULT_LIBPATH
103 _tmakepath( szPathValue, szDrive, szDir, DEFAULT_LIBPATH, TEXT("") );
104 #endif
106 for ( int argn = 1; argn < nArguments; argn++ )
108 if ( 0 == _tcscmp( ppArguments[argn], PARAM_REMOTE_STR ) )
110 _tmakepath( szPathValue, szDrive, szDir, TEXT("remote;"), TEXT("") );
111 break;
113 else if ( 0 == _tcscmp( ppArguments[argn], PARAM_LOCAL_STR ) )
115 _tmakepath( szPathValue, szDrive, szDir, TEXT("local;"), TEXT("") );
116 break;
118 else if ( 0 == _tcsncmp( ppArguments[argn], PARAM_LIBPATH_STR, _tcslen(PARAM_LIBPATH_STR) ) )
120 LPTSTR pFileSpec = NULL;
122 GetFullPathName( ppArguments[argn] + _tcslen(PARAM_LIBPATH_STR), sizeof(szPathValue) / sizeof(TCHAR), szPathValue, &pFileSpec );
123 _tcscat( szPathValue, TEXT(";") );
124 break;
128 GetEnvironmentVariable( TEXT("PATH"), szBuffer, sizeof(szBuffer) );
129 _tcscat( szPathValue, szBuffer );
130 SetEnvironmentVariable( TEXT("PATH"), szPathValue);
132 LPVOID lpEnvironment = GetEnvironmentStrings();
135 // Retrieve current directory
137 TCHAR szCurrentDirectory[MAX_PATH];
138 GetCurrentDirectory( MAX_PATH, szCurrentDirectory );
140 // Set the Flags
142 DWORD dwCreationFlags = PROCESS_CREATIONFLAGS;
144 PROCESS_INFORMATION aProcessInfo;
146 BOOL fSuccess = CreateProcess(
147 szApplicationName,
148 lpCommandLine,
149 NULL,
150 NULL,
151 TRUE,
152 dwCreationFlags,
153 lpEnvironment,
154 szCurrentDirectory,
155 &aStartupInfo,
156 &aProcessInfo );
158 if ( fSuccess )
160 DWORD dwExitCode;
162 WaitForSingleObject( aProcessInfo.hProcess, INFINITE );
163 fSuccess = GetExitCodeProcess( aProcessInfo.hProcess, &dwExitCode );
165 return dwExitCode;
168 DWORD dwError = GetLastError();
170 LPVOID lpMsgBuf;
172 FormatMessage(
173 FORMAT_MESSAGE_ALLOCATE_BUFFER |
174 FORMAT_MESSAGE_FROM_SYSTEM,
175 NULL,
176 dwError,
177 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
178 (LPTSTR)&lpMsgBuf,
180 NULL
183 // Display the string.
184 MessageBox( NULL, (LPCTSTR)lpMsgBuf, NULL, MB_OK | MB_ICONERROR );
186 // Free the buffer.
187 LocalFree( lpMsgBuf );
189 return GetLastError();
192 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */