ntoskrnl.exe/tests: Add some IOCTL_HID_WRITE_REPORT tests.
[wine.git] / programs / uninstaller / main.c
blob8941d3a47adb078ce31101bf43fb6d0052bc9395
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 <commctrl.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 const WCHAR PathUninstallW[] = L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
52 static void output_writeconsole(const WCHAR *str, DWORD len)
54 DWORD written, ret, lenA;
55 char *strA;
57 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, len, &written, NULL);
58 if (ret) return;
60 /* WriteConsole fails if its output is redirected to a file.
61 * If this occurs, we should use an OEM codepage and call WriteFile.
63 lenA = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len, NULL, 0, NULL, NULL);
64 strA = HeapAlloc(GetProcessHeap(), 0, lenA);
65 if (strA)
67 WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len, strA, lenA, NULL, NULL);
68 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), strA, lenA, &written, FALSE);
69 HeapFree(GetProcessHeap(), 0, strA);
73 static void output_formatstring(const WCHAR *fmt, __ms_va_list va_args)
75 WCHAR *str;
76 DWORD len;
78 len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
79 fmt, 0, 0, (LPWSTR)&str, 0, &va_args);
80 if (len == 0 && GetLastError() != ERROR_NO_WORK_DONE)
82 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
83 return;
85 output_writeconsole(str, len);
86 LocalFree(str);
89 static void WINAPIV output_message(unsigned int id, ...)
91 WCHAR fmt[1024];
92 __ms_va_list va_args;
94 if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, ARRAY_SIZE(fmt)))
96 WINE_FIXME("LoadString failed with %d\n", GetLastError());
97 return;
99 __ms_va_start(va_args, id);
100 output_formatstring(fmt, va_args);
101 __ms_va_end(va_args);
104 static void WINAPIV output_array(const WCHAR *fmt, ...)
106 __ms_va_list va_args;
108 __ms_va_start(va_args, fmt);
109 output_formatstring(fmt, va_args);
110 __ms_va_end(va_args);
114 * Used to output program list when used with --list
116 static void ListUninstallPrograms(void)
118 unsigned int i;
120 FetchUninstallInformation();
122 for (i=0; i < numentries; i++)
123 output_array(L"%1|||%2\n", entries[i].key, entries[i].descr);
127 static void RemoveSpecificProgram(WCHAR *nameW)
129 unsigned int i;
131 FetchUninstallInformation();
133 for (i=0; i < numentries; i++)
135 if (CompareStringW(GetThreadLocale(), NORM_IGNORECASE, entries[i].key, -1, nameW, -1) == CSTR_EQUAL)
137 entries[i].active++;
138 break;
142 if (i < numentries)
143 UninstallProgram();
144 else
145 output_message(STRING_NO_APP_MATCH, nameW);
149 int __cdecl wmain(int argc, WCHAR *argv[])
151 LPCWSTR token = NULL;
152 int i = 1;
153 BOOL is_wow64;
155 if (IsWow64Process( GetCurrentProcess(), &is_wow64 ) && is_wow64)
157 STARTUPINFOW si;
158 PROCESS_INFORMATION pi;
159 WCHAR filename[MAX_PATH];
160 void *redir;
161 DWORD exit_code;
163 memset( &si, 0, sizeof(si) );
164 si.cb = sizeof(si);
165 GetSystemDirectoryW( filename, MAX_PATH );
166 wcscat( filename, L"\\uninstaller.exe" );
168 Wow64DisableWow64FsRedirection( &redir );
169 if (CreateProcessW( filename, GetCommandLineW(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
171 WINE_TRACE( "restarting %s\n", wine_dbgstr_w(filename) );
172 WaitForSingleObject( pi.hProcess, INFINITE );
173 GetExitCodeProcess( pi.hProcess, &exit_code );
174 ExitProcess( exit_code );
176 else WINE_ERR( "failed to restart 64-bit %s, err %d\n", wine_dbgstr_w(filename), GetLastError() );
177 Wow64RevertWow64FsRedirection( redir );
180 InitCommonControls();
182 while( i<argc )
184 token = argv[i++];
186 if( !lstrcmpW( token, L"--help" ) )
188 output_message(STRING_HEADER);
189 output_message(STRING_USAGE);
190 return 0;
192 else if( !lstrcmpW( token, L"--list" ) )
194 ListUninstallPrograms();
195 return 0;
197 else if( !lstrcmpW( token, L"--remove" ) )
199 if( i >= argc )
201 output_message(STRING_PARAMETER_REQUIRED);
202 return 1;
205 RemoveSpecificProgram( argv[i++] );
206 return 0;
208 else
210 output_message(STRING_INVALID_OPTION, token);
211 return 1;
215 /* Start the GUI control panel */
216 Control_RunDLL(GetDesktopWindow(), 0, "appwiz.cpl", SW_SHOW);
217 return 1;
222 * Used to sort entries by name.
224 static int __cdecl cmp_by_name(const void *a, const void *b)
226 return lstrcmpiW(((const uninst_entry *)a)->descr, ((const uninst_entry *)b)->descr);
231 * Fetch information from the uninstall key.
233 static int FetchFromRootKey(HKEY root)
235 HKEY hkeyApp;
236 int i;
237 DWORD sizeOfSubKeyName, displen, uninstlen, value, type, size;
238 WCHAR subKeyName[256];
240 sizeOfSubKeyName = 255;
241 for (i=0; RegEnumKeyExW( root, i, subKeyName, &sizeOfSubKeyName, NULL, NULL, NULL, NULL ) != ERROR_NO_MORE_ITEMS; ++i)
243 RegOpenKeyExW(root, subKeyName, 0, KEY_READ, &hkeyApp);
244 size = sizeof(value);
245 if (!RegQueryValueExW(hkeyApp, L"SystemComponent", NULL, &type, (BYTE *)&value, &size) &&
246 type == REG_DWORD && value == 1)
248 RegCloseKey(hkeyApp);
249 sizeOfSubKeyName = 255;
250 continue;
252 if (!RegQueryValueExW(hkeyApp, L"DisplayName", NULL, NULL, NULL, &displen))
254 WCHAR *command;
256 size = sizeof(value);
257 if (!RegQueryValueExW(hkeyApp, L"WindowsInstaller", NULL, &type, (BYTE *)&value, &size) &&
258 type == REG_DWORD && value == 1)
260 command = HeapAlloc(GetProcessHeap(), 0,
261 (lstrlenW(L"msiexec /x%s") + lstrlenW(subKeyName)) * sizeof(WCHAR));
262 wsprintfW(command, L"msiexec /x%s", subKeyName);
264 else if (!RegQueryValueExW(hkeyApp, L"UninstallString", NULL, NULL, NULL, &uninstlen))
266 command = HeapAlloc(GetProcessHeap(), 0, uninstlen);
267 RegQueryValueExW(hkeyApp, L"UninstallString", 0, 0, (BYTE *)command, &uninstlen);
269 else
271 RegCloseKey(hkeyApp);
272 sizeOfSubKeyName = 255;
273 continue;
275 numentries++;
276 entries = HeapReAlloc(GetProcessHeap(), 0, entries, numentries*sizeof(uninst_entry));
277 entries[numentries-1].root = root;
278 entries[numentries-1].key = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(subKeyName)+1)*sizeof(WCHAR));
279 lstrcpyW(entries[numentries-1].key, subKeyName);
280 entries[numentries-1].descr = HeapAlloc(GetProcessHeap(), 0, displen);
281 RegQueryValueExW(hkeyApp, L"DisplayName", 0, 0, (BYTE *)entries[numentries-1].descr, &displen);
282 entries[numentries-1].command = command;
283 entries[numentries-1].active = 0;
284 WINE_TRACE("allocated entry #%d: %s (%s), %s\n",
285 numentries, wine_dbgstr_w(entries[numentries-1].key), wine_dbgstr_w(entries[numentries-1].descr), wine_dbgstr_w(entries[numentries-1].command));
286 if(sFilter != NULL && StrStrIW(entries[numentries-1].descr,sFilter)==NULL)
287 numentries--;
289 RegCloseKey(hkeyApp);
290 sizeOfSubKeyName = 255;
292 return 1;
296 static int FetchUninstallInformation(void)
298 static const BOOL is_64bit = sizeof(void *) > sizeof(int);
299 int rc = 0;
300 HKEY root;
302 numentries = 0;
303 oldsel = -1;
304 if (!entries)
305 entries = HeapAlloc(GetProcessHeap(), 0, sizeof(uninst_entry));
307 if (!RegOpenKeyExW(HKEY_LOCAL_MACHINE, PathUninstallW, 0, KEY_READ, &root))
309 rc |= FetchFromRootKey(root);
310 RegCloseKey(root);
312 if (is_64bit &&
313 !RegOpenKeyExW(HKEY_LOCAL_MACHINE, PathUninstallW, 0, KEY_READ|KEY_WOW64_32KEY, &root))
315 rc |= FetchFromRootKey(root);
316 RegCloseKey(root);
318 if (!RegOpenKeyExW(HKEY_CURRENT_USER, PathUninstallW, 0, KEY_READ, &root))
320 rc |= FetchFromRootKey(root);
321 RegCloseKey(root);
324 qsort(entries, numentries, sizeof(uninst_entry), cmp_by_name);
325 return rc;
328 static void UninstallProgram(void)
330 unsigned int i;
331 WCHAR errormsg[1024];
332 BOOL res;
333 STARTUPINFOW si;
334 PROCESS_INFORMATION info;
335 DWORD exit_code;
336 HKEY hkey;
337 for (i=0; i < numentries; i++)
339 if (!(entries[i].active)) /* don't uninstall this one */
340 continue;
341 WINE_TRACE("uninstalling %s\n", wine_dbgstr_w(entries[i].descr));
342 memset(&si, 0, sizeof(STARTUPINFOW));
343 si.cb = sizeof(STARTUPINFOW);
344 si.wShowWindow = SW_NORMAL;
345 res = CreateProcessW(NULL, entries[i].command, NULL, NULL, FALSE, 0, NULL, NULL, &si, &info);
346 if (res)
347 { /* wait for the process to exit */
348 WaitForSingleObject(info.hProcess, INFINITE);
349 res = GetExitCodeProcess(info.hProcess, &exit_code);
350 WINE_TRACE("%d: %08x\n", res, exit_code);
352 else
354 WCHAR sAppName[MAX_STRING_LEN];
355 WCHAR sUninstallFailed[MAX_STRING_LEN];
356 HINSTANCE hInst = GetModuleHandleW(0);
358 LoadStringW(hInst, IDS_APPNAME, sAppName, ARRAY_SIZE(sAppName));
359 LoadStringW(hInst, IDS_UNINSTALLFAILED, sUninstallFailed, ARRAY_SIZE(sUninstallFailed));
360 wsprintfW(errormsg, sUninstallFailed, entries[i].command);
361 if(MessageBoxW(0, errormsg, sAppName, MB_YESNO | MB_ICONQUESTION)==IDYES)
363 /* delete the application's uninstall entry */
364 RegOpenKeyExW(entries[i].root, PathUninstallW, 0, KEY_READ, &hkey);
365 RegDeleteKeyW(hkey, entries[i].key);
366 RegCloseKey(hkey);
370 WINE_TRACE("finished uninstall phase.\n");