1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include "quickstarter.hxx"
21 
22 #include <psapi.h>
23 
24 #include <malloc.h>
25 
GetOfficeInstallationPathW(MSIHANDLE handle)26 std::wstring GetOfficeInstallationPathW(MSIHANDLE handle)
27 {
28     std::wstring progpath;
29     DWORD sz = 0;
30     PWSTR dummy = const_cast<PWSTR>(L"");
31 
32     if (MsiGetPropertyW(handle, L"INSTALLLOCATION", dummy, &sz) == ERROR_MORE_DATA)
33     {
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;
40     }
41     return progpath;
42 }
43 
GetOfficeProductNameW(MSIHANDLE handle)44 std::wstring GetOfficeProductNameW(MSIHANDLE handle)
45 {
46     std::wstring productname;
47     DWORD sz = 0;
48     PWSTR dummy = const_cast<PWSTR>(L"");
49 
50     if (MsiGetPropertyW(handle, L"ProductName", dummy, &sz) == ERROR_MORE_DATA)
51     {
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;
58     }
59     return productname;
60 }
61 
GetQuickstarterLinkNameW(MSIHANDLE handle)62 std::wstring GetQuickstarterLinkNameW(MSIHANDLE handle)
63 {
64     std::wstring quickstarterlinkname;
65     DWORD sz = 0;
66     PWSTR dummy = const_cast<PWSTR>(L"");
67 
68     if (MsiGetPropertyW(handle, L"Quickstarterlinkname", dummy, &sz) == ERROR_MORE_DATA)
69     {
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;
76     }
77     else if (MsiGetPropertyW(handle, L"ProductName", dummy, &sz) == ERROR_MORE_DATA)
78     {
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;
85     }
86     return quickstarterlinkname;
87 }
88 
IsValidHandle(HANDLE handle)89 static bool IsValidHandle( HANDLE handle )
90 {
91     return nullptr != handle && INVALID_HANDLE_VALUE != handle;
92 }
93 
GetModuleFileNameExW_(HANDLE hProcess,HMODULE hModule,PWSTR lpFileName,DWORD nSize)94 static DWORD WINAPI GetModuleFileNameExW_( HANDLE hProcess, HMODULE hModule, PWSTR lpFileName, DWORD nSize )
95 {
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     }();
104 
105     if ( lpProc )
106         return lpProc( hProcess, hModule, lpFileName, nSize );
107 
108     return 0;
109 
110 }
111 
GetProcessImagePathW(DWORD dwProcessId)112 std::wstring GetProcessImagePathW( DWORD dwProcessId )
113 {
114     std::wstring sImagePath;
115 
116     HANDLE  hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcessId );
117 
118     if ( IsValidHandle( hProcess ) )
119     {
120         WCHAR szPathBuffer[MAX_PATH] = L"";
121 
122         if ( GetModuleFileNameExW_( hProcess, nullptr, szPathBuffer, sizeof(szPathBuffer)/sizeof(szPathBuffer[0]) ) )
123             sImagePath = szPathBuffer;
124 
125         CloseHandle( hProcess );
126     }
127 
128     return sImagePath;
129 }
130 
131 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
132