Avoid potential negative array index access to cached text.
[LibreOffice.git] / desktop / win32 / source / applauncher / launcher.cxx
blobbf80dba2cc5a6b4c3ad5ac67474f168919f1e84d
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 .
20 #include "launcher.hxx"
22 #include <stdlib.h>
23 #include <malloc.h>
25 extern "C" int APIENTRY wWinMain( HINSTANCE, HINSTANCE, LPWSTR, int )
27 // Retrieve startup info
29 STARTUPINFOW aStartupInfo;
31 ZeroMemory( &aStartupInfo, sizeof(aStartupInfo) );
32 aStartupInfo.cb = sizeof( aStartupInfo );
33 GetStartupInfoW( &aStartupInfo );
35 // Retrieve command line
37 LPWSTR lpCommandLine = static_cast<LPWSTR>(_alloca( sizeof(WCHAR) * (wcslen(GetCommandLineW()) + wcslen(APPLICATION_SWITCH) + 2) ));
39 wcscpy( lpCommandLine, GetCommandLineW() );
40 wcscat( lpCommandLine, L" " );
41 wcscat( lpCommandLine, APPLICATION_SWITCH );
43 // Calculate application name
45 WCHAR szApplicationName[MAX_PATH];
46 WCHAR szDrive[MAX_PATH];
47 WCHAR szDir[MAX_PATH];
48 WCHAR szFileName[MAX_PATH];
49 WCHAR szExt[MAX_PATH];
51 GetModuleFileNameW( nullptr, szApplicationName, MAX_PATH );
52 _wsplitpath( szApplicationName, szDrive, szDir, szFileName, szExt );
53 _wmakepath( szApplicationName, szDrive, szDir, L"soffice", L".exe" );
55 PROCESS_INFORMATION aProcessInfo;
57 bool fSuccess = CreateProcessW(
58 szApplicationName,
59 lpCommandLine,
60 nullptr,
61 nullptr,
62 TRUE,
64 nullptr,
65 nullptr,
66 &aStartupInfo,
67 &aProcessInfo );
69 if ( fSuccess )
71 // Wait for soffice process to be terminated to allow other applications
72 // to wait for termination of started process
74 WaitForSingleObject( aProcessInfo.hProcess, INFINITE );
76 CloseHandle( aProcessInfo.hProcess );
77 CloseHandle( aProcessInfo.hThread );
79 return 0;
82 DWORD dwError = GetLastError();
84 LPWSTR lpMsgBuf = nullptr;
86 FormatMessageW(
87 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
88 nullptr,
89 dwError,
90 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
91 reinterpret_cast<LPWSTR>(&lpMsgBuf),
93 nullptr
96 // Display the string.
97 MessageBoxW( nullptr, lpMsgBuf, nullptr, MB_OK | MB_ICONERROR );
99 // Free the buffer.
100 HeapFree( GetProcessHeap(), 0, lpMsgBuf );
102 return dwError;
105 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */