localspl: Implement DeletePort for XcvDataPort.
[wine/wine64.git] / programs / uninstaller / main.c
blob372f7a16b20acb1ce82fe5254268290f55c72233
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 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 FetchUninstallInformation();
81 for (i=0; i < numentries; i++)
83 lenDescr = WideCharToMultiByte(CP_UNIXCP, 0, entries[i].descr, -1, NULL, 0, NULL, NULL);
84 lenKey = WideCharToMultiByte(CP_UNIXCP, 0, entries[i].key, -1, NULL, 0, NULL, NULL);
85 descr = HeapAlloc(GetProcessHeap(), 0, lenDescr);
86 key = HeapAlloc(GetProcessHeap(), 0, lenKey);
87 WideCharToMultiByte(CP_UNIXCP, 0, entries[i].descr, -1, descr, lenDescr, NULL, NULL);
88 WideCharToMultiByte(CP_UNIXCP, 0, entries[i].key, -1, key, lenKey, NULL, NULL);
89 printf("%s|||%s\n", key, descr);
90 HeapFree(GetProcessHeap(), 0, descr);
91 HeapFree(GetProcessHeap(), 0, key);
96 static void RemoveSpecificProgram(WCHAR *nameW)
98 unsigned int i;
99 int lenName;
100 char *name;
102 FetchUninstallInformation();
104 for (i=0; i < numentries; i++)
106 if (lstrcmpW(entries[i].key, nameW) == 0)
108 entries[i].active++;
109 break;
113 if (i < numentries)
114 UninstallProgram();
115 else
117 lenName = WideCharToMultiByte(CP_UNIXCP, 0, nameW, -1, NULL, 0, NULL, NULL);
118 name = HeapAlloc(GetProcessHeap(), 0, lenName);
119 WideCharToMultiByte(CP_UNIXCP, 0, nameW, -1, name, lenName, NULL, NULL);
120 fprintf(stderr, "Error: could not match application [%s]\n", name);
121 HeapFree(GetProcessHeap(), 0, name);
126 int wmain(int argc, WCHAR *argv[])
128 LPCWSTR token = NULL;
129 HINSTANCE hInst = GetModuleHandleW(0);
130 static const WCHAR listW[] = { '-','-','l','i','s','t',0 };
131 static const WCHAR removeW[] = { '-','-','r','e','m','o','v','e',0 };
132 int i = 1;
134 while( i<argc )
136 token = argv[i++];
138 /* Handle requests just to list the applications */
139 if( !lstrcmpW( token, listW ) )
141 ListUninstallPrograms();
142 return 0;
144 else if( !lstrcmpW( token, removeW ) )
146 if( i >= argc )
148 WINE_ERR( "The remove option requires a parameter.\n");
149 return 1;
152 RemoveSpecificProgram( argv[i++] );
153 return 0;
155 else
157 WINE_ERR( "unknown option %s\n",wine_dbgstr_w(token));
158 return 1;
162 /* Load MessageBox's strings */
163 LoadStringW(hInst, IDS_APPNAME, sAppName, sizeof(sAppName)/sizeof(WCHAR));
164 LoadStringW(hInst, IDS_ABOUTTITLE, sAboutTitle, sizeof(sAboutTitle)/sizeof(WCHAR));
165 LoadStringW(hInst, IDS_ABOUT, sAbout, sizeof(sAbout)/sizeof(WCHAR));
166 LoadStringW(hInst, IDS_REGISTRYKEYNOTAVAILABLE, sRegistryKeyNotAvailable, sizeof(sRegistryKeyNotAvailable)/sizeof(WCHAR));
167 LoadStringW(hInst, IDS_UNINSTALLFAILED, sUninstallFailed, sizeof(sUninstallFailed)/sizeof(WCHAR));
169 return DialogBoxW(hInst, MAKEINTRESOURCEW(IDD_UNINSTALLER), NULL, DlgProc);
174 * Used to sort entries by name.
176 static int cmp_by_name(const void *a, const void *b)
178 return lstrcmpiW(((const uninst_entry *)a)->descr, ((const uninst_entry *)b)->descr);
183 * Fetch information from the uninstall key.
185 static int FetchUninstallInformation(void)
187 HKEY hkeyUninst, hkeyApp;
188 int i;
189 DWORD sizeOfSubKeyName, displen, uninstlen;
190 WCHAR subKeyName[256];
191 WCHAR key_app[1024];
192 WCHAR *p;
194 numentries = 0;
195 oldsel = -1;
196 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, PathUninstallW, 0, KEY_READ, &hkeyUninst) != ERROR_SUCCESS)
197 return 0;
199 if (!entries)
200 entries = HeapAlloc(GetProcessHeap(), 0, sizeof(uninst_entry));
202 lstrcpyW(key_app, PathUninstallW);
203 lstrcatW(key_app, BackSlashW);
204 p = key_app+lstrlenW(PathUninstallW)+1;
206 sizeOfSubKeyName = 255;
207 for (i=0; RegEnumKeyExW( hkeyUninst, i, subKeyName, &sizeOfSubKeyName, NULL, NULL, NULL, NULL ) != ERROR_NO_MORE_ITEMS; ++i)
209 lstrcpyW(p, subKeyName);
210 RegOpenKeyExW(HKEY_LOCAL_MACHINE, key_app, 0, KEY_READ, &hkeyApp);
211 if ((RegQueryValueExW(hkeyApp, DisplayNameW, 0, 0, NULL, &displen) == ERROR_SUCCESS)
212 && (RegQueryValueExW(hkeyApp, UninstallCommandlineW, 0, 0, NULL, &uninstlen) == ERROR_SUCCESS))
214 numentries++;
215 entries = HeapReAlloc(GetProcessHeap(), 0, entries, numentries*sizeof(uninst_entry));
216 entries[numentries-1].key = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(subKeyName)+1)*sizeof(WCHAR));
217 lstrcpyW(entries[numentries-1].key, subKeyName);
218 entries[numentries-1].descr = HeapAlloc(GetProcessHeap(), 0, displen);
219 RegQueryValueExW(hkeyApp, DisplayNameW, 0, 0, (LPBYTE)entries[numentries-1].descr, &displen);
220 entries[numentries-1].command = HeapAlloc(GetProcessHeap(), 0, uninstlen);
221 entries[numentries-1].active = 0;
222 RegQueryValueExW(hkeyApp, UninstallCommandlineW, 0, 0, (LPBYTE)entries[numentries-1].command, &uninstlen);
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 qsort(entries, numentries, sizeof(uninst_entry), cmp_by_name);
232 RegCloseKey(hkeyUninst);
233 return 1;
237 static void UninstallProgram(void)
239 unsigned int i;
240 WCHAR errormsg[1024];
241 BOOL res;
242 STARTUPINFOW si;
243 PROCESS_INFORMATION info;
244 DWORD exit_code;
245 HKEY hkey;
246 for (i=0; i < numentries; i++)
248 if (!(entries[i].active)) /* don't uninstall this one */
249 continue;
250 WINE_TRACE("uninstalling %s\n", wine_dbgstr_w(entries[i].descr));
251 memset(&si, 0, sizeof(STARTUPINFOW));
252 si.cb = sizeof(STARTUPINFOW);
253 si.wShowWindow = SW_NORMAL;
254 res = CreateProcessW(NULL, entries[i].command, NULL, NULL, FALSE, 0, NULL, NULL, &si, &info);
255 if (res)
256 { /* wait for the process to exit */
257 WaitForSingleObject(info.hProcess, INFINITE);
258 res = GetExitCodeProcess(info.hProcess, &exit_code);
259 WINE_TRACE("%d: %08x\n", res, exit_code);
261 else
263 wsprintfW(errormsg, sUninstallFailed, entries[i].command);
264 if(MessageBoxW(0, errormsg, sAppName, MB_YESNO | MB_ICONQUESTION)==IDYES)
266 /* delete the application's uninstall entry */
267 RegOpenKeyExW(HKEY_LOCAL_MACHINE, PathUninstallW, 0, KEY_READ, &hkey);
268 RegDeleteKeyW(hkey, entries[i].key);
269 RegCloseKey(hkey);
273 WINE_TRACE("finished uninstall phase.\n");
274 list_need_update = 1;
278 static INT_PTR CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
280 TEXTMETRICW tm;
281 HDC hdc;
282 HWND hList = GetDlgItem(hwnd, IDC_LIST);
283 switch(Message)
285 case WM_INITDIALOG:
286 hdc = GetDC(hwnd);
287 GetTextMetricsW(hdc, &tm);
288 UpdateList(hList);
289 ReleaseDC(hwnd, hdc);
290 break;
291 case WM_COMMAND:
292 switch(LOWORD(wParam))
294 case IDC_FILTER:
296 if (HIWORD(wParam) == EN_CHANGE)
298 int len = GetWindowTextLengthW(GetDlgItem(hwnd, IDC_FILTER));
299 list_need_update = 1;
300 if(len > 0)
302 sFilter = (WCHAR*)GlobalAlloc(GPTR, (len + 1)*sizeof(WCHAR));
303 GetDlgItemTextW(hwnd, IDC_FILTER, sFilter, len + 1);
305 else sFilter = NULL;
306 UpdateList(hList);
308 break;
310 case IDC_UNINSTALL:
312 int count = SendMessageW(hList, LB_GETSELCOUNT, 0, 0);
313 if(count != 0)
315 UninstallProgram();
316 UpdateList(hList);
318 break;
320 case IDC_LIST:
321 if (HIWORD(wParam) == LBN_SELCHANGE)
323 int sel = SendMessageW(hList, LB_GETCURSEL, 0, 0);
324 if (oldsel != -1)
326 entries[oldsel].active ^= 1; /* toggle */
327 WINE_TRACE("toggling %d old %s\n", entries[oldsel].active,
328 wine_dbgstr_w(entries[oldsel].descr));
330 entries[sel].active ^= 1; /* toggle */
331 WINE_TRACE("toggling %d %s\n", entries[sel].active,
332 wine_dbgstr_w(entries[oldsel].descr));
333 oldsel = sel;
335 break;
336 case IDC_ABOUT:
337 MessageBoxW(0, sAbout, sAboutTitle, MB_OK);
338 break;
339 case IDCANCEL:
340 case IDC_EXIT:
341 EndDialog(hwnd, 0);
342 break;
344 break;
345 default:
346 return FALSE;
348 return TRUE;
352 static void UpdateList(HWND hList)
354 unsigned int i;
355 if (list_need_update)
357 int prevsel;
358 prevsel = SendMessageW(hList, LB_GETCURSEL, 0, 0);
359 if (!(FetchUninstallInformation()))
361 MessageBoxW(0, sRegistryKeyNotAvailable, sAppName, MB_OK);
362 PostQuitMessage(0);
363 return;
365 SendMessageW(hList, LB_RESETCONTENT, 0, 0);
366 SendMessageW(hList, WM_SETREDRAW, FALSE, 0);
367 for (i=0; i < numentries; i++)
369 WINE_TRACE("adding %s\n", wine_dbgstr_w(entries[i].descr));
370 SendMessageW(hList, LB_ADDSTRING, 0, (LPARAM)entries[i].descr);
372 WINE_TRACE("setting prevsel %d\n", prevsel);
373 if (prevsel != -1)
374 SendMessageW(hList, LB_SETCURSEL, prevsel, 0 );
375 SendMessageW(hList, WM_SETREDRAW, TRUE, 0);
376 list_need_update = 0;