riched20: Implement SetEnd().
[wine.git] / programs / uninstaller / main.c
blob6b16e5d930b20ba2cf459859e4f49362fd4dcc72
1 /*
2 * Uninstaller
4 * Copyright 2000 Andreas Mohr
5 * Copyright 2004 Hannu Valtonen
6 * Copyright 2005 Jonathan Ernst
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.
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.
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
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"
32 WINE_DEFAULT_DEBUG_CHANNEL(uninstaller);
34 extern void WINAPI Control_RunDLL(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow);
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 oldsel = -1;
46 static WCHAR *sFilter;
48 static int FetchUninstallInformation(void);
49 static void UninstallProgram(void);
50 static int cmp_by_name(const void *a, const void *b);
52 static const WCHAR DisplayNameW[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
53 static const WCHAR PathUninstallW[] = {
54 'S','o','f','t','w','a','r','e','\\',
55 'M','i','c','r','o','s','o','f','t','\\',
56 'W','i','n','d','o','w','s','\\',
57 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
58 'U','n','i','n','s','t','a','l','l',0 };
59 static const WCHAR UninstallCommandlineW[] = {'U','n','i','n','s','t','a','l','l','S','t','r','i','n','g',0};
60 static const WCHAR WindowsInstallerW[] = {'W','i','n','d','o','w','s','I','n','s','t','a','l','l','e','r',0};
61 static const WCHAR SystemComponentW[] = {'S','y','s','t','e','m','C','o','m','p','o','n','e','n','t',0};
63 /**
64 * Used to output program list when used with --list
66 static void ListUninstallPrograms(void)
68 unsigned int i;
69 int lenDescr, lenKey;
70 char *descr;
71 char *key;
73 FetchUninstallInformation();
75 for (i=0; i < numentries; i++)
77 lenDescr = WideCharToMultiByte(CP_UNIXCP, 0, entries[i].descr, -1, NULL, 0, NULL, NULL);
78 lenKey = WideCharToMultiByte(CP_UNIXCP, 0, entries[i].key, -1, NULL, 0, NULL, NULL);
79 descr = HeapAlloc(GetProcessHeap(), 0, lenDescr);
80 key = HeapAlloc(GetProcessHeap(), 0, lenKey);
81 WideCharToMultiByte(CP_UNIXCP, 0, entries[i].descr, -1, descr, lenDescr, NULL, NULL);
82 WideCharToMultiByte(CP_UNIXCP, 0, entries[i].key, -1, key, lenKey, NULL, NULL);
83 printf("%s|||%s\n", key, descr);
84 HeapFree(GetProcessHeap(), 0, descr);
85 HeapFree(GetProcessHeap(), 0, key);
90 static void RemoveSpecificProgram(WCHAR *nameW)
92 unsigned int i;
93 int lenName;
94 char *name;
96 FetchUninstallInformation();
98 for (i=0; i < numentries; i++)
100 if (CompareStringW(GetThreadLocale(), NORM_IGNORECASE, entries[i].key, -1, nameW, -1) == CSTR_EQUAL)
102 entries[i].active++;
103 break;
107 if (i < numentries)
108 UninstallProgram();
109 else
111 lenName = WideCharToMultiByte(CP_UNIXCP, 0, nameW, -1, NULL, 0, NULL, NULL);
112 name = HeapAlloc(GetProcessHeap(), 0, lenName);
113 WideCharToMultiByte(CP_UNIXCP, 0, nameW, -1, name, lenName, NULL, NULL);
114 fprintf(stderr, "Error: could not match application [%s]\n", name);
115 HeapFree(GetProcessHeap(), 0, name);
120 int wmain(int argc, WCHAR *argv[])
122 LPCWSTR token = NULL;
123 static const WCHAR listW[] = { '-','-','l','i','s','t',0 };
124 static const WCHAR removeW[] = { '-','-','r','e','m','o','v','e',0 };
125 int i = 1;
127 while( i<argc )
129 token = argv[i++];
131 /* Handle requests just to list the applications */
132 if( !lstrcmpW( token, listW ) )
134 ListUninstallPrograms();
135 return 0;
137 else if( !lstrcmpW( token, removeW ) )
139 if( i >= argc )
141 WINE_ERR( "The remove option requires a parameter.\n");
142 return 1;
145 RemoveSpecificProgram( argv[i++] );
146 return 0;
148 else
150 WINE_ERR( "unknown option %s\n",wine_dbgstr_w(token));
151 return 1;
155 /* Start the GUI control panel */
156 Control_RunDLL(GetDesktopWindow(), 0, "appwiz.cpl", SW_SHOW);
157 return 1;
162 * Used to sort entries by name.
164 static int cmp_by_name(const void *a, const void *b)
166 return lstrcmpiW(((const uninst_entry *)a)->descr, ((const uninst_entry *)b)->descr);
171 * Fetch information from the uninstall key.
173 static int FetchFromRootKey(HKEY root)
175 HKEY hkeyApp;
176 int i;
177 DWORD sizeOfSubKeyName, displen, uninstlen, value, type, size;
178 WCHAR subKeyName[256];
180 sizeOfSubKeyName = 255;
181 for (i=0; RegEnumKeyExW( root, i, subKeyName, &sizeOfSubKeyName, NULL, NULL, NULL, NULL ) != ERROR_NO_MORE_ITEMS; ++i)
183 RegOpenKeyExW(root, subKeyName, 0, KEY_READ, &hkeyApp);
184 if (!RegQueryValueExW(hkeyApp, SystemComponentW, NULL, &type, (LPBYTE)&value, &size) &&
185 type == REG_DWORD && value == 1)
187 RegCloseKey(hkeyApp);
188 sizeOfSubKeyName = 255;
189 continue;
191 if (!RegQueryValueExW(hkeyApp, DisplayNameW, NULL, NULL, NULL, &displen))
193 WCHAR *command;
195 size = sizeof(value);
196 if (!RegQueryValueExW(hkeyApp, WindowsInstallerW, NULL, &type, (LPBYTE)&value, &size) &&
197 type == REG_DWORD && value == 1)
199 static const WCHAR fmtW[] = {'m','s','i','e','x','e','c',' ','/','x','%','s',0};
200 command = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(fmtW) + lstrlenW(subKeyName)) * sizeof(WCHAR));
201 wsprintfW(command, fmtW, subKeyName);
203 else if (!RegQueryValueExW(hkeyApp, UninstallCommandlineW, NULL, NULL, NULL, &uninstlen))
205 command = HeapAlloc(GetProcessHeap(), 0, uninstlen);
206 RegQueryValueExW(hkeyApp, UninstallCommandlineW, 0, 0, (LPBYTE)command, &uninstlen);
208 else
210 RegCloseKey(hkeyApp);
211 sizeOfSubKeyName = 255;
212 continue;
214 numentries++;
215 entries = HeapReAlloc(GetProcessHeap(), 0, entries, numentries*sizeof(uninst_entry));
216 entries[numentries-1].root = root;
217 entries[numentries-1].key = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(subKeyName)+1)*sizeof(WCHAR));
218 lstrcpyW(entries[numentries-1].key, subKeyName);
219 entries[numentries-1].descr = HeapAlloc(GetProcessHeap(), 0, displen);
220 RegQueryValueExW(hkeyApp, DisplayNameW, 0, 0, (LPBYTE)entries[numentries-1].descr, &displen);
221 entries[numentries-1].command = command;
222 entries[numentries-1].active = 0;
223 WINE_TRACE("allocated entry #%d: %s (%s), %s\n",
224 numentries, wine_dbgstr_w(entries[numentries-1].key), wine_dbgstr_w(entries[numentries-1].descr), wine_dbgstr_w(entries[numentries-1].command));
225 if(sFilter != NULL && StrStrIW(entries[numentries-1].descr,sFilter)==NULL)
226 numentries--;
228 RegCloseKey(hkeyApp);
229 sizeOfSubKeyName = 255;
231 return 1;
235 static int FetchUninstallInformation(void)
237 static const BOOL is_64bit = sizeof(void *) > sizeof(int);
238 int rc = 0;
239 HKEY root;
241 numentries = 0;
242 oldsel = -1;
243 if (!entries)
244 entries = HeapAlloc(GetProcessHeap(), 0, sizeof(uninst_entry));
246 if (!RegOpenKeyExW(HKEY_LOCAL_MACHINE, PathUninstallW, 0, KEY_READ, &root))
248 rc |= FetchFromRootKey(root);
249 RegCloseKey(root);
251 if (is_64bit &&
252 !RegOpenKeyExW(HKEY_LOCAL_MACHINE, PathUninstallW, 0, KEY_READ|KEY_WOW64_32KEY, &root))
254 rc |= FetchFromRootKey(root);
255 RegCloseKey(root);
257 if (!RegOpenKeyExW(HKEY_CURRENT_USER, PathUninstallW, 0, KEY_READ, &root))
259 rc |= FetchFromRootKey(root);
260 RegCloseKey(root);
263 qsort(entries, numentries, sizeof(uninst_entry), cmp_by_name);
264 return rc;
267 static void UninstallProgram(void)
269 unsigned int i;
270 WCHAR errormsg[1024];
271 BOOL res;
272 STARTUPINFOW si;
273 PROCESS_INFORMATION info;
274 DWORD exit_code;
275 HKEY hkey;
276 for (i=0; i < numentries; i++)
278 if (!(entries[i].active)) /* don't uninstall this one */
279 continue;
280 WINE_TRACE("uninstalling %s\n", wine_dbgstr_w(entries[i].descr));
281 memset(&si, 0, sizeof(STARTUPINFOW));
282 si.cb = sizeof(STARTUPINFOW);
283 si.wShowWindow = SW_NORMAL;
284 res = CreateProcessW(NULL, entries[i].command, NULL, NULL, FALSE, 0, NULL, NULL, &si, &info);
285 if (res)
286 { /* wait for the process to exit */
287 WaitForSingleObject(info.hProcess, INFINITE);
288 res = GetExitCodeProcess(info.hProcess, &exit_code);
289 WINE_TRACE("%d: %08x\n", res, exit_code);
291 else
293 WCHAR sAppName[MAX_STRING_LEN];
294 WCHAR sUninstallFailed[MAX_STRING_LEN];
295 HINSTANCE hInst = GetModuleHandleW(0);
297 LoadStringW(hInst, IDS_APPNAME, sAppName, sizeof(sAppName)/sizeof(WCHAR));
298 LoadStringW(hInst, IDS_UNINSTALLFAILED, sUninstallFailed, sizeof(sUninstallFailed)/sizeof(WCHAR));
299 wsprintfW(errormsg, sUninstallFailed, entries[i].command);
300 if(MessageBoxW(0, errormsg, sAppName, MB_YESNO | MB_ICONQUESTION)==IDYES)
302 /* delete the application's uninstall entry */
303 RegOpenKeyExW(entries[i].root, PathUninstallW, 0, KEY_READ, &hkey);
304 RegDeleteKeyW(hkey, entries[i].key);
305 RegCloseKey(hkey);
309 WINE_TRACE("finished uninstall phase.\n");