1*32b1fd08SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*32b1fd08SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*32b1fd08SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*32b1fd08SAndrew Rist  * distributed with this work for additional information
6*32b1fd08SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*32b1fd08SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*32b1fd08SAndrew Rist  * "License"); you may not use this file except in compliance
9*32b1fd08SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*32b1fd08SAndrew Rist  *
11*32b1fd08SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*32b1fd08SAndrew Rist  *
13*32b1fd08SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*32b1fd08SAndrew Rist  * software distributed under the License is distributed on an
15*32b1fd08SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*32b1fd08SAndrew Rist  * KIND, either express or implied.  See the License for the
17*32b1fd08SAndrew Rist  * specific language governing permissions and limitations
18*32b1fd08SAndrew Rist  * under the License.
19*32b1fd08SAndrew Rist  *
20*32b1fd08SAndrew Rist  *************************************************************/
21*32b1fd08SAndrew Rist 
22*32b1fd08SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #define _WIN32_WINDOWS 0x0410
25cdf0e10cSrcweir #ifdef _MSC_VER
26cdf0e10cSrcweir #pragma warning(push, 1) /* disable warnings within system headers */
27cdf0e10cSrcweir #endif
28cdf0e10cSrcweir #define WIN32_LEAN_AND_MEAN
29cdf0e10cSrcweir #include <windows.h>
30cdf0e10cSrcweir #include <msiquery.h>
31cdf0e10cSrcweir #ifdef _MSC_VER
32cdf0e10cSrcweir #pragma warning(pop)
33cdf0e10cSrcweir #endif
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <malloc.h>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #ifdef UNICODE
38cdf0e10cSrcweir #define _UNICODE
39cdf0e10cSrcweir #define _tstring	wstring
40cdf0e10cSrcweir #else
41cdf0e10cSrcweir #define _tstring	string
42cdf0e10cSrcweir #endif
43cdf0e10cSrcweir #include <tchar.h>
44cdf0e10cSrcweir #include <string>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir using namespace std;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir namespace
49cdf0e10cSrcweir {
GetMsiProperty(MSIHANDLE handle,const std::_tstring & sProperty)50cdf0e10cSrcweir 	std::_tstring GetMsiProperty( MSIHANDLE handle, const std::_tstring& sProperty )
51cdf0e10cSrcweir 	{
52cdf0e10cSrcweir 		std::_tstring	result;
53cdf0e10cSrcweir 		TCHAR	szDummy[1] = TEXT("");
54cdf0e10cSrcweir 		DWORD	nChars = 0;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir 		if ( MsiGetProperty( handle, sProperty.c_str(), szDummy, &nChars ) == ERROR_MORE_DATA )
57cdf0e10cSrcweir 		{
58cdf0e10cSrcweir 			DWORD nBytes = ++nChars * sizeof(TCHAR);
59cdf0e10cSrcweir 			LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
60cdf0e10cSrcweir 			ZeroMemory( buffer, nBytes );
61cdf0e10cSrcweir 			MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
62cdf0e10cSrcweir 			result = buffer;
63cdf0e10cSrcweir 		}
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 		return	result;
66cdf0e10cSrcweir 	}
67cdf0e10cSrcweir } // namespace
68cdf0e10cSrcweir 
MigrateInstallPath(MSIHANDLE handle)69cdf0e10cSrcweir extern "C" UINT __stdcall MigrateInstallPath( MSIHANDLE handle )
70cdf0e10cSrcweir {
71cdf0e10cSrcweir 	TCHAR	szValue[8192];
72cdf0e10cSrcweir 	DWORD	nValueSize = sizeof(szValue);
73cdf0e10cSrcweir 	HKEY	hKey;
74cdf0e10cSrcweir 	std::_tstring	sInstDir;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 	std::_tstring	sManufacturer = GetMsiProperty( handle, TEXT("Manufacturer") );
77cdf0e10cSrcweir 	std::_tstring	sDefinedName = GetMsiProperty( handle, TEXT("DEFINEDPRODUCT") );
78cdf0e10cSrcweir 	std::_tstring	sUpdateVersion = GetMsiProperty( handle, TEXT("DEFINEDVERSION") );
79cdf0e10cSrcweir 	std::_tstring	sUpgradeCode = GetMsiProperty( handle, TEXT("UpgradeCode") );
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 	std::_tstring	sProductKey = "Software\\" + sManufacturer + "\\" + sDefinedName +
82cdf0e10cSrcweir 										"\\" + sUpdateVersion + "\\" + sUpgradeCode;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 	std::_tstring	mystr;
85cdf0e10cSrcweir 	mystr = "ProductKey: " + sProductKey;
86cdf0e10cSrcweir 	// MessageBox( NULL, mystr.c_str(), "ProductKey", MB_OK );
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 	if ( ERROR_SUCCESS == RegOpenKey( HKEY_CURRENT_USER,  sProductKey.c_str(), &hKey ) )
89cdf0e10cSrcweir 	{
90cdf0e10cSrcweir 		if ( ERROR_SUCCESS == RegQueryValueEx( hKey, TEXT("INSTALLLOCATION"), NULL, NULL, (LPBYTE)szValue, &nValueSize ) )
91cdf0e10cSrcweir 		{
92cdf0e10cSrcweir 			sInstDir = szValue;
93cdf0e10cSrcweir 			MsiSetProperty(handle, TEXT("INSTALLLOCATION"), sInstDir.c_str());
94cdf0e10cSrcweir 			// MessageBox( NULL, sInstDir.c_str(), "Found in HKEY_CURRENT_USER", MB_OK );
95cdf0e10cSrcweir 		}
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 		RegCloseKey( hKey );
98cdf0e10cSrcweir 	}
99cdf0e10cSrcweir 	else if ( ERROR_SUCCESS == RegOpenKey( HKEY_LOCAL_MACHINE,  sProductKey.c_str(), &hKey ) )
100cdf0e10cSrcweir 	{
101cdf0e10cSrcweir 		if ( ERROR_SUCCESS == RegQueryValueEx( hKey, TEXT("INSTALLLOCATION"), NULL, NULL, (LPBYTE)szValue, &nValueSize ) )
102cdf0e10cSrcweir 		{
103cdf0e10cSrcweir 			sInstDir = szValue;
104cdf0e10cSrcweir 			MsiSetProperty(handle, TEXT("INSTALLLOCATION"), sInstDir.c_str());
105cdf0e10cSrcweir 			// MessageBox( NULL, sInstDir.c_str(), "Found in HKEY_LOCAL_MACHINE", MB_OK );
106cdf0e10cSrcweir 		}
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 		RegCloseKey( hKey );
109cdf0e10cSrcweir 	}
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 	return ERROR_SUCCESS;
112cdf0e10cSrcweir 
113cdf0e10cSrcweir }
114