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 WINAPIV
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 WINAPIV
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 size
= sizeof(value
);
232 if (!RegQueryValueExW(hkeyApp
, SystemComponentW
, NULL
, &type
, (LPBYTE
)&value
, &size
) &&
233 type
== REG_DWORD
&& value
== 1)
235 RegCloseKey(hkeyApp
);
236 sizeOfSubKeyName
= 255;
239 if (!RegQueryValueExW(hkeyApp
, DisplayNameW
, NULL
, NULL
, NULL
, &displen
))
243 size
= sizeof(value
);
244 if (!RegQueryValueExW(hkeyApp
, WindowsInstallerW
, NULL
, &type
, (LPBYTE
)&value
, &size
) &&
245 type
== REG_DWORD
&& value
== 1)
247 static const WCHAR fmtW
[] = {'m','s','i','e','x','e','c',' ','/','x','%','s',0};
248 command
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(fmtW
) + lstrlenW(subKeyName
)) * sizeof(WCHAR
));
249 wsprintfW(command
, fmtW
, subKeyName
);
251 else if (!RegQueryValueExW(hkeyApp
, UninstallCommandlineW
, NULL
, NULL
, NULL
, &uninstlen
))
253 command
= HeapAlloc(GetProcessHeap(), 0, uninstlen
);
254 RegQueryValueExW(hkeyApp
, UninstallCommandlineW
, 0, 0, (LPBYTE
)command
, &uninstlen
);
258 RegCloseKey(hkeyApp
);
259 sizeOfSubKeyName
= 255;
263 entries
= HeapReAlloc(GetProcessHeap(), 0, entries
, numentries
*sizeof(uninst_entry
));
264 entries
[numentries
-1].root
= root
;
265 entries
[numentries
-1].key
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(subKeyName
)+1)*sizeof(WCHAR
));
266 lstrcpyW(entries
[numentries
-1].key
, subKeyName
);
267 entries
[numentries
-1].descr
= HeapAlloc(GetProcessHeap(), 0, displen
);
268 RegQueryValueExW(hkeyApp
, DisplayNameW
, 0, 0, (LPBYTE
)entries
[numentries
-1].descr
, &displen
);
269 entries
[numentries
-1].command
= command
;
270 entries
[numentries
-1].active
= 0;
271 WINE_TRACE("allocated entry #%d: %s (%s), %s\n",
272 numentries
, wine_dbgstr_w(entries
[numentries
-1].key
), wine_dbgstr_w(entries
[numentries
-1].descr
), wine_dbgstr_w(entries
[numentries
-1].command
));
273 if(sFilter
!= NULL
&& StrStrIW(entries
[numentries
-1].descr
,sFilter
)==NULL
)
276 RegCloseKey(hkeyApp
);
277 sizeOfSubKeyName
= 255;
283 static int FetchUninstallInformation(void)
285 static const BOOL is_64bit
= sizeof(void *) > sizeof(int);
292 entries
= HeapAlloc(GetProcessHeap(), 0, sizeof(uninst_entry
));
294 if (!RegOpenKeyExW(HKEY_LOCAL_MACHINE
, PathUninstallW
, 0, KEY_READ
, &root
))
296 rc
|= FetchFromRootKey(root
);
300 !RegOpenKeyExW(HKEY_LOCAL_MACHINE
, PathUninstallW
, 0, KEY_READ
|KEY_WOW64_32KEY
, &root
))
302 rc
|= FetchFromRootKey(root
);
305 if (!RegOpenKeyExW(HKEY_CURRENT_USER
, PathUninstallW
, 0, KEY_READ
, &root
))
307 rc
|= FetchFromRootKey(root
);
311 qsort(entries
, numentries
, sizeof(uninst_entry
), cmp_by_name
);
315 static void UninstallProgram(void)
318 WCHAR errormsg
[1024];
321 PROCESS_INFORMATION info
;
324 for (i
=0; i
< numentries
; i
++)
326 if (!(entries
[i
].active
)) /* don't uninstall this one */
328 WINE_TRACE("uninstalling %s\n", wine_dbgstr_w(entries
[i
].descr
));
329 memset(&si
, 0, sizeof(STARTUPINFOW
));
330 si
.cb
= sizeof(STARTUPINFOW
);
331 si
.wShowWindow
= SW_NORMAL
;
332 res
= CreateProcessW(NULL
, entries
[i
].command
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &info
);
334 { /* wait for the process to exit */
335 WaitForSingleObject(info
.hProcess
, INFINITE
);
336 res
= GetExitCodeProcess(info
.hProcess
, &exit_code
);
337 WINE_TRACE("%d: %08x\n", res
, exit_code
);
341 WCHAR sAppName
[MAX_STRING_LEN
];
342 WCHAR sUninstallFailed
[MAX_STRING_LEN
];
343 HINSTANCE hInst
= GetModuleHandleW(0);
345 LoadStringW(hInst
, IDS_APPNAME
, sAppName
, sizeof(sAppName
)/sizeof(WCHAR
));
346 LoadStringW(hInst
, IDS_UNINSTALLFAILED
, sUninstallFailed
, sizeof(sUninstallFailed
)/sizeof(WCHAR
));
347 wsprintfW(errormsg
, sUninstallFailed
, entries
[i
].command
);
348 if(MessageBoxW(0, errormsg
, sAppName
, MB_YESNO
| MB_ICONQUESTION
)==IDYES
)
350 /* delete the application's uninstall entry */
351 RegOpenKeyExW(entries
[i
].root
, PathUninstallW
, 0, KEY_READ
, &hkey
);
352 RegDeleteKeyW(hkey
, entries
[i
].key
);
357 WINE_TRACE("finished uninstall phase.\n");