~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/programs/uninstaller/main.c

Version: ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

  1 /*
  2  * Uninstaller
  3  *
  4  * Copyright 2000 Andreas Mohr
  5  * Copyright 2004 Hannu Valtonen
  6  * Copyright 2005 Jonathan Ernst
  7  *
  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.
 12  *
 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.
 17  *
 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
 21  *
 22  */
 23 
 24 #include <stdio.h>
 25 #include <string.h>
 26 #include <windows.h>
 27 #include <shlwapi.h>
 28 #include "resource.h"
 29 #include "regstr.h"
 30 #include "wine/debug.h"
 31 
 32 WINE_DEFAULT_DEBUG_CHANNEL(uninstaller);
 33 
 34 extern void WINAPI Control_RunDLL(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow);
 35 
 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 list_need_update = 1;
 46 static int oldsel = -1;
 47 static WCHAR *sFilter;
 48 
 49 static int FetchUninstallInformation(void);
 50 static void UninstallProgram(void);
 51 static int cmp_by_name(const void *a, const void *b);
 52 
 53 static const WCHAR BackSlashW[] = { '\\', 0 };
 54 static const WCHAR DisplayNameW[] = {'D','i','s','p','l','a','y','N','a','m','e',0};
 55 static const WCHAR PathUninstallW[] = {
 56         'S','o','f','t','w','a','r','e','\\',
 57         'M','i','c','r','o','s','o','f','t','\\',
 58         'W','i','n','d','o','w','s','\\',
 59         'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
 60         'U','n','i','n','s','t','a','l','l',0 };
 61 static const WCHAR UninstallCommandlineW[] = {'U','n','i','n','s','t','a','l','l','S','t','r','i','n','g',0};
 62 
 63 
 64 /**
 65  * Used to output program list when used with --list
 66  */
 67 static void ListUninstallPrograms(void)
 68 {
 69     unsigned int i;
 70     int lenDescr, lenKey;
 71     char *descr;
 72     char *key;
 73 
 74     FetchUninstallInformation();
 75 
 76     for (i=0; i < numentries; i++)
 77     {
 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);
 87     }
 88 }
 89 
 90 
 91 static void RemoveSpecificProgram(WCHAR *nameW)
 92 {
 93     unsigned int i;
 94     int lenName;
 95     char *name;
 96 
 97     FetchUninstallInformation();
 98 
 99     for (i=0; i < numentries; i++)
100     {
101         if (CompareStringW(GetThreadLocale(), NORM_IGNORECASE, entries[i].key, -1, nameW, -1) == CSTR_EQUAL)
102         {
103             entries[i].active++;
104             break;
105         }
106     }
107 
108     if (i < numentries)
109         UninstallProgram();
110     else
111     {
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);
117     }
118 }
119 
120 
121 int wmain(int argc, WCHAR *argv[])
122 {
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 };
126     int i = 1;
127 
128     while( i<argc )
129     {
130         token = argv[i++];
131         
132         /* Handle requests just to list the applications */
133         if( !lstrcmpW( token, listW ) )
134         {
135             ListUninstallPrograms();
136             return 0;
137         }
138         else if( !lstrcmpW( token, removeW ) )
139         {
140             if( i >= argc )
141             {
142                 WINE_ERR( "The remove option requires a parameter.\n");
143                 return 1;
144             }
145 
146             RemoveSpecificProgram( argv[i++] );
147             return 0;
148         }
149         else 
150         {
151             WINE_ERR( "unknown option %s\n",wine_dbgstr_w(token));
152             return 1;
153         }
154     }
155 
156     /* Start the GUI control panel */
157     Control_RunDLL(GetDesktopWindow(), 0, "appwiz.cpl", SW_SHOW);
158     return 1;
159 }
160 
161 
162 /**
163  * Used to sort entries by name.
164  */
165 static int cmp_by_name(const void *a, const void *b)
166 {
167     return lstrcmpiW(((const uninst_entry *)a)->descr, ((const uninst_entry *)b)->descr);
168 }
169 
170 
171 /**
172  * Fetch information from the uninstall key.
173  */
174 static int FetchFromRootKey(HKEY root)
175 {
176     HKEY hkeyUninst, hkeyApp;
177     int i;
178     DWORD sizeOfSubKeyName, displen, uninstlen;
179     WCHAR subKeyName[256];
180     WCHAR key_app[1024];
181     WCHAR *p;
182 
183     if (RegOpenKeyExW(root, PathUninstallW, 0, KEY_READ, &hkeyUninst) != ERROR_SUCCESS)
184         return 0;
185 
186     lstrcpyW(key_app, PathUninstallW);
187     lstrcatW(key_app, BackSlashW);
188     p = key_app+lstrlenW(PathUninstallW)+1;
189 
190     sizeOfSubKeyName = 255;
191     for (i=0; RegEnumKeyExW( hkeyUninst, i, subKeyName, &sizeOfSubKeyName, NULL, NULL, NULL, NULL ) != ERROR_NO_MORE_ITEMS; ++i)
192     {
193         lstrcpyW(p, subKeyName);
194         RegOpenKeyExW(root, key_app, 0, KEY_READ, &hkeyApp);
195         if ((RegQueryValueExW(hkeyApp, DisplayNameW, 0, 0, NULL, &displen) == ERROR_SUCCESS)
196          && (RegQueryValueExW(hkeyApp, UninstallCommandlineW, 0, 0, NULL, &uninstlen) == ERROR_SUCCESS))
197         {
198             numentries++;
199             entries = HeapReAlloc(GetProcessHeap(), 0, entries, numentries*sizeof(uninst_entry));
200             entries[numentries-1].root = root;
201             entries[numentries-1].key = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(subKeyName)+1)*sizeof(WCHAR));
202             lstrcpyW(entries[numentries-1].key, subKeyName);
203             entries[numentries-1].descr = HeapAlloc(GetProcessHeap(), 0, displen);
204             RegQueryValueExW(hkeyApp, DisplayNameW, 0, 0, (LPBYTE)entries[numentries-1].descr, &displen);
205             entries[numentries-1].command = HeapAlloc(GetProcessHeap(), 0, uninstlen);
206             entries[numentries-1].active = 0;
207             RegQueryValueExW(hkeyApp, UninstallCommandlineW, 0, 0, (LPBYTE)entries[numentries-1].command, &uninstlen);
208             WINE_TRACE("allocated entry #%d: %s (%s), %s\n",
209             numentries, wine_dbgstr_w(entries[numentries-1].key), wine_dbgstr_w(entries[numentries-1].descr), wine_dbgstr_w(entries[numentries-1].command));
210             if(sFilter != NULL && StrStrIW(entries[numentries-1].descr,sFilter)==NULL)
211                 numentries--;
212         }
213         RegCloseKey(hkeyApp);
214         sizeOfSubKeyName = 255;
215     }
216     RegCloseKey(hkeyUninst);
217     return 1;
218 
219 }
220 
221 static int FetchUninstallInformation(void)
222 {
223     int rc;
224 
225     numentries = 0;
226     oldsel = -1;
227     if (!entries)
228         entries = HeapAlloc(GetProcessHeap(), 0, sizeof(uninst_entry));
229 
230     rc = FetchFromRootKey(HKEY_LOCAL_MACHINE);
231     rc |= FetchFromRootKey(HKEY_CURRENT_USER);
232 
233     qsort(entries, numentries, sizeof(uninst_entry), cmp_by_name);
234     return rc;
235 }
236 
237 static void UninstallProgram(void)
238 {
239     unsigned int i;
240     WCHAR errormsg[1024];
241     BOOL res;
242     STARTUPINFOW si;
243     PROCESS_INFORMATION info;
244     DWORD exit_code;
245     HKEY hkey;
246     for (i=0; i < numentries; i++)
247     {
248         if (!(entries[i].active)) /* don't uninstall this one */
249             continue;
250         WINE_TRACE("uninstalling %s\n", wine_dbgstr_w(entries[i].descr));
251         memset(&si, 0, sizeof(STARTUPINFOW));
252         si.cb = sizeof(STARTUPINFOW);
253         si.wShowWindow = SW_NORMAL;
254         res = CreateProcessW(NULL, entries[i].command, NULL, NULL, FALSE, 0, NULL, NULL, &si, &info);
255         if (res)
256         {   /* wait for the process to exit */
257             WaitForSingleObject(info.hProcess, INFINITE);
258             res = GetExitCodeProcess(info.hProcess, &exit_code);
259             WINE_TRACE("%d: %08x\n", res, exit_code);
260         }
261         else
262         {
263             WCHAR sAppName[MAX_STRING_LEN];
264             WCHAR sUninstallFailed[MAX_STRING_LEN];
265             HINSTANCE hInst = GetModuleHandleW(0);
266 
267             LoadStringW(hInst, IDS_APPNAME, sAppName, sizeof(sAppName)/sizeof(WCHAR));
268             LoadStringW(hInst, IDS_UNINSTALLFAILED, sUninstallFailed, sizeof(sUninstallFailed)/sizeof(WCHAR));
269             wsprintfW(errormsg, sUninstallFailed, entries[i].command);
270             if(MessageBoxW(0, errormsg, sAppName, MB_YESNO | MB_ICONQUESTION)==IDYES)
271             {
272                 /* delete the application's uninstall entry */
273                 RegOpenKeyExW(entries[i].root, PathUninstallW, 0, KEY_READ, &hkey);
274                 RegDeleteKeyW(hkey, entries[i].key);
275                 RegCloseKey(hkey);
276             }
277         }
278     }
279     WINE_TRACE("finished uninstall phase.\n");
280     list_need_update = 1;
281 }
282 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.