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 #include <io.h>
47cdf0e10cSrcweir 
GetMsiProperty(MSIHANDLE handle,const std::_tstring & sProperty)48cdf0e10cSrcweir static std::_tstring GetMsiProperty( MSIHANDLE handle, const std::_tstring& sProperty )
49cdf0e10cSrcweir {
50cdf0e10cSrcweir 	std::_tstring	result;
51cdf0e10cSrcweir 	TCHAR	szDummy[1] = TEXT("");
52cdf0e10cSrcweir 	DWORD	nChars = 0;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 	if ( MsiGetProperty( handle, sProperty.c_str(), szDummy, &nChars ) == ERROR_MORE_DATA )
55cdf0e10cSrcweir 	{
56cdf0e10cSrcweir         DWORD nBytes = ++nChars * sizeof(TCHAR);
57cdf0e10cSrcweir         LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
58cdf0e10cSrcweir         ZeroMemory( buffer, nBytes );
59cdf0e10cSrcweir         MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
60cdf0e10cSrcweir         result = buffer;
61cdf0e10cSrcweir 	}
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 	return	result;
64cdf0e10cSrcweir }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 
ExecuteCommand(LPCTSTR lpCommand,BOOL bSync)67cdf0e10cSrcweir static BOOL ExecuteCommand( LPCTSTR lpCommand, BOOL bSync )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir 	BOOL				fSuccess = FALSE;
70cdf0e10cSrcweir 	STARTUPINFO			si;
71cdf0e10cSrcweir 	PROCESS_INFORMATION	pi;
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 	ZeroMemory( &si, sizeof(si) );
74cdf0e10cSrcweir 	si.cb = sizeof(si);
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 	fSuccess = CreateProcess(
77cdf0e10cSrcweir 		NULL,
78cdf0e10cSrcweir 		(LPTSTR)lpCommand,
79cdf0e10cSrcweir 		NULL,
80cdf0e10cSrcweir 		NULL,
81cdf0e10cSrcweir 		FALSE,
82cdf0e10cSrcweir 		0,
83cdf0e10cSrcweir 		NULL,
84cdf0e10cSrcweir 		NULL,
85cdf0e10cSrcweir 		&si,
86cdf0e10cSrcweir 		&pi
87cdf0e10cSrcweir 		);
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 	if ( fSuccess )
90cdf0e10cSrcweir 	{
91cdf0e10cSrcweir 		if ( bSync )
92cdf0e10cSrcweir 			WaitForSingleObject( pi.hProcess, INFINITE );
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 		CloseHandle( pi.hProcess );
95cdf0e10cSrcweir 		CloseHandle( pi.hThread );
96cdf0e10cSrcweir 	}
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 	return fSuccess;
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
ExecutePostUninstallScript(MSIHANDLE handle)101cdf0e10cSrcweir extern "C" UINT __stdcall ExecutePostUninstallScript( MSIHANDLE handle )
102cdf0e10cSrcweir {
103cdf0e10cSrcweir 	TCHAR	szValue[8192];
104cdf0e10cSrcweir 	DWORD	nValueSize = sizeof(szValue);
105cdf0e10cSrcweir 	HKEY	hKey;
106cdf0e10cSrcweir 	std::_tstring	sInstDir;
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 	std::_tstring	sProductKey = GetMsiProperty( handle, TEXT("FINDPRODUCT") );
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 	// MessageBox( NULL, sProductKey.c_str(), "Titel", MB_OK );
111cdf0e10cSrcweir 
112cdf0e10cSrcweir 	if ( ERROR_SUCCESS == RegOpenKey( HKEY_CURRENT_USER,  sProductKey.c_str(), &hKey ) )
113cdf0e10cSrcweir 	{
114cdf0e10cSrcweir 		if ( ERROR_SUCCESS == RegQueryValueEx( hKey, TEXT("INSTALLLOCATION"), NULL, NULL, (LPBYTE)szValue, &nValueSize ) )
115cdf0e10cSrcweir 		{
116cdf0e10cSrcweir 			sInstDir = szValue;
117cdf0e10cSrcweir 		}
118cdf0e10cSrcweir 		RegCloseKey( hKey );
119cdf0e10cSrcweir 	}
120cdf0e10cSrcweir 	else if ( ERROR_SUCCESS == RegOpenKey( HKEY_LOCAL_MACHINE,  sProductKey.c_str(), &hKey ) )
121cdf0e10cSrcweir 	{
122cdf0e10cSrcweir 		if ( ERROR_SUCCESS == RegQueryValueEx( hKey, TEXT("INSTALLLOCATION"), NULL, NULL, (LPBYTE)szValue, &nValueSize ) )
123cdf0e10cSrcweir 		{
124cdf0e10cSrcweir 			sInstDir = szValue;
125cdf0e10cSrcweir 		}
126cdf0e10cSrcweir 		RegCloseKey( hKey );
127cdf0e10cSrcweir 	}
128cdf0e10cSrcweir 	else
129cdf0e10cSrcweir 		return ERROR_SUCCESS;
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 	std::_tstring	sInfFile = sInstDir + TEXT("program\\postuninstall.inf");
132cdf0e10cSrcweir 	std::_tstring	sCommand = _T("RUNDLL32.EXE ");
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 	// MessageBox( NULL, sInfFile.c_str(), "Titel", MB_OK );
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 	if ( (LONG)GetVersion() < 0 )
137cdf0e10cSrcweir 		sCommand += _T("setupx.dll");
138cdf0e10cSrcweir 	else
139cdf0e10cSrcweir 		sCommand += _T("setupapi.dll");
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 	sCommand += _T(",InstallHinfSection PostUninstall 132 ");
142cdf0e10cSrcweir 	sCommand += sInfFile;
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 	if ( 0 == _taccess( sInfFile.c_str(), 2 ) )
145cdf0e10cSrcweir 		ExecuteCommand( sCommand.c_str(), TRUE );
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 	DeleteFile( sInfFile.c_str() );
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 	return ERROR_SUCCESS;
150cdf0e10cSrcweir }
151cdf0e10cSrcweir 
152