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 int cmp_by_name(const void *a
, const void *b
);
52 static const WCHAR BackSlashW
[] = { '\\', 0 };
53 static const WCHAR DisplayNameW
[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
54 static const WCHAR PathUninstallW
[] = {
55 'S','o','f','t','w','a','r','e','\\',
56 'M','i','c','r','o','s','o','f','t','\\',
57 'W','i','n','d','o','w','s','\\',
58 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
59 'U','n','i','n','s','t','a','l','l',0 };
60 static const WCHAR UninstallCommandlineW
[] = {'U','n','i','n','s','t','a','l','l','S','t','r','i','n','g',0};
61 static const WCHAR WindowsInstallerW
[] = {'W','i','n','d','o','w','s','I','n','s','t','a','l','l','e','r',0};
62 static const WCHAR SystemComponentW
[] = {'S','y','s','t','e','m','C','o','m','p','o','n','e','n','t',0};
65 * Used to output program list when used with --list
67 static void ListUninstallPrograms(void)
74 FetchUninstallInformation();
76 for (i
=0; i
< numentries
; i
++)
78 lenDescr
= WideCharToMultiByte(CP_UNIXCP
, 0, entries
[i
].descr
, -1, NULL
, 0, NULL
, NULL
);
79 lenKey
= WideCharToMultiByte(CP_UNIXCP
, 0, entries
[i
].key
, -1, NULL
, 0, NULL
, NULL
);
80 descr
= HeapAlloc(GetProcessHeap(), 0, lenDescr
);
81 key
= HeapAlloc(GetProcessHeap(), 0, lenKey
);
82 WideCharToMultiByte(CP_UNIXCP
, 0, entries
[i
].descr
, -1, descr
, lenDescr
, NULL
, NULL
);
83 WideCharToMultiByte(CP_UNIXCP
, 0, entries
[i
].key
, -1, key
, lenKey
, NULL
, NULL
);
84 printf("%s|||%s\n", key
, descr
);
85 HeapFree(GetProcessHeap(), 0, descr
);
86 HeapFree(GetProcessHeap(), 0, key
);
91 static void RemoveSpecificProgram(WCHAR
*nameW
)
97 FetchUninstallInformation();
99 for (i
=0; i
< numentries
; i
++)
101 if (CompareStringW(GetThreadLocale(), NORM_IGNORECASE
, entries
[i
].key
, -1, nameW
, -1) == CSTR_EQUAL
)
112 lenName
= WideCharToMultiByte(CP_UNIXCP
, 0, nameW
, -1, NULL
, 0, NULL
, NULL
);
113 name
= HeapAlloc(GetProcessHeap(), 0, lenName
);
114 WideCharToMultiByte(CP_UNIXCP
, 0, nameW
, -1, name
, lenName
, NULL
, NULL
);
115 fprintf(stderr
, "Error: could not match application [%s]\n", name
);
116 HeapFree(GetProcessHeap(), 0, name
);
121 int wmain(int argc
, WCHAR
*argv
[])
123 LPCWSTR token
= NULL
;
124 static const WCHAR listW
[] = { '-','-','l','i','s','t',0 };
125 static const WCHAR removeW
[] = { '-','-','r','e','m','o','v','e',0 };
132 /* Handle requests just to list the applications */
133 if( !lstrcmpW( token
, listW
) )
135 ListUninstallPrograms();
138 else if( !lstrcmpW( token
, removeW
) )
142 WINE_ERR( "The remove option requires a parameter.\n");
146 RemoveSpecificProgram( argv
[i
++] );
151 WINE_ERR( "unknown option %s\n",wine_dbgstr_w(token
));
156 /* Start the GUI control panel */
157 Control_RunDLL(GetDesktopWindow(), 0, "appwiz.cpl", SW_SHOW
);
163 * Used to sort entries by name.
165 static int cmp_by_name(const void *a
, const void *b
)
167 return lstrcmpiW(((const uninst_entry
*)a
)->descr
, ((const uninst_entry
*)b
)->descr
);
172 * Fetch information from the uninstall key.
174 static int FetchFromRootKey(HKEY root
)
178 DWORD sizeOfSubKeyName
, displen
, uninstlen
, value
, type
, size
;
179 WCHAR subKeyName
[256];
181 sizeOfSubKeyName
= 255;
182 for (i
=0; RegEnumKeyExW( root
, i
, subKeyName
, &sizeOfSubKeyName
, NULL
, NULL
, NULL
, NULL
) != ERROR_NO_MORE_ITEMS
; ++i
)
184 RegOpenKeyExW(root
, subKeyName
, 0, KEY_READ
, &hkeyApp
);
185 if (!RegQueryValueExW(hkeyApp
, SystemComponentW
, NULL
, &type
, (LPBYTE
)&value
, &size
) &&
186 type
== REG_DWORD
&& value
== 1)
188 RegCloseKey(hkeyApp
);
189 sizeOfSubKeyName
= 255;
192 if (!RegQueryValueExW(hkeyApp
, DisplayNameW
, NULL
, NULL
, NULL
, &displen
))
196 size
= sizeof(value
);
197 if (!RegQueryValueExW(hkeyApp
, WindowsInstallerW
, NULL
, &type
, (LPBYTE
)&value
, &size
) &&
198 type
== REG_DWORD
&& value
== 1)
200 static const WCHAR fmtW
[] = {'m','s','i','e','x','e','c',' ','/','x','%','s',0};
201 command
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(fmtW
) + lstrlenW(subKeyName
)) * sizeof(WCHAR
));
202 wsprintfW(command
, fmtW
, subKeyName
);
204 else if (!RegQueryValueExW(hkeyApp
, UninstallCommandlineW
, NULL
, NULL
, NULL
, &uninstlen
))
206 command
= HeapAlloc(GetProcessHeap(), 0, uninstlen
);
207 RegQueryValueExW(hkeyApp
, UninstallCommandlineW
, 0, 0, (LPBYTE
)command
, &uninstlen
);
211 RegCloseKey(hkeyApp
);
212 sizeOfSubKeyName
= 255;
216 entries
= HeapReAlloc(GetProcessHeap(), 0, entries
, numentries
*sizeof(uninst_entry
));
217 entries
[numentries
-1].root
= root
;
218 entries
[numentries
-1].key
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(subKeyName
)+1)*sizeof(WCHAR
));
219 lstrcpyW(entries
[numentries
-1].key
, subKeyName
);
220 entries
[numentries
-1].descr
= HeapAlloc(GetProcessHeap(), 0, displen
);
221 RegQueryValueExW(hkeyApp
, DisplayNameW
, 0, 0, (LPBYTE
)entries
[numentries
-1].descr
, &displen
);
222 entries
[numentries
-1].command
= command
;
223 entries
[numentries
-1].active
= 0;
224 WINE_TRACE("allocated entry #%d: %s (%s), %s\n",
225 numentries
, wine_dbgstr_w(entries
[numentries
-1].key
), wine_dbgstr_w(entries
[numentries
-1].descr
), wine_dbgstr_w(entries
[numentries
-1].command
));
226 if(sFilter
!= NULL
&& StrStrIW(entries
[numentries
-1].descr
,sFilter
)==NULL
)
229 RegCloseKey(hkeyApp
);
230 sizeOfSubKeyName
= 255;
236 static int FetchUninstallInformation(void)
238 static const BOOL is_64bit
= sizeof(void *) > sizeof(int);
245 entries
= HeapAlloc(GetProcessHeap(), 0, sizeof(uninst_entry
));
247 if (!RegOpenKeyExW(HKEY_LOCAL_MACHINE
, PathUninstallW
, 0, KEY_READ
, &root
))
249 rc
|= FetchFromRootKey(root
);
253 !RegOpenKeyExW(HKEY_LOCAL_MACHINE
, PathUninstallW
, 0, KEY_READ
|KEY_WOW64_32KEY
, &root
))
255 rc
|= FetchFromRootKey(root
);
258 if (!RegOpenKeyExW(HKEY_CURRENT_USER
, PathUninstallW
, 0, KEY_READ
, &root
))
260 rc
|= FetchFromRootKey(root
);
264 qsort(entries
, numentries
, sizeof(uninst_entry
), cmp_by_name
);
268 static void UninstallProgram(void)
271 WCHAR errormsg
[1024];
274 PROCESS_INFORMATION info
;
277 for (i
=0; i
< numentries
; i
++)
279 if (!(entries
[i
].active
)) /* don't uninstall this one */
281 WINE_TRACE("uninstalling %s\n", wine_dbgstr_w(entries
[i
].descr
));
282 memset(&si
, 0, sizeof(STARTUPINFOW
));
283 si
.cb
= sizeof(STARTUPINFOW
);
284 si
.wShowWindow
= SW_NORMAL
;
285 res
= CreateProcessW(NULL
, entries
[i
].command
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &info
);
287 { /* wait for the process to exit */
288 WaitForSingleObject(info
.hProcess
, INFINITE
);
289 res
= GetExitCodeProcess(info
.hProcess
, &exit_code
);
290 WINE_TRACE("%d: %08x\n", res
, exit_code
);
294 WCHAR sAppName
[MAX_STRING_LEN
];
295 WCHAR sUninstallFailed
[MAX_STRING_LEN
];
296 HINSTANCE hInst
= GetModuleHandleW(0);
298 LoadStringW(hInst
, IDS_APPNAME
, sAppName
, sizeof(sAppName
)/sizeof(WCHAR
));
299 LoadStringW(hInst
, IDS_UNINSTALLFAILED
, sUninstallFailed
, sizeof(sUninstallFailed
)/sizeof(WCHAR
));
300 wsprintfW(errormsg
, sUninstallFailed
, entries
[i
].command
);
301 if(MessageBoxW(0, errormsg
, sAppName
, MB_YESNO
| MB_ICONQUESTION
)==IDYES
)
303 /* delete the application's uninstall entry */
304 RegOpenKeyExW(entries
[i
].root
, PathUninstallW
, 0, KEY_READ
, &hkey
);
305 RegDeleteKeyW(hkey
, entries
[i
].key
);
310 WINE_TRACE("finished uninstall phase.\n");