d3dx9: Dump preshader immediates table.
[wine.git] / programs / uninstaller / main.c
blob6909e0fda793ba2d2688d9154121096dbc95042b
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 <string.h>
25 #include <windows.h>
26 #include <shlwapi.h>
27 #include "resource.h"
28 #include "regstr.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(uninstaller);
33 extern void WINAPI Control_RunDLL(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow);
35 typedef struct {
36 HKEY root;
37 WCHAR *key;
38 WCHAR *descr;
39 WCHAR *command;
40 int active;
41 } uninst_entry;
42 static uninst_entry *entries = NULL;
43 static unsigned int numentries = 0;
44 static int oldsel = -1;
45 static WCHAR *sFilter;
47 static int FetchUninstallInformation(void);
48 static void UninstallProgram(void);
49 static int cmp_by_name(const void *a, const void *b);
51 static const WCHAR DisplayNameW[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
52 static const WCHAR PathUninstallW[] = {
53 'S','o','f','t','w','a','r','e','\\',
54 'M','i','c','r','o','s','o','f','t','\\',
55 'W','i','n','d','o','w','s','\\',
56 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
57 'U','n','i','n','s','t','a','l','l',0 };
58 static const WCHAR UninstallCommandlineW[] = {'U','n','i','n','s','t','a','l','l','S','t','r','i','n','g',0};
59 static const WCHAR WindowsInstallerW[] = {'W','i','n','d','o','w','s','I','n','s','t','a','l','l','e','r',0};
60 static const WCHAR SystemComponentW[] = {'S','y','s','t','e','m','C','o','m','p','o','n','e','n','t',0};
62 static void output_writeconsole(const WCHAR *str, DWORD len)
64 DWORD written, ret, lenA;
65 char *strA;
67 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, len, &written, NULL);
68 if (ret) return;
70 /* WriteConsole fails if its output is redirected to a file.
71 * If this occurs, we should use an OEM codepage and call WriteFile.
73 lenA = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len, NULL, 0, NULL, NULL);
74 strA = HeapAlloc(GetProcessHeap(), 0, lenA);
75 if (strA)
77 WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len, strA, lenA, NULL, NULL);
78 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), strA, lenA, &written, FALSE);
79 HeapFree(GetProcessHeap(), 0, strA);
83 static void output_formatstring(const WCHAR *fmt, __ms_va_list va_args)
85 WCHAR *str;
86 DWORD len;
88 SetLastError(NO_ERROR);
89 len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
90 fmt, 0, 0, (LPWSTR)&str, 0, &va_args);
91 if (len == 0 && GetLastError() != NO_ERROR)
93 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
94 return;
96 output_writeconsole(str, len);
97 LocalFree(str);
100 static void __cdecl output_message(unsigned int id, ...)
102 WCHAR fmt[1024];
103 __ms_va_list va_args;
105 if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, sizeof(fmt)/sizeof(fmt[0])))
107 WINE_FIXME("LoadString failed with %d\n", GetLastError());
108 return;
110 __ms_va_start(va_args, id);
111 output_formatstring(fmt, va_args);
112 __ms_va_end(va_args);
115 static void __cdecl output_array(WCHAR *fmt, ...)
117 __ms_va_list va_args;
119 __ms_va_start(va_args, fmt);
120 output_formatstring(fmt, va_args);
121 __ms_va_end(va_args);
125 * Used to output program list when used with --list
127 static void ListUninstallPrograms(void)
129 unsigned int i;
130 static WCHAR fmtW[] = {'%','1','|','|','|','%','2','\n',0};
132 FetchUninstallInformation();
134 for (i=0; i < numentries; i++)
135 output_array(fmtW, entries[i].key, entries[i].descr);
139 static void RemoveSpecificProgram(WCHAR *nameW)
141 unsigned int i;
143 FetchUninstallInformation();
145 for (i=0; i < numentries; i++)
147 if (CompareStringW(GetThreadLocale(), NORM_IGNORECASE, entries[i].key, -1, nameW, -1) == CSTR_EQUAL)
149 entries[i].active++;
150 break;
154 if (i < numentries)
155 UninstallProgram();
156 else
157 output_message(STRING_NO_APP_MATCH, nameW);
161 int wmain(int argc, WCHAR *argv[])
163 LPCWSTR token = NULL;
164 static const WCHAR helpW[] = { '-','-','h','e','l','p',0 };
165 static const WCHAR listW[] = { '-','-','l','i','s','t',0 };
166 static const WCHAR removeW[] = { '-','-','r','e','m','o','v','e',0 };
167 int i = 1;
169 while( i<argc )
171 token = argv[i++];
173 if( !lstrcmpW( token, helpW ) )
175 output_message(STRING_HEADER);
176 output_message(STRING_USAGE);
177 return 0;
179 else if( !lstrcmpW( token, listW ) )
181 ListUninstallPrograms();
182 return 0;
184 else if( !lstrcmpW( token, removeW ) )
186 if( i >= argc )
188 output_message(STRING_PARAMETER_REQUIRED);
189 return 1;
192 RemoveSpecificProgram( argv[i++] );
193 return 0;
195 else
197 output_message(STRING_INVALID_OPTION, token);
198 return 1;
202 /* Start the GUI control panel */
203 Control_RunDLL(GetDesktopWindow(), 0, "appwiz.cpl", SW_SHOW);
204 return 1;
209 * Used to sort entries by name.
211 static int cmp_by_name(const void *a, const void *b)
213 return lstrcmpiW(((const uninst_entry *)a)->descr, ((const uninst_entry *)b)->descr);
218 * Fetch information from the uninstall key.
220 static int FetchFromRootKey(HKEY root)
222 HKEY hkeyApp;
223 int i;
224 DWORD sizeOfSubKeyName, displen, uninstlen, value, type, size;
225 WCHAR subKeyName[256];
227 sizeOfSubKeyName = 255;
228 for (i=0; RegEnumKeyExW( root, i, subKeyName, &sizeOfSubKeyName, NULL, NULL, NULL, NULL ) != ERROR_NO_MORE_ITEMS; ++i)
230 RegOpenKeyExW(root, subKeyName, 0, KEY_READ, &hkeyApp);
231 if (!RegQueryValueExW(hkeyApp, SystemComponentW, NULL, &type, (LPBYTE)&value, &size) &&
232 type == REG_DWORD && value == 1)
234 RegCloseKey(hkeyApp);
235 sizeOfSubKeyName = 255;
236 continue;
238 if (!RegQueryValueExW(hkeyApp, DisplayNameW, NULL, NULL, NULL, &displen))
240 WCHAR *command;
242 size = sizeof(value);
243 if (!RegQueryValueExW(hkeyApp, WindowsInstallerW, NULL, &type, (LPBYTE)&value, &size) &&
244 type == REG_DWORD && value == 1)
246 static const WCHAR fmtW[] = {'m','s','i','e','x','e','c',' ','/','x','%','s',0};
247 command = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(fmtW) + lstrlenW(subKeyName)) * sizeof(WCHAR));
248 wsprintfW(command, fmtW, subKeyName);
250 else if (!RegQueryValueExW(hkeyApp, UninstallCommandlineW, NULL, NULL, NULL, &uninstlen))
252 command = HeapAlloc(GetProcessHeap(), 0, uninstlen);
253 RegQueryValueExW(hkeyApp, UninstallCommandlineW, 0, 0, (LPBYTE)command, &uninstlen);
255 else
257 RegCloseKey(hkeyApp);
258 sizeOfSubKeyName = 255;
259 continue;
261 numentries++;
262 entries = HeapReAlloc(GetProcessHeap(), 0, entries, numentries*sizeof(uninst_entry));
263 entries[numentries-1].root = root;
264 entries[numentries-1].key = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(subKeyName)+1)*sizeof(WCHAR));
265 lstrcpyW(entries[numentries-1].key, subKeyName);
266 entries[numentries-1].descr = HeapAlloc(GetProcessHeap(), 0, displen);
267 RegQueryValueExW(hkeyApp, DisplayNameW, 0, 0, (LPBYTE)entries[numentries-1].descr, &displen);
268 entries[numentries-1].command = command;
269 entries[numentries-1].active = 0;
270 WINE_TRACE("allocated entry #%d: %s (%s), %s\n",
271 numentries, wine_dbgstr_w(entries[numentries-1].key), wine_dbgstr_w(entries[numentries-1].descr), wine_dbgstr_w(entries[numentries-1].command));
272 if(sFilter != NULL && StrStrIW(entries[numentries-1].descr,sFilter)==NULL)
273 numentries--;
275 RegCloseKey(hkeyApp);
276 sizeOfSubKeyName = 255;
278 return 1;
282 static int FetchUninstallInformation(void)
284 static const BOOL is_64bit = sizeof(void *) > sizeof(int);
285 int rc = 0;
286 HKEY root;
288 numentries = 0;
289 oldsel = -1;
290 if (!entries)
291 entries = HeapAlloc(GetProcessHeap(), 0, sizeof(uninst_entry));
293 if (!RegOpenKeyExW(HKEY_LOCAL_MACHINE, PathUninstallW, 0, KEY_READ, &root))
295 rc |= FetchFromRootKey(root);
296 RegCloseKey(root);
298 if (is_64bit &&
299 !RegOpenKeyExW(HKEY_LOCAL_MACHINE, PathUninstallW, 0, KEY_READ|KEY_WOW64_32KEY, &root))
301 rc |= FetchFromRootKey(root);
302 RegCloseKey(root);
304 if (!RegOpenKeyExW(HKEY_CURRENT_USER, PathUninstallW, 0, KEY_READ, &root))
306 rc |= FetchFromRootKey(root);
307 RegCloseKey(root);
310 qsort(entries, numentries, sizeof(uninst_entry), cmp_by_name);
311 return rc;
314 static void UninstallProgram(void)
316 unsigned int i;
317 WCHAR errormsg[1024];
318 BOOL res;
319 STARTUPINFOW si;
320 PROCESS_INFORMATION info;
321 DWORD exit_code;
322 HKEY hkey;
323 for (i=0; i < numentries; i++)
325 if (!(entries[i].active)) /* don't uninstall this one */
326 continue;
327 WINE_TRACE("uninstalling %s\n", wine_dbgstr_w(entries[i].descr));
328 memset(&si, 0, sizeof(STARTUPINFOW));
329 si.cb = sizeof(STARTUPINFOW);
330 si.wShowWindow = SW_NORMAL;
331 res = CreateProcessW(NULL, entries[i].command, NULL, NULL, FALSE, 0, NULL, NULL, &si, &info);
332 if (res)
333 { /* wait for the process to exit */
334 WaitForSingleObject(info.hProcess, INFINITE);
335 res = GetExitCodeProcess(info.hProcess, &exit_code);
336 WINE_TRACE("%d: %08x\n", res, exit_code);
338 else
340 WCHAR sAppName[MAX_STRING_LEN];
341 WCHAR sUninstallFailed[MAX_STRING_LEN];
342 HINSTANCE hInst = GetModuleHandleW(0);
344 LoadStringW(hInst, IDS_APPNAME, sAppName, sizeof(sAppName)/sizeof(WCHAR));
345 LoadStringW(hInst, IDS_UNINSTALLFAILED, sUninstallFailed, sizeof(sUninstallFailed)/sizeof(WCHAR));
346 wsprintfW(errormsg, sUninstallFailed, entries[i].command);
347 if(MessageBoxW(0, errormsg, sAppName, MB_YESNO | MB_ICONQUESTION)==IDYES)
349 /* delete the application's uninstall entry */
350 RegOpenKeyExW(entries[i].root, PathUninstallW, 0, KEY_READ, &hkey);
351 RegDeleteKeyW(hkey, entries[i].key);
352 RegCloseKey(hkey);
356 WINE_TRACE("finished uninstall phase.\n");