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
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
);
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
;
67 ret
= WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), str
, len
, &written
, NULL
);
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
);
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
)
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
));
96 output_writeconsole(str
, len
);
100 static void __cdecl
output_message(unsigned int id
, ...)
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());
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)
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
)
143 FetchUninstallInformation();
145 for (i
=0; i
< numentries
; i
++)
147 if (CompareStringW(GetThreadLocale(), NORM_IGNORECASE
, entries
[i
].key
, -1, nameW
, -1) == CSTR_EQUAL
)
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 };
173 if( !lstrcmpW( token
, helpW
) )
175 output_message(STRING_HEADER
);
176 output_message(STRING_USAGE
);
179 else if( !lstrcmpW( token
, listW
) )
181 ListUninstallPrograms();
184 else if( !lstrcmpW( token
, removeW
) )
188 output_message(STRING_PARAMETER_REQUIRED
);
192 RemoveSpecificProgram( argv
[i
++] );
197 output_message(STRING_INVALID_OPTION
, token
);
202 /* Start the GUI control panel */
203 Control_RunDLL(GetDesktopWindow(), 0, "appwiz.cpl", SW_SHOW
);
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
)
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;
238 if (!RegQueryValueExW(hkeyApp
, DisplayNameW
, NULL
, NULL
, NULL
, &displen
))
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
);
257 RegCloseKey(hkeyApp
);
258 sizeOfSubKeyName
= 255;
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
)
275 RegCloseKey(hkeyApp
);
276 sizeOfSubKeyName
= 255;
282 static int FetchUninstallInformation(void)
284 static const BOOL is_64bit
= sizeof(void *) > sizeof(int);
291 entries
= HeapAlloc(GetProcessHeap(), 0, sizeof(uninst_entry
));
293 if (!RegOpenKeyExW(HKEY_LOCAL_MACHINE
, PathUninstallW
, 0, KEY_READ
, &root
))
295 rc
|= FetchFromRootKey(root
);
299 !RegOpenKeyExW(HKEY_LOCAL_MACHINE
, PathUninstallW
, 0, KEY_READ
|KEY_WOW64_32KEY
, &root
))
301 rc
|= FetchFromRootKey(root
);
304 if (!RegOpenKeyExW(HKEY_CURRENT_USER
, PathUninstallW
, 0, KEY_READ
, &root
))
306 rc
|= FetchFromRootKey(root
);
310 qsort(entries
, numentries
, sizeof(uninst_entry
), cmp_by_name
);
314 static void UninstallProgram(void)
317 WCHAR errormsg
[1024];
320 PROCESS_INFORMATION info
;
323 for (i
=0; i
< numentries
; i
++)
325 if (!(entries
[i
].active
)) /* don't uninstall this one */
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
);
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
);
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
);
356 WINE_TRACE("finished uninstall phase.\n");