uninstaller: List 32-bit applications on 64-bit platforms.
[wine] / programs / uninstaller / main.c
1 /*
2  * Uninstaller
3  *
4  * Copyright 2000 Andreas Mohr
5  * Copyright 2004 Hannu Valtonen
6  * Copyright 2005 Jonathan Ernst
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  *
22  */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <windows.h>
27 #include <shlwapi.h>
28 #include "resource.h"
29 #include "regstr.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(uninstaller);
33
34 extern void WINAPI Control_RunDLL(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow);
35
36 typedef struct {
37     HKEY  root;
38     WCHAR *key;
39     WCHAR *descr;
40     WCHAR *command;
41     int active;
42 } uninst_entry;
43 static uninst_entry *entries = NULL;
44 static unsigned int numentries = 0;
45 static int list_need_update = 1;
46 static int oldsel = -1;
47 static WCHAR *sFilter;
48
49 static int FetchUninstallInformation(void);
50 static void UninstallProgram(void);
51 static int cmp_by_name(const void *a, const void *b);
52
53 static const WCHAR BackSlashW[] = { '\\', 0 };
54 static const WCHAR DisplayNameW[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
55 static const WCHAR PathUninstallW[] = {
56         'S','o','f','t','w','a','r','e','\\',
57         'M','i','c','r','o','s','o','f','t','\\',
58         'W','i','n','d','o','w','s','\\',
59         'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
60         'U','n','i','n','s','t','a','l','l',0 };
61 static const WCHAR UninstallCommandlineW[] = {'U','n','i','n','s','t','a','l','l','S','t','r','i','n','g',0};
62 static const WCHAR WindowsInstallerW[] = {'W','i','n','d','o','w','s','I','n','s','t','a','l','l','e','r',0};
63
64
65 /**
66  * Used to output program list when used with --list
67  */
68 static void ListUninstallPrograms(void)
69 {
70     unsigned int i;
71     int lenDescr, lenKey;
72     char *descr;
73     char *key;
74
75     FetchUninstallInformation();
76
77     for (i=0; i < numentries; i++)
78     {
79         lenDescr = WideCharToMultiByte(CP_UNIXCP, 0, entries[i].descr, -1, NULL, 0, NULL, NULL); 
80         lenKey = WideCharToMultiByte(CP_UNIXCP, 0, entries[i].key, -1, NULL, 0, NULL, NULL); 
81         descr = HeapAlloc(GetProcessHeap(), 0, lenDescr);
82         key = HeapAlloc(GetProcessHeap(), 0, lenKey);
83         WideCharToMultiByte(CP_UNIXCP, 0, entries[i].descr, -1, descr, lenDescr, NULL, NULL);
84         WideCharToMultiByte(CP_UNIXCP, 0, entries[i].key, -1, key, lenKey, NULL, NULL);
85         printf("%s|||%s\n", key, descr);
86         HeapFree(GetProcessHeap(), 0, descr);
87         HeapFree(GetProcessHeap(), 0, key);
88     }
89 }
90
91
92 static void RemoveSpecificProgram(WCHAR *nameW)
93 {
94     unsigned int i;
95     int lenName;
96     char *name;
97
98     FetchUninstallInformation();
99
100     for (i=0; i < numentries; i++)
101     {
102         if (CompareStringW(GetThreadLocale(), NORM_IGNORECASE, entries[i].key, -1, nameW, -1) == CSTR_EQUAL)
103         {
104             entries[i].active++;
105             break;
106         }
107     }
108
109     if (i < numentries)
110         UninstallProgram();
111     else
112     {
113         lenName = WideCharToMultiByte(CP_UNIXCP, 0, nameW, -1, NULL, 0, NULL, NULL); 
114         name = HeapAlloc(GetProcessHeap(), 0, lenName);
115         WideCharToMultiByte(CP_UNIXCP, 0, nameW, -1, name, lenName, NULL, NULL);
116         fprintf(stderr, "Error: could not match application [%s]\n", name);
117         HeapFree(GetProcessHeap(), 0, name);
118     }
119 }
120
121
122 int wmain(int argc, WCHAR *argv[])
123 {
124     LPCWSTR token = NULL;
125     static const WCHAR listW[] = { '-','-','l','i','s','t',0 };
126     static const WCHAR removeW[] = { '-','-','r','e','m','o','v','e',0 };
127     int i = 1;
128
129     while( i<argc )
130     {
131         token = argv[i++];
132         
133         /* Handle requests just to list the applications */
134         if( !lstrcmpW( token, listW ) )
135         {
136             ListUninstallPrograms();
137             return 0;
138         }
139         else if( !lstrcmpW( token, removeW ) )
140         {
141             if( i >= argc )
142             {
143                 WINE_ERR( "The remove option requires a parameter.\n");
144                 return 1;
145             }
146
147             RemoveSpecificProgram( argv[i++] );
148             return 0;
149         }
150         else 
151         {
152             WINE_ERR( "unknown option %s\n",wine_dbgstr_w(token));
153             return 1;
154         }
155     }
156
157     /* Start the GUI control panel */
158     Control_RunDLL(GetDesktopWindow(), 0, "appwiz.cpl", SW_SHOW);
159     return 1;
160 }
161
162
163 /**
164  * Used to sort entries by name.
165  */
166 static int cmp_by_name(const void *a, const void *b)
167 {
168     return lstrcmpiW(((const uninst_entry *)a)->descr, ((const uninst_entry *)b)->descr);
169 }
170
171
172 /**
173  * Fetch information from the uninstall key.
174  */
175 static int FetchFromRootKey(HKEY root)
176 {
177     HKEY hkeyApp;
178     int i;
179     DWORD sizeOfSubKeyName, displen, uninstlen;
180     WCHAR subKeyName[256];
181
182     sizeOfSubKeyName = 255;
183     for (i=0; RegEnumKeyExW( root, i, subKeyName, &sizeOfSubKeyName, NULL, NULL, NULL, NULL ) != ERROR_NO_MORE_ITEMS; ++i)
184     {
185         RegOpenKeyExW(root, subKeyName, 0, KEY_READ, &hkeyApp);
186         if (!RegQueryValueExW(hkeyApp, DisplayNameW, NULL, NULL, NULL, &displen))
187         {
188             DWORD value, type;
189             WCHAR *command;
190
191             if (!RegQueryValueExW(hkeyApp, WindowsInstallerW, NULL, &type, NULL, &value) && type == REG_DWORD && value == 1)
192             {
193                 static const WCHAR fmtW[] = {'m','s','i','e','x','e','c',' ','/','x','%','s',0};
194                 command = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(fmtW) + lstrlenW(subKeyName)) * sizeof(WCHAR));
195                 wsprintfW(command, fmtW, subKeyName);
196             }
197             else if (!RegQueryValueExW(hkeyApp, UninstallCommandlineW, NULL, NULL, NULL, &uninstlen))
198             {
199                 command = HeapAlloc(GetProcessHeap(), 0, uninstlen);
200                 RegQueryValueExW(hkeyApp, UninstallCommandlineW, 0, 0, (LPBYTE)command, &uninstlen);
201             }
202             else
203             {
204                 RegCloseKey(hkeyApp);
205                 sizeOfSubKeyName = 255;
206                 continue;
207             }
208             numentries++;
209             entries = HeapReAlloc(GetProcessHeap(), 0, entries, numentries*sizeof(uninst_entry));
210             entries[numentries-1].root = root;
211             entries[numentries-1].key = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(subKeyName)+1)*sizeof(WCHAR));
212             lstrcpyW(entries[numentries-1].key, subKeyName);
213             entries[numentries-1].descr = HeapAlloc(GetProcessHeap(), 0, displen);
214             RegQueryValueExW(hkeyApp, DisplayNameW, 0, 0, (LPBYTE)entries[numentries-1].descr, &displen);
215             entries[numentries-1].command = command;
216             entries[numentries-1].active = 0;
217             WINE_TRACE("allocated entry #%d: %s (%s), %s\n",
218             numentries, wine_dbgstr_w(entries[numentries-1].key), wine_dbgstr_w(entries[numentries-1].descr), wine_dbgstr_w(entries[numentries-1].command));
219             if(sFilter != NULL && StrStrIW(entries[numentries-1].descr,sFilter)==NULL)
220                 numentries--;
221         }
222         RegCloseKey(hkeyApp);
223         sizeOfSubKeyName = 255;
224     }
225     return 1;
226
227 }
228
229 static int FetchUninstallInformation(void)
230 {
231     static const BOOL is_64bit = sizeof(void *) > sizeof(int);
232     int rc = 0;
233     HKEY root;
234
235     numentries = 0;
236     oldsel = -1;
237     if (!entries)
238         entries = HeapAlloc(GetProcessHeap(), 0, sizeof(uninst_entry));
239
240     if (!RegOpenKeyExW(HKEY_LOCAL_MACHINE, PathUninstallW, 0, KEY_READ, &root))
241     {
242         rc |= FetchFromRootKey(root);
243         RegCloseKey(root);
244     }
245     if (is_64bit &&
246         !RegOpenKeyExW(HKEY_LOCAL_MACHINE, PathUninstallW, 0, KEY_READ|KEY_WOW64_32KEY, &root))
247     {
248         rc |= FetchFromRootKey(root);
249         RegCloseKey(root);
250     }
251     if (!RegOpenKeyExW(HKEY_CURRENT_USER, PathUninstallW, 0, KEY_READ, &root))
252     {
253         rc |= FetchFromRootKey(root);
254         RegCloseKey(root);
255     }
256
257     qsort(entries, numentries, sizeof(uninst_entry), cmp_by_name);
258     return rc;
259 }
260
261 static void UninstallProgram(void)
262 {
263     unsigned int i;
264     WCHAR errormsg[1024];
265     BOOL res;
266     STARTUPINFOW si;
267     PROCESS_INFORMATION info;
268     DWORD exit_code;
269     HKEY hkey;
270     for (i=0; i < numentries; i++)
271     {
272         if (!(entries[i].active)) /* don't uninstall this one */
273             continue;
274         WINE_TRACE("uninstalling %s\n", wine_dbgstr_w(entries[i].descr));
275         memset(&si, 0, sizeof(STARTUPINFOW));
276         si.cb = sizeof(STARTUPINFOW);
277         si.wShowWindow = SW_NORMAL;
278         res = CreateProcessW(NULL, entries[i].command, NULL, NULL, FALSE, 0, NULL, NULL, &si, &info);
279         if (res)
280         {   /* wait for the process to exit */
281             WaitForSingleObject(info.hProcess, INFINITE);
282             res = GetExitCodeProcess(info.hProcess, &exit_code);
283             WINE_TRACE("%d: %08x\n", res, exit_code);
284         }
285         else
286         {
287             WCHAR sAppName[MAX_STRING_LEN];
288             WCHAR sUninstallFailed[MAX_STRING_LEN];
289             HINSTANCE hInst = GetModuleHandleW(0);
290
291             LoadStringW(hInst, IDS_APPNAME, sAppName, sizeof(sAppName)/sizeof(WCHAR));
292             LoadStringW(hInst, IDS_UNINSTALLFAILED, sUninstallFailed, sizeof(sUninstallFailed)/sizeof(WCHAR));
293             wsprintfW(errormsg, sUninstallFailed, entries[i].command);
294             if(MessageBoxW(0, errormsg, sAppName, MB_YESNO | MB_ICONQUESTION)==IDYES)
295             {
296                 /* delete the application's uninstall entry */
297                 RegOpenKeyExW(entries[i].root, PathUninstallW, 0, KEY_READ, &hkey);
298                 RegDeleteKeyW(hkey, entries[i].key);
299                 RegCloseKey(hkey);
300             }
301         }
302     }
303     WINE_TRACE("finished uninstall phase.\n");
304     list_need_update = 1;
305 }