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
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
);
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
)
57 if (WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), str
, len
, &written
, NULL
))
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(GetOEMCP(), 0, str
, len
, NULL
, 0, NULL
, NULL
);
64 strA
= HeapAlloc(GetProcessHeap(), 0, lenA
);
67 WideCharToMultiByte(GetOEMCP(), 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
, va_list va_args
)
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=%lu, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt
));
85 output_writeconsole(str
, len
);
89 static void WINAPIV
output_message(unsigned int id
, ...)
94 if (!LoadStringW(GetModuleHandleW(NULL
), id
, fmt
, ARRAY_SIZE(fmt
)))
96 WINE_FIXME("LoadString failed with %ld\n", GetLastError());
99 va_start(va_args
, id
);
100 output_formatstring(fmt
, va_args
);
104 static void WINAPIV
output_array(const WCHAR
*fmt
, ...)
108 va_start(va_args
, fmt
);
109 output_formatstring(fmt
, va_args
);
114 * Used to output program list when used with --list
116 static void ListUninstallPrograms(void)
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
)
131 FetchUninstallInformation();
133 for (i
=0; i
< numentries
; i
++)
135 if (CompareStringW(GetThreadLocale(), NORM_IGNORECASE
, entries
[i
].key
, -1, nameW
, -1) == CSTR_EQUAL
)
145 output_message(STRING_NO_APP_MATCH
, nameW
);
149 int __cdecl
wmain(int argc
, WCHAR
*argv
[])
151 LPCWSTR token
= NULL
;
155 if (IsWow64Process( GetCurrentProcess(), &is_wow64
) && is_wow64
)
158 PROCESS_INFORMATION pi
;
159 WCHAR filename
[MAX_PATH
];
163 memset( &si
, 0, 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 %ld\n", wine_dbgstr_w(filename
), GetLastError() );
177 Wow64RevertWow64FsRedirection( redir
);
180 InitCommonControls();
186 if( !lstrcmpW( token
, L
"--help" ) )
188 output_message(STRING_HEADER
);
189 output_message(STRING_USAGE
);
192 else if( !lstrcmpW( token
, L
"--list" ) )
194 ListUninstallPrograms();
197 else if( !lstrcmpW( token
, L
"--remove" ) )
201 output_message(STRING_PARAMETER_REQUIRED
);
205 RemoveSpecificProgram( argv
[i
++] );
210 output_message(STRING_INVALID_OPTION
, token
);
215 /* Start the GUI control panel */
216 Control_RunDLL(GetDesktopWindow(), 0, "appwiz.cpl", SW_SHOW
);
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
)
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;
252 if (!RegQueryValueExW(hkeyApp
, L
"DisplayName", NULL
, NULL
, NULL
, &displen
))
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
);
271 RegCloseKey(hkeyApp
);
272 sizeOfSubKeyName
= 255;
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
)
289 RegCloseKey(hkeyApp
);
290 sizeOfSubKeyName
= 255;
296 static int FetchUninstallInformation(void)
298 static const BOOL is_64bit
= sizeof(void *) > sizeof(int);
305 entries
= HeapAlloc(GetProcessHeap(), 0, sizeof(uninst_entry
));
307 if (!RegOpenKeyExW(HKEY_LOCAL_MACHINE
, PathUninstallW
, 0, KEY_READ
, &root
))
309 rc
|= FetchFromRootKey(root
);
313 !RegOpenKeyExW(HKEY_LOCAL_MACHINE
, PathUninstallW
, 0, KEY_READ
|KEY_WOW64_32KEY
, &root
))
315 rc
|= FetchFromRootKey(root
);
318 if (!RegOpenKeyExW(HKEY_CURRENT_USER
, PathUninstallW
, 0, KEY_READ
, &root
))
320 rc
|= FetchFromRootKey(root
);
324 qsort(entries
, numentries
, sizeof(uninst_entry
), cmp_by_name
);
328 static void UninstallProgram(void)
331 WCHAR errormsg
[1024];
334 PROCESS_INFORMATION info
;
337 for (i
=0; i
< numentries
; i
++)
339 if (!(entries
[i
].active
)) /* don't uninstall this one */
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
);
347 { /* wait for the process to exit */
348 WaitForSingleObject(info
.hProcess
, INFINITE
);
349 res
= GetExitCodeProcess(info
.hProcess
, &exit_code
);
350 WINE_TRACE("%d: %08lx\n", res
, exit_code
);
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
);
370 WINE_TRACE("finished uninstall phase.\n");