msacm: Fix acmMetrics(ACM_METRIC_DRIVER_PRIORITY) return on error.
[wine/multimedia.git] / programs / uninstaller / main.c
blobb4bd9e3281e74b0659c3636f93e0060ff54279ae
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 typedef struct {
35 WCHAR *key;
36 WCHAR *descr;
37 WCHAR *command;
38 int active;
39 } uninst_entry;
40 static uninst_entry *entries = NULL;
41 static unsigned int numentries = 0;
42 static int list_need_update = 1;
43 static int oldsel = -1;
44 static WCHAR *sFilter;
45 static WCHAR sAppName[MAX_STRING_LEN];
46 static WCHAR sAboutTitle[MAX_STRING_LEN];
47 static WCHAR sAbout[MAX_STRING_LEN];
48 static WCHAR sRegistryKeyNotAvailable[MAX_STRING_LEN];
49 static WCHAR sUninstallFailed[MAX_STRING_LEN];
51 static int FetchUninstallInformation(void);
52 static void UninstallProgram(void);
53 static void UpdateList(HWND hList);
54 static int cmp_by_name(const void *a, const void *b);
55 static INT_PTR CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
58 static const WCHAR BackSlashW[] = { '\\', 0 };
59 static const WCHAR DisplayNameW[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
60 static const WCHAR PathUninstallW[] = {
61 'S','o','f','t','w','a','r','e','\\',
62 'M','i','c','r','o','s','o','f','t','\\',
63 'W','i','n','d','o','w','s','\\',
64 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
65 'U','n','i','n','s','t','a','l','l',0 };
66 static const WCHAR UninstallCommandlineW[] = {'U','n','i','n','s','t','a','l','l','S','t','r','i','n','g',0};
69 /**
70 * Used to output program list when used with --list
72 static void ListUninstallPrograms(void)
74 unsigned int i;
75 int lenDescr, lenKey;
76 char *descr;
77 char *key;
79 if (! FetchUninstallInformation())
80 return;
82 for (i=0; i < numentries; i++)
84 lenDescr = WideCharToMultiByte(CP_UNIXCP, 0, entries[i].descr, -1, NULL, 0, NULL, NULL);
85 lenKey = WideCharToMultiByte(CP_UNIXCP, 0, entries[i].key, -1, NULL, 0, NULL, NULL);
86 descr = HeapAlloc(GetProcessHeap(), 0, lenDescr);
87 key = HeapAlloc(GetProcessHeap(), 0, lenKey);
88 WideCharToMultiByte(CP_UNIXCP, 0, entries[i].descr, -1, descr, lenDescr, NULL, NULL);
89 WideCharToMultiByte(CP_UNIXCP, 0, entries[i].key, -1, key, lenKey, NULL, NULL);
90 printf("%s|||%s\n", key, descr);
91 HeapFree(GetProcessHeap(), 0, descr);
92 HeapFree(GetProcessHeap(), 0, key);
97 static void RemoveSpecificProgram(WCHAR *nameW)
99 unsigned int i;
100 int lenName;
101 char *name;
103 if (! FetchUninstallInformation())
104 return;
106 for (i=0; i < numentries; i++)
108 if (lstrcmpW(entries[i].key, nameW) == 0)
110 entries[i].active++;
111 break;
115 if (i < numentries)
116 UninstallProgram();
117 else
119 lenName = WideCharToMultiByte(CP_UNIXCP, 0, nameW, -1, NULL, 0, NULL, NULL);
120 name = HeapAlloc(GetProcessHeap(), 0, lenName);
121 WideCharToMultiByte(CP_UNIXCP, 0, nameW, -1, name, lenName, NULL, NULL);
122 fprintf(stderr, "Error: could not match application [%s]\n", name);
123 HeapFree(GetProcessHeap(), 0, name);
128 int wmain(int argc, WCHAR *argv[])
130 LPCWSTR token = NULL;
131 HINSTANCE hInst = GetModuleHandleW(0);
132 static const WCHAR listW[] = { '-','-','l','i','s','t',0 };
133 static const WCHAR removeW[] = { '-','-','r','e','m','o','v','e',0 };
134 int i = 1;
136 while( i<argc )
138 token = argv[i++];
140 /* Handle requests just to list the applications */
141 if( !lstrcmpW( token, listW ) )
143 ListUninstallPrograms();
144 return 0;
146 else if( !lstrcmpW( token, removeW ) )
148 if( i >= argc )
150 WINE_ERR( "The remove option requires a parameter.\n");
151 return 1;
154 RemoveSpecificProgram( argv[i++] );
155 return 0;
157 else
159 WINE_ERR( "unknown option %s\n",wine_dbgstr_w(token));
160 return 1;
164 /* Load MessageBox's strings */
165 LoadStringW(hInst, IDS_APPNAME, sAppName, sizeof(sAppName)/sizeof(WCHAR));
166 LoadStringW(hInst, IDS_ABOUTTITLE, sAboutTitle, sizeof(sAboutTitle)/sizeof(WCHAR));
167 LoadStringW(hInst, IDS_ABOUT, sAbout, sizeof(sAbout)/sizeof(WCHAR));
168 LoadStringW(hInst, IDS_REGISTRYKEYNOTAVAILABLE, sRegistryKeyNotAvailable, sizeof(sRegistryKeyNotAvailable)/sizeof(WCHAR));
169 LoadStringW(hInst, IDS_UNINSTALLFAILED, sUninstallFailed, sizeof(sUninstallFailed)/sizeof(WCHAR));
171 return DialogBoxW(hInst, MAKEINTRESOURCEW(IDD_UNINSTALLER), NULL, DlgProc);
176 * Used to sort entries by name.
178 static int cmp_by_name(const void *a, const void *b)
180 return lstrcmpiW(((const uninst_entry *)a)->descr, ((const uninst_entry *)b)->descr);
185 * Fetch informations from the uninstall key.
187 static int FetchUninstallInformation(void)
189 HKEY hkeyUninst, hkeyApp;
190 int i;
191 DWORD sizeOfSubKeyName, displen, uninstlen;
192 WCHAR subKeyName[256];
193 WCHAR key_app[1024];
194 WCHAR *p;
196 numentries = 0;
197 oldsel = -1;
198 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, PathUninstallW, 0, KEY_READ, &hkeyUninst) != ERROR_SUCCESS)
200 MessageBoxW(0, sRegistryKeyNotAvailable, sAppName, MB_OK);
201 return 0;
204 if (!entries)
205 entries = HeapAlloc(GetProcessHeap(), 0, sizeof(uninst_entry));
207 lstrcpyW(key_app, PathUninstallW);
208 lstrcatW(key_app, BackSlashW);
209 p = key_app+lstrlenW(PathUninstallW)+1;
211 sizeOfSubKeyName = 255;
212 for (i=0; RegEnumKeyExW( hkeyUninst, i, subKeyName, &sizeOfSubKeyName, NULL, NULL, NULL, NULL ) != ERROR_NO_MORE_ITEMS; ++i)
214 lstrcpyW(p, subKeyName);
215 RegOpenKeyExW(HKEY_LOCAL_MACHINE, key_app, 0, KEY_READ, &hkeyApp);
216 if ((RegQueryValueExW(hkeyApp, DisplayNameW, 0, 0, NULL, &displen) == ERROR_SUCCESS)
217 && (RegQueryValueExW(hkeyApp, UninstallCommandlineW, 0, 0, NULL, &uninstlen) == ERROR_SUCCESS))
219 numentries++;
220 entries = HeapReAlloc(GetProcessHeap(), 0, entries, numentries*sizeof(uninst_entry));
221 entries[numentries-1].key = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(subKeyName)+1)*sizeof(WCHAR));
222 lstrcpyW(entries[numentries-1].key, subKeyName);
223 entries[numentries-1].descr = HeapAlloc(GetProcessHeap(), 0, displen);
224 RegQueryValueExW(hkeyApp, DisplayNameW, 0, 0, (LPBYTE)entries[numentries-1].descr, &displen);
225 entries[numentries-1].command = HeapAlloc(GetProcessHeap(), 0, uninstlen);
226 entries[numentries-1].active = 0;
227 RegQueryValueExW(hkeyApp, UninstallCommandlineW, 0, 0, (LPBYTE)entries[numentries-1].command, &uninstlen);
228 WINE_TRACE("allocated entry #%d: %s (%s), %s\n",
229 numentries, wine_dbgstr_w(entries[numentries-1].key), wine_dbgstr_w(entries[numentries-1].descr), wine_dbgstr_w(entries[numentries-1].command));
230 if(sFilter != NULL && StrStrIW(entries[numentries-1].descr,sFilter)==NULL)
231 numentries--;
233 RegCloseKey(hkeyApp);
234 sizeOfSubKeyName = 255;
236 qsort(entries, numentries, sizeof(uninst_entry), cmp_by_name);
237 RegCloseKey(hkeyUninst);
238 return 1;
242 static void UninstallProgram(void)
244 unsigned int i;
245 WCHAR errormsg[1024];
246 BOOL res;
247 STARTUPINFOW si;
248 PROCESS_INFORMATION info;
249 DWORD exit_code;
250 HKEY hkey;
251 for (i=0; i < numentries; i++)
253 if (!(entries[i].active)) /* don't uninstall this one */
254 continue;
255 WINE_TRACE("uninstalling %s\n", wine_dbgstr_w(entries[i].descr));
256 memset(&si, 0, sizeof(STARTUPINFOW));
257 si.cb = sizeof(STARTUPINFOW);
258 si.wShowWindow = SW_NORMAL;
259 res = CreateProcessW(NULL, entries[i].command, NULL, NULL, FALSE, 0, NULL, NULL, &si, &info);
260 if (res)
261 { /* wait for the process to exit */
262 WaitForSingleObject(info.hProcess, INFINITE);
263 res = GetExitCodeProcess(info.hProcess, &exit_code);
264 WINE_TRACE("%d: %08lx\n", res, exit_code);
266 else
268 wsprintfW(errormsg, sUninstallFailed, entries[i].command);
269 if(MessageBoxW(0, errormsg, sAppName, MB_YESNO | MB_ICONQUESTION)==IDYES)
271 /* delete the application's uninstall entry */
272 RegOpenKeyExW(HKEY_LOCAL_MACHINE, PathUninstallW, 0, KEY_READ, &hkey);
273 RegDeleteKeyW(hkey, entries[i].key);
274 RegCloseKey(hkey);
278 WINE_TRACE("finished uninstall phase.\n");
279 list_need_update = 1;
283 static INT_PTR CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
285 TEXTMETRICW tm;
286 HDC hdc;
287 HWND hList = GetDlgItem(hwnd, IDC_LIST);
288 switch(Message)
290 case WM_INITDIALOG:
291 hdc = GetDC(hwnd);
292 GetTextMetricsW(hdc, &tm);
293 UpdateList(hList);
294 ReleaseDC(hwnd, hdc);
295 break;
296 case WM_COMMAND:
297 switch(LOWORD(wParam))
299 case IDC_FILTER:
301 if (HIWORD(wParam) == EN_CHANGE)
303 int len = GetWindowTextLengthW(GetDlgItem(hwnd, IDC_FILTER));
304 list_need_update = 1;
305 if(len > 0)
307 sFilter = (WCHAR*)GlobalAlloc(GPTR, (len + 1)*sizeof(WCHAR));
308 GetDlgItemTextW(hwnd, IDC_FILTER, sFilter, len + 1);
310 else sFilter = NULL;
311 UpdateList(hList);
313 break;
315 case IDC_UNINSTALL:
317 int count = SendMessageW(hList, LB_GETSELCOUNT, 0, 0);
318 if(count != 0)
320 UninstallProgram();
321 UpdateList(hList);
323 break;
325 case IDC_LIST:
326 if (HIWORD(wParam) == LBN_SELCHANGE)
328 int sel = SendMessageW(hList, LB_GETCURSEL, 0, 0);
329 if (oldsel != -1)
331 entries[oldsel].active ^= 1; /* toggle */
332 WINE_TRACE("toggling %d old %s\n", entries[oldsel].active,
333 wine_dbgstr_w(entries[oldsel].descr));
335 entries[sel].active ^= 1; /* toggle */
336 WINE_TRACE("toggling %d %s\n", entries[sel].active,
337 wine_dbgstr_w(entries[oldsel].descr));
338 oldsel = sel;
340 break;
341 case IDC_ABOUT:
342 MessageBoxW(0, sAbout, sAboutTitle, MB_OK);
343 break;
344 case IDCANCEL:
345 case IDC_EXIT:
346 EndDialog(hwnd, 0);
347 break;
349 break;
350 default:
351 return FALSE;
353 return TRUE;
357 static void UpdateList(HWND hList)
359 unsigned int i;
360 if (list_need_update)
362 int prevsel;
363 prevsel = SendMessageW(hList, LB_GETCURSEL, 0, 0);
364 if (!(FetchUninstallInformation()))
366 PostQuitMessage(0);
367 return;
369 SendMessageW(hList, LB_RESETCONTENT, 0, 0);
370 SendMessageW(hList, WM_SETREDRAW, FALSE, 0);
371 for (i=0; i < numentries; i++)
373 WINE_TRACE("adding %s\n", wine_dbgstr_w(entries[i].descr));
374 SendMessageW(hList, LB_ADDSTRING, 0, (LPARAM)entries[i].descr);
376 WINE_TRACE("setting prevsel %d\n", prevsel);
377 if (prevsel != -1)
378 SendMessageW(hList, LB_SETCURSEL, prevsel, 0 );
379 SendMessageW(hList, WM_SETREDRAW, TRUE, 0);
380 list_need_update = 0;