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