xref: /trunk/main/setup_native/source/win32/customactions/shellextensions/migrateinstallpath.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #define _WIN32_WINDOWS 0x0410
29 #ifdef _MSC_VER
30 #pragma warning(push, 1) /* disable warnings within system headers */
31 #endif
32 #define WIN32_LEAN_AND_MEAN
33 #include <windows.h>
34 #include <msiquery.h>
35 #ifdef _MSC_VER
36 #pragma warning(pop)
37 #endif
38 
39 #include <malloc.h>
40 
41 #ifdef UNICODE
42 #define _UNICODE
43 #define _tstring    wstring
44 #else
45 #define _tstring    string
46 #endif
47 #include <tchar.h>
48 #include <string>
49 
50 using namespace std;
51 
52 namespace
53 {
54     std::_tstring GetMsiProperty( MSIHANDLE handle, const std::_tstring& sProperty )
55     {
56         std::_tstring   result;
57         TCHAR   szDummy[1] = TEXT("");
58         DWORD   nChars = 0;
59 
60         if ( MsiGetProperty( handle, sProperty.c_str(), szDummy, &nChars ) == ERROR_MORE_DATA )
61         {
62             DWORD nBytes = ++nChars * sizeof(TCHAR);
63             LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
64             ZeroMemory( buffer, nBytes );
65             MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
66             result = buffer;
67         }
68 
69         return  result;
70     }
71 } // namespace
72 
73 extern "C" UINT __stdcall MigrateInstallPath( MSIHANDLE handle )
74 {
75     TCHAR   szValue[8192];
76     DWORD   nValueSize = sizeof(szValue);
77     HKEY    hKey;
78     std::_tstring   sInstDir;
79 
80     std::_tstring   sManufacturer = GetMsiProperty( handle, TEXT("Manufacturer") );
81     std::_tstring   sDefinedName = GetMsiProperty( handle, TEXT("DEFINEDPRODUCT") );
82     std::_tstring   sUpdateVersion = GetMsiProperty( handle, TEXT("DEFINEDVERSION") );
83     std::_tstring   sUpgradeCode = GetMsiProperty( handle, TEXT("UpgradeCode") );
84 
85     std::_tstring   sProductKey = "Software\\" + sManufacturer + "\\" + sDefinedName +
86                                         "\\" + sUpdateVersion + "\\" + sUpgradeCode;
87 
88     std::_tstring   mystr;
89     mystr = "ProductKey: " + sProductKey;
90     // MessageBox( NULL, mystr.c_str(), "ProductKey", MB_OK );
91 
92     if ( ERROR_SUCCESS == RegOpenKey( HKEY_CURRENT_USER,  sProductKey.c_str(), &hKey ) )
93     {
94         if ( ERROR_SUCCESS == RegQueryValueEx( hKey, TEXT("INSTALLLOCATION"), NULL, NULL, (LPBYTE)szValue, &nValueSize ) )
95         {
96             sInstDir = szValue;
97             MsiSetProperty(handle, TEXT("INSTALLLOCATION"), sInstDir.c_str());
98             // MessageBox( NULL, sInstDir.c_str(), "Found in HKEY_CURRENT_USER", MB_OK );
99         }
100 
101         RegCloseKey( hKey );
102     }
103     else if ( ERROR_SUCCESS == RegOpenKey( HKEY_LOCAL_MACHINE,  sProductKey.c_str(), &hKey ) )
104     {
105         if ( ERROR_SUCCESS == RegQueryValueEx( hKey, TEXT("INSTALLLOCATION"), NULL, NULL, (LPBYTE)szValue, &nValueSize ) )
106         {
107             sInstDir = szValue;
108             MsiSetProperty(handle, TEXT("INSTALLLOCATION"), sInstDir.c_str());
109             // MessageBox( NULL, sInstDir.c_str(), "Found in HKEY_LOCAL_MACHINE", MB_OK );
110         }
111 
112         RegCloseKey( hKey );
113     }
114 
115     return ERROR_SUCCESS;
116 
117 }
118