Update ooo320-m1
[ooovba.git] / setup_native / source / win32 / customactions / quickstarter / quickstarter.cxx
blob51293ab7f32eb801fb66850e7649f06db124f702
1 #include "quickstarter.hxx"
2 #ifdef _MSC_VER
3 #pragma warning(push, 1) /* disable warnings within system headers */
4 #endif
5 #include <psapi.h>
6 #ifdef _MSC_VER
7 #pragma warning(pop)
8 #endif
9 #include <tlhelp32.h>
10 #include <malloc.h>
12 std::string GetOfficeInstallationPath(MSIHANDLE handle)
14 std::string progpath;
15 DWORD sz = 0;
16 LPTSTR dummy = TEXT("");
18 if (MsiGetProperty(handle, TEXT("OFFICEINSTALLLOCATION"), dummy, &sz) == ERROR_MORE_DATA)
20 sz++; // space for the final '\0'
21 DWORD nbytes = sz * sizeof(TCHAR);
22 LPTSTR buff = reinterpret_cast<LPTSTR>(_alloca(nbytes));
23 ZeroMemory(buff, nbytes);
24 MsiGetProperty(handle, TEXT("OFFICEINSTALLLOCATION"), buff, &sz);
25 progpath = buff;
27 return progpath;
30 std::string GetOfficeProductName(MSIHANDLE handle)
32 std::string productname;
33 DWORD sz = 0;
34 LPTSTR dummy = TEXT("");
36 if (MsiGetProperty(handle, TEXT("ProductName"), dummy, &sz) == ERROR_MORE_DATA)
38 sz++; // space for the final '\0'
39 DWORD nbytes = sz * sizeof(TCHAR);
40 LPTSTR buff = reinterpret_cast<LPTSTR>(_alloca(nbytes));
41 ZeroMemory(buff, nbytes);
42 MsiGetProperty(handle, TEXT("ProductName"), buff, &sz);
43 productname = buff;
45 return productname;
48 std::string GetQuickstarterLinkName(MSIHANDLE handle)
50 std::string quickstarterlinkname;
51 DWORD sz = 0;
52 LPTSTR dummy = TEXT("");
54 if (MsiGetProperty(handle, TEXT("Quickstarterlinkname"), dummy, &sz) == ERROR_MORE_DATA)
56 sz++; // space for the final '\0'
57 DWORD nbytes = sz * sizeof(TCHAR);
58 LPTSTR buff = reinterpret_cast<LPTSTR>(_alloca(nbytes));
59 ZeroMemory(buff, nbytes);
60 MsiGetProperty(handle, TEXT("Quickstarterlinkname"), buff, &sz);
61 quickstarterlinkname = buff;
63 else if (MsiGetProperty(handle, TEXT("ProductName"), dummy, &sz) == ERROR_MORE_DATA)
65 sz++; // space for the final '\0'
66 DWORD nbytes = sz * sizeof(TCHAR);
67 LPTSTR buff = reinterpret_cast<LPTSTR>(_alloca(nbytes));
68 ZeroMemory(buff, nbytes);
69 MsiGetProperty(handle, TEXT("ProductName"), buff, &sz);
70 quickstarterlinkname = buff;
72 return quickstarterlinkname;
75 inline bool IsValidHandle( HANDLE handle )
77 return NULL != handle && INVALID_HANDLE_VALUE != handle;
81 static HANDLE WINAPI _CreateToolhelp32Snapshot( DWORD dwFlags, DWORD th32ProcessID )
83 typedef HANDLE (WINAPI *FN_PROC)( DWORD dwFlags, DWORD th32ProcessID );
84 static FN_PROC lpProc = NULL;
86 HANDLE hSnapshot = NULL;
88 if ( !lpProc )
90 HMODULE hLibrary = GetModuleHandle("KERNEL32.DLL");
92 if ( hLibrary )
93 lpProc = reinterpret_cast< FN_PROC >(GetProcAddress( hLibrary, "CreateToolhelp32Snapshot" ));
96 if ( lpProc )
97 hSnapshot = lpProc( dwFlags, th32ProcessID );
99 return hSnapshot;
102 static BOOL WINAPI _Process32First( HANDLE hSnapshot, PROCESSENTRY32 *lppe32 )
104 typedef BOOL (WINAPI *FN_PROC)( HANDLE hSnapshot, PROCESSENTRY32 *lppe32 );
105 static FN_PROC lpProc = NULL;
107 BOOL fSuccess = FALSE;
109 if ( !lpProc )
111 HMODULE hLibrary = GetModuleHandle("KERNEL32.DLL");
113 if ( hLibrary )
114 lpProc = reinterpret_cast< FN_PROC >(GetProcAddress( hLibrary, "Process32First" ));
117 if ( lpProc )
118 fSuccess = lpProc( hSnapshot, lppe32 );
120 return fSuccess;
123 static BOOL WINAPI _Process32Next( HANDLE hSnapshot, PROCESSENTRY32 *lppe32 )
125 typedef BOOL (WINAPI *FN_PROC)( HANDLE hSnapshot, PROCESSENTRY32 *lppe32 );
126 static FN_PROC lpProc = NULL;
128 BOOL fSuccess = FALSE;
130 if ( !lpProc )
132 HMODULE hLibrary = GetModuleHandle("KERNEL32.DLL");
134 if ( hLibrary )
135 lpProc = reinterpret_cast< FN_PROC >(GetProcAddress( hLibrary, "Process32Next" ));
138 if ( lpProc )
139 fSuccess = lpProc( hSnapshot, lppe32 );
141 return fSuccess;
144 static std::string GetProcessImagePath_9x( DWORD dwProcessId )
146 std::string sImagePath;
148 HANDLE hSnapshot = _CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
150 if ( IsValidHandle( hSnapshot ) )
152 PROCESSENTRY32 pe32 = { 0 };
154 pe32.dwSize = sizeof(PROCESSENTRY32);
156 BOOL fSuccess = _Process32First( hSnapshot, &pe32 );
157 bool found = false;
159 while ( !found && fSuccess )
161 if ( pe32.th32ProcessID == dwProcessId )
163 found = true;
164 sImagePath = pe32.szExeFile;
167 if ( !found )
168 fSuccess = _Process32Next( hSnapshot, &pe32 );
171 CloseHandle( hSnapshot );
174 return sImagePath;
177 static DWORD WINAPI _GetModuleFileNameExA( HANDLE hProcess, HMODULE hModule, LPSTR lpFileName, DWORD nSize )
179 typedef DWORD (WINAPI *FN_PROC)( HANDLE hProcess, HMODULE hModule, LPSTR lpFileName, DWORD nSize );
181 static FN_PROC lpProc = NULL;
183 if ( !lpProc )
185 HMODULE hLibrary = LoadLibrary("PSAPI.DLL");
187 if ( hLibrary )
188 lpProc = reinterpret_cast< FN_PROC >(GetProcAddress( hLibrary, "GetModuleFileNameExA" ));
191 if ( lpProc )
192 return lpProc( hProcess, hModule, lpFileName, nSize );
194 return 0;
198 static std::string GetProcessImagePath_NT( DWORD dwProcessId )
200 std::string sImagePath;
202 HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcessId );
204 if ( IsValidHandle( hProcess ) )
206 CHAR szPathBuffer[MAX_PATH] = "";
208 if ( _GetModuleFileNameExA( hProcess, NULL, szPathBuffer, sizeof(szPathBuffer) ) )
209 sImagePath = szPathBuffer;
211 CloseHandle( hProcess );
214 return sImagePath;
217 std::string GetProcessImagePath( DWORD dwProcessId )
219 return (LONG)GetVersion() < 0 ? GetProcessImagePath_9x( dwProcessId ) : GetProcessImagePath_NT( dwProcessId );