Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / setup_native / source / win32 / customactions / quickstarter / quickstarter.cxx
blobf09ecb2a3abbf91e868743c8f8b55185d73c47f8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 #include "quickstarter.hxx"
3 #ifdef _MSC_VER
4 #pragma warning(push, 1) /* disable warnings within system headers */
5 #endif
6 #include <psapi.h>
7 #ifdef _MSC_VER
8 #pragma warning(pop)
9 #endif
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("INSTALLLOCATION"), 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("INSTALLLOCATION"), 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;
80 static DWORD WINAPI _GetModuleFileNameExA( HANDLE hProcess, HMODULE hModule, LPSTR lpFileName, DWORD nSize )
82 typedef DWORD (WINAPI *FN_PROC)( HANDLE hProcess, HMODULE hModule, LPSTR lpFileName, DWORD nSize );
84 static FN_PROC lpProc = NULL;
86 if ( !lpProc )
88 HMODULE hLibrary = LoadLibrary("PSAPI.DLL");
90 if ( hLibrary )
91 lpProc = reinterpret_cast< FN_PROC >(GetProcAddress( hLibrary, "GetModuleFileNameExA" ));
94 if ( lpProc )
95 return lpProc( hProcess, hModule, lpFileName, nSize );
97 return 0;
101 std::string GetProcessImagePath( DWORD dwProcessId )
103 std::string sImagePath;
105 HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcessId );
107 if ( IsValidHandle( hProcess ) )
109 CHAR szPathBuffer[MAX_PATH] = "";
111 if ( _GetModuleFileNameExA( hProcess, NULL, szPathBuffer, sizeof(szPathBuffer) ) )
112 sImagePath = szPathBuffer;
114 CloseHandle( hProcess );
117 return sImagePath;
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */