tdf#133368: update extensions website URLs and use https
[LibreOffice.git] / setup_native / source / win32 / customactions / quickstarter / quickstarter.cxx
blob960274f393d0e9daa889f90fdf95382827909b28
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 "quickstarter.hxx"
22 #include <psapi.h>
24 #include <malloc.h>
26 std::wstring GetOfficeInstallationPathW(MSIHANDLE handle)
28 std::wstring progpath;
29 DWORD sz = 0;
30 PWSTR dummy = const_cast<PWSTR>(L"");
32 if (MsiGetPropertyW(handle, L"INSTALLLOCATION", dummy, &sz) == ERROR_MORE_DATA)
34 sz++; // space for the final '\0'
35 DWORD nbytes = sz * sizeof(WCHAR);
36 PWSTR buff = static_cast<PWSTR>(_alloca(nbytes));
37 ZeroMemory(buff, nbytes);
38 MsiGetPropertyW(handle, L"INSTALLLOCATION", buff, &sz);
39 progpath = buff;
41 return progpath;
44 std::wstring GetOfficeProductNameW(MSIHANDLE handle)
46 std::wstring productname;
47 DWORD sz = 0;
48 PWSTR dummy = const_cast<PWSTR>(L"");
50 if (MsiGetPropertyW(handle, L"ProductName", dummy, &sz) == ERROR_MORE_DATA)
52 sz++; // space for the final '\0'
53 DWORD nbytes = sz * sizeof(WCHAR);
54 PWSTR buff = static_cast<PWSTR>(_alloca(nbytes));
55 ZeroMemory(buff, nbytes);
56 MsiGetPropertyW(handle, L"ProductName", buff, &sz);
57 productname = buff;
59 return productname;
62 std::wstring GetQuickstarterLinkNameW(MSIHANDLE handle)
64 std::wstring quickstarterlinkname;
65 DWORD sz = 0;
66 PWSTR dummy = const_cast<PWSTR>(L"");
68 if (MsiGetPropertyW(handle, L"Quickstarterlinkname", dummy, &sz) == ERROR_MORE_DATA)
70 sz++; // space for the final '\0'
71 DWORD nbytes = sz * sizeof(WCHAR);
72 PWSTR buff = static_cast<PWSTR>(_alloca(nbytes));
73 ZeroMemory(buff, nbytes);
74 MsiGetPropertyW(handle, L"Quickstarterlinkname", buff, &sz);
75 quickstarterlinkname = buff;
77 else if (MsiGetPropertyW(handle, L"ProductName", dummy, &sz) == ERROR_MORE_DATA)
79 sz++; // space for the final '\0'
80 DWORD nbytes = sz * sizeof(WCHAR);
81 PWSTR buff = static_cast<PWSTR>(_alloca(nbytes));
82 ZeroMemory(buff, nbytes);
83 MsiGetPropertyW(handle, L"ProductName", buff, &sz);
84 quickstarterlinkname = buff;
86 return quickstarterlinkname;
89 static bool IsValidHandle( HANDLE handle )
91 return nullptr != handle && INVALID_HANDLE_VALUE != handle;
94 static DWORD WINAPI GetModuleFileNameExW_( HANDLE hProcess, HMODULE hModule, PWSTR lpFileName, DWORD nSize )
96 static auto lpProc = []() {
97 HMODULE hLibrary = LoadLibraryW(L"PSAPI.DLL");
98 decltype(GetModuleFileNameExW)* pRet = nullptr;
99 if (hLibrary)
100 pRet = reinterpret_cast<decltype(GetModuleFileNameExW)*>(
101 GetProcAddress(hLibrary, "GetModuleFileNameExW"));
102 return pRet;
103 }();
105 if ( lpProc )
106 return lpProc( hProcess, hModule, lpFileName, nSize );
108 return 0;
112 std::wstring GetProcessImagePathW( DWORD dwProcessId )
114 std::wstring sImagePath;
116 HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcessId );
118 if ( IsValidHandle( hProcess ) )
120 WCHAR szPathBuffer[MAX_PATH] = L"";
122 if ( GetModuleFileNameExW_( hProcess, nullptr, szPathBuffer, sizeof(szPathBuffer)/sizeof(szPathBuffer[0]) ) )
123 sImagePath = szPathBuffer;
125 CloseHandle( hProcess );
128 return sImagePath;
131 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */