winhttp: Move common code to the upper level, avoid accessing the buffer before the...
[wine.git] / programs / uninstaller / main.c
blob1eaa3f30bf49cdab8babf66eb7ce3dae2643eacd
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 list_need_update = 1;
46 static int oldsel = -1;
47 static WCHAR *sFilter;
48 static WCHAR sAppName[MAX_STRING_LEN];
49 static WCHAR sUninstallFailed[MAX_STRING_LEN];
51 static int FetchUninstallInformation(void);
52 static void UninstallProgram(void);
53 static int cmp_by_name(const void *a, const void *b);
55 static const WCHAR BackSlashW[] = { '\\', 0 };
56 static const WCHAR DisplayNameW[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
57 static const WCHAR PathUninstallW[] = {
58 'S','o','f','t','w','a','r','e','\\',
59 'M','i','c','r','o','s','o','f','t','\\',
60 'W','i','n','d','o','w','s','\\',
61 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
62 'U','n','i','n','s','t','a','l','l',0 };
63 static const WCHAR UninstallCommandlineW[] = {'U','n','i','n','s','t','a','l','l','S','t','r','i','n','g',0};
66 /**
67 * Used to output program list when used with --list
69 static void ListUninstallPrograms(void)
71 unsigned int i;
72 int lenDescr, lenKey;
73 char *descr;
74 char *key;
76 FetchUninstallInformation();
78 for (i=0; i < numentries; i++)
80 lenDescr = WideCharToMultiByte(CP_UNIXCP, 0, entries[i].descr, -1, NULL, 0, NULL, NULL);
81 lenKey = WideCharToMultiByte(CP_UNIXCP, 0, entries[i].key, -1, NULL, 0, NULL, NULL);
82 descr = HeapAlloc(GetProcessHeap(), 0, lenDescr);
83 key = HeapAlloc(GetProcessHeap(), 0, lenKey);
84 WideCharToMultiByte(CP_UNIXCP, 0, entries[i].descr, -1, descr, lenDescr, NULL, NULL);
85 WideCharToMultiByte(CP_UNIXCP, 0, entries[i].key, -1, key, lenKey, NULL, NULL);
86 printf("%s|||%s\n", key, descr);
87 HeapFree(GetProcessHeap(), 0, descr);
88 HeapFree(GetProcessHeap(), 0, key);
93 static void RemoveSpecificProgram(WCHAR *nameW)
95 unsigned int i;
96 int lenName;
97 char *name;
99 FetchUninstallInformation();
101 for (i=0; i < numentries; i++)
103 if (lstrcmpW(entries[i].key, nameW) == 0)
105 entries[i].active++;
106 break;
110 if (i < numentries)
111 UninstallProgram();
112 else
114 lenName = WideCharToMultiByte(CP_UNIXCP, 0, nameW, -1, NULL, 0, NULL, NULL);
115 name = HeapAlloc(GetProcessHeap(), 0, lenName);
116 WideCharToMultiByte(CP_UNIXCP, 0, nameW, -1, name, lenName, NULL, NULL);
117 fprintf(stderr, "Error: could not match application [%s]\n", name);
118 HeapFree(GetProcessHeap(), 0, name);
123 int wmain(int argc, WCHAR *argv[])
125 LPCWSTR token = NULL;
126 HINSTANCE hInst = GetModuleHandleW(0);
127 static const WCHAR listW[] = { '-','-','l','i','s','t',0 };
128 static const WCHAR removeW[] = { '-','-','r','e','m','o','v','e',0 };
129 int i = 1;
131 while( i<argc )
133 token = argv[i++];
135 /* Handle requests just to list the applications */
136 if( !lstrcmpW( token, listW ) )
138 ListUninstallPrograms();
139 return 0;
141 else if( !lstrcmpW( token, removeW ) )
143 if( i >= argc )
145 WINE_ERR( "The remove option requires a parameter.\n");
146 return 1;
149 RemoveSpecificProgram( argv[i++] );
150 return 0;
152 else
154 WINE_ERR( "unknown option %s\n",wine_dbgstr_w(token));
155 return 1;
159 /* Load MessageBox's strings */
160 LoadStringW(hInst, IDS_APPNAME, sAppName, sizeof(sAppName)/sizeof(WCHAR));
161 LoadStringW(hInst, IDS_UNINSTALLFAILED, sUninstallFailed, sizeof(sUninstallFailed)/sizeof(WCHAR));
163 /* Start the GUI control panel */
164 Control_RunDLL(GetDesktopWindow(), 0, "appwiz.cpl", SW_SHOW);
165 return 1;
170 * Used to sort entries by name.
172 static int cmp_by_name(const void *a, const void *b)
174 return lstrcmpiW(((const uninst_entry *)a)->descr, ((const uninst_entry *)b)->descr);
179 * Fetch information from the uninstall key.
181 static int FetchFromRootKey(HKEY root)
183 HKEY hkeyUninst, hkeyApp;
184 int i;
185 DWORD sizeOfSubKeyName, displen, uninstlen;
186 WCHAR subKeyName[256];
187 WCHAR key_app[1024];
188 WCHAR *p;
190 if (RegOpenKeyExW(root, PathUninstallW, 0, KEY_READ, &hkeyUninst) != ERROR_SUCCESS)
191 return 0;
193 lstrcpyW(key_app, PathUninstallW);
194 lstrcatW(key_app, BackSlashW);
195 p = key_app+lstrlenW(PathUninstallW)+1;
197 sizeOfSubKeyName = 255;
198 for (i=0; RegEnumKeyExW( hkeyUninst, i, subKeyName, &sizeOfSubKeyName, NULL, NULL, NULL, NULL ) != ERROR_NO_MORE_ITEMS; ++i)
200 lstrcpyW(p, subKeyName);
201 RegOpenKeyExW(root, key_app, 0, KEY_READ, &hkeyApp);
202 if ((RegQueryValueExW(hkeyApp, DisplayNameW, 0, 0, NULL, &displen) == ERROR_SUCCESS)
203 && (RegQueryValueExW(hkeyApp, UninstallCommandlineW, 0, 0, NULL, &uninstlen) == ERROR_SUCCESS))
205 numentries++;
206 entries = HeapReAlloc(GetProcessHeap(), 0, entries, numentries*sizeof(uninst_entry));
207 entries[numentries-1].root = root;
208 entries[numentries-1].key = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(subKeyName)+1)*sizeof(WCHAR));
209 lstrcpyW(entries[numentries-1].key, subKeyName);
210 entries[numentries-1].descr = HeapAlloc(GetProcessHeap(), 0, displen);
211 RegQueryValueExW(hkeyApp, DisplayNameW, 0, 0, (LPBYTE)entries[numentries-1].descr, &displen);
212 entries[numentries-1].command = HeapAlloc(GetProcessHeap(), 0, uninstlen);
213 entries[numentries-1].active = 0;
214 RegQueryValueExW(hkeyApp, UninstallCommandlineW, 0, 0, (LPBYTE)entries[numentries-1].command, &uninstlen);
215 WINE_TRACE("allocated entry #%d: %s (%s), %s\n",
216 numentries, wine_dbgstr_w(entries[numentries-1].key), wine_dbgstr_w(entries[numentries-1].descr), wine_dbgstr_w(entries[numentries-1].command));
217 if(sFilter != NULL && StrStrIW(entries[numentries-1].descr,sFilter)==NULL)
218 numentries--;
220 RegCloseKey(hkeyApp);
221 sizeOfSubKeyName = 255;
223 RegCloseKey(hkeyUninst);
224 return 1;
228 static int FetchUninstallInformation(void)
230 int rc;
232 numentries = 0;
233 oldsel = -1;
234 if (!entries)
235 entries = HeapAlloc(GetProcessHeap(), 0, sizeof(uninst_entry));
237 rc = FetchFromRootKey(HKEY_LOCAL_MACHINE);
238 rc |= FetchFromRootKey(HKEY_CURRENT_USER);
240 qsort(entries, numentries, sizeof(uninst_entry), cmp_by_name);
241 return rc;
244 static void UninstallProgram(void)
246 unsigned int i;
247 WCHAR errormsg[1024];
248 BOOL res;
249 STARTUPINFOW si;
250 PROCESS_INFORMATION info;
251 DWORD exit_code;
252 HKEY hkey;
253 for (i=0; i < numentries; i++)
255 if (!(entries[i].active)) /* don't uninstall this one */
256 continue;
257 WINE_TRACE("uninstalling %s\n", wine_dbgstr_w(entries[i].descr));
258 memset(&si, 0, sizeof(STARTUPINFOW));
259 si.cb = sizeof(STARTUPINFOW);
260 si.wShowWindow = SW_NORMAL;
261 res = CreateProcessW(NULL, entries[i].command, NULL, NULL, FALSE, 0, NULL, NULL, &si, &info);
262 if (res)
263 { /* wait for the process to exit */
264 WaitForSingleObject(info.hProcess, INFINITE);
265 res = GetExitCodeProcess(info.hProcess, &exit_code);
266 WINE_TRACE("%d: %08x\n", res, exit_code);
268 else
270 wsprintfW(errormsg, sUninstallFailed, entries[i].command);
271 if(MessageBoxW(0, errormsg, sAppName, MB_YESNO | MB_ICONQUESTION)==IDYES)
273 /* delete the application's uninstall entry */
274 RegOpenKeyExW(entries[i].root, PathUninstallW, 0, KEY_READ, &hkey);
275 RegDeleteKeyW(hkey, entries[i].key);
276 RegCloseKey(hkey);
280 WINE_TRACE("finished uninstall phase.\n");
281 list_need_update = 1;