source: trunk/gsinstaller/gsProgman.cpp@ 10021

Last change on this file since 10021 was 2063, checked in by cs025, 23 years ago

Improvements towards fixing Windows 3.1 uninstallation.

  • Property svn:keywords set to Author Date Id Revision
File size: 12.3 KB
RevLine 
[1536]1#include "gsProgman.h"
2
3#include <stdio.h>
4
5// include shell for explorer-based OS'es
6#include <shlobj.h>
7// include dde for old (win 3.1/winNT 3.5) OS'es
8#include <ddeml.h>
9
10#include "FilePath.h"
11
12extern HINSTANCE app_instance;
13
14/**
15 * A private function for Dde callbacks; in practice it needs to be here,
16 * but does nothing whatsoever
17 */
[1543]18HDDEDATA CALLBACK DdeCallBack(UINT uType, // transaction type
19 UINT uFmt, // clipboard data format
20 HCONV hconv, // handle to the conversation
21 HSZ hsz1, // handle to a string
22 HSZ hsz2, // handle to a string
23 HDDEDATA hdata, // handle to a global memory object
24 DWORD dwData1, // transaction-specific data
25 DWORD dwData2 // transaction-specific data
26 )
27{
28 return NULL;
[1536]29}
30
31 /**
32 * A private little function to do DDE activity
33 */
34bool DDEShellCommand(DWORD instance, char *command)
[1543]35{
36 HSZ serviceName;
37 HCONV conversation;
38 int commandLength;
39 HDDEDATA ddeData;
40 DWORD result;
41 bool reply = false;
[1536]42
[1543]43 // create DDE string from service request
44 serviceName = DdeCreateStringHandle(instance, "PROGMAN", CP_WINANSI);
45 if (serviceName == NULL)
46 {
47 /*char buffer[20];
[1536]48 sprintf(buffer, "%d %d", DdeGetLastError(instance), instance);
[1543]49 MessageBox(0, buffer, "GSInstall", MB_OK);*/
50 return FALSE;
51 }
[1536]52
53 // strike up a conversation with the DDE service
54 if ((conversation = DdeConnect(instance, serviceName, serviceName, NULL)) != NULL)
[1543]55 {
56 commandLength = lstrlen(command);
[1536]57
[1543]58 // send the message via DDE
59 // MessageBox(0, command, "Executing", MB_OK);
60 ddeData = DdeClientTransaction((LPBYTE) command, commandLength + 1,
61 conversation, NULL, CF_TEXT, XTYP_EXECUTE,
62 1000, &result);
63 // if our reply isn't NULL, it succeeded
64 reply = (ddeData != NULL);
65 if (reply == false)
66 { /*
67 char buffer[20];
68
69 sprintf(buffer, "%d", DdeGetLastError(instance));
70 MessageBox(0, buffer, "DDE Error", MB_OK);*/
71 }
72
73 // disconnect the conversation
74 DdeDisconnect(conversation);
[1536]75 }
76 else
[1543]77 {
78 MessageBox(0, "DDE Connect failed", "GSInstall", MB_OK);
79 }
80
[1536]81 // free DDE string
82 DdeFreeStringHandle(instance, serviceName);
83 return reply;
84}
85
86gsProgramManager::gsProgramManager(installManager &manager)
[1543]87 : installAgent(manager)
88{
89 this->platform = new gsPlatform();
90 this->connected = false;
[1536]91}
92
93void gsProgramManager::logAction(string actionName, string group)
[1543]94{
95 unInstallCommand command(actionName);
96 command.addParameter(group);
97 manager->storeCommand(command);
[1536]98}
99
100void gsProgramManager::logAction(string actionName, string group, string item, string parameter)
[1543]101{
102 unInstallCommand command(actionName);
[1536]103 command.addParameter(group);
[1543]104 command.addParameter(item);
[1536]105 command.addParameter(parameter);
106 manager->storeCommand(command);
107}
108
109bool gsProgramManager::undoAction(string actionName, stringArray &params)
[1543]110{
111 if (actionName == "ProgManCreateGroup")
112 {
113 this->removeProgramGroup(params[0]);
114 return true;
115 }
[1536]116 else if (actionName == "ProgManAddItem")
[1543]117 {
118 this->removeIcon(params[0], params[1]);
119 return true;
120 }
[1536]121 return false;
122}
123
124bool gsProgramManager::connect()
[1543]125{
126 HRESULT response;
127
128 if (this->platform->isExplorerShell())
129 {
130 // Initialise the shell connection
131 response = CoInitialize(NULL);
132 if (response < 0)
133 {
134 return false;
135 }
[1536]136 }
137 else
[1543]138 {
139 FARPROC procPtr;
140
141 procPtr = MakeProcInstance((FARPROC) DdeCallBack, app_instance);
142
143 instance = 0L;
144 if (DdeInitialize(&instance, (PFNCALLBACK) procPtr, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
145 {
146 MessageBox(0, "DDE failed to initialise", "GSInstall", MB_OK);
147 return false;
148 }
[1536]149 }
[1543]150 this->connected = true;
[1536]151 return true;
152}
153
154bool gsProgramManager::disconnect()
[1543]155{
156 if (this->connected)
157 {
158 if (this->platform->isExplorerShell())
159 {
160 CoUninitialize();
161 }
162 else
163 {
164 DdeUninitialize(instance);
165 }
[1536]166 }
[1543]167 this->connected = false;
[1536]168 return true;
169}
170
171bool gsProgramManager::addProgramGroup(string groupName)
[1543]172{
173 //CreateGroup(groupName);
174 char buffer[256];
[1536]175
176 if (this->platform->isExplorerShell())
[1580]177 {
178 // add using shell controls
179 LPITEMIDLIST pidl;
180
[1672]181 if (this->platform->isWindowsNT() && this->platform->isUserAdministrator())
[1580]182 {
[1547]183#if defined (__GNUC__)
184 // this is a hack as windows port of gcc doesn't have CSIDL_COMMON_PROGRAMS defined
[1580]185 if (SHGetSpecialFolderLocation(0, CSIDL_PROGRAMS, &pidl) != NOERROR)
[1547]186#else
[1580]187 if (SHGetSpecialFolderLocation(0, CSIDL_COMMON_PROGRAMS, &pidl) != NOERROR)
[1547]188#endif
[1580]189 {
[1543]190 return false;
191 }
[1580]192 }
193 else
[1672]194 { if (SHGetSpecialFolderLocation(0, CSIDL_PROGRAMS, &pidl) != NOERROR)
[1543]195 {
196 return false;
197 }
[1580]198 }
199 SHGetPathFromIDList(pidl, buffer);
[1536]200
[1580]201 // Create the folder as required
202 FilePath *path = new FilePath(buffer, groupName);
203 if (!CreateDirectory((char *) path->cString(), NULL))
204 {
205 // it probably existed already - if so skip it!
206 DWORD reason = GetLastError();
207 if (reason == ERROR_ALREADY_EXISTS)
208 { return true;
209 }
210 else
211 { return false;
212 }
213 }
214 delete path;
215 }
[1536]216 else
[1543]217 {
218 // add using program manager DDE
219 sprintf(buffer, "[CreateGroup (%s)]", groupName.c_str());
220 if (!DDEShellCommand(this->instance, buffer))
221 {
222 // flag error by returning false
[2063]223 MessageBox(0, "Couldn't create group", "Greenstone Installer", MB_OK);
[1543]224 return false;
225 }
[1536]226 }
227
228 this->logAction("ProgManCreateGroup", groupName);
[1543]229 return true;
[1536]230}
231
[1543]232bool gsProgramManager::addIcon(string groupName, // group of the icon
233 string iconName, // name of the icon
234 string iconDestination, // destination file of the icon
[1657]235 string arguments, // any arguments to go along with iconDestination
[1543]236 string description) // the textual description
[1657]237 // of the icon (not usually displayed)
[1543]238{
239 bool reply = false;
240 char buffer[MAX_PATH];
241
[1536]242 if (this->platform->isExplorerShell())
[1543]243 {
244 // create item using the shell
245 LPITEMIDLIST pidl;
246 HRESULT hres;
247 IShellLink* psl;
248 FilePath *fullPath;
[1536]249
[1878]250 if (this->platform->isWindowsNT() && this->platform->isUserAdministrator())
[1543]251 {
[1547]252#if defined (__GNUC__)
253 // this is a hack as windows port of gcc doesn't have CSIDL_COMMON_PROGRAMS defined
254 if (SHGetSpecialFolderLocation(0, CSIDL_PROGRAMS, &pidl) != NOERROR)
255#else
[1543]256 if (SHGetSpecialFolderLocation(0, CSIDL_COMMON_PROGRAMS, &pidl) != NOERROR)
[1547]257#endif
258
[1543]259 {
260 return false;
261 }
262 }
263 else
264 {
265 if (SHGetSpecialFolderLocation(0, CSIDL_PROGRAMS, &pidl) != NOERROR)
266 {
267 return false;
268 }
269 }
[1536]270
[1543]271 SHGetPathFromIDList(pidl, buffer);
272 fullPath = new FilePath(4, buffer, groupName.c_str(), iconName.c_str(), "!.lnk");
273
274 // check if the shortcut already exists then don't create this again
275 if (fullPath->exists())
276 {
277 delete fullPath;
278 return true;
279 }
280
281 // Get a pointer to the IShellLink interface.
282 hres = CoCreateInstance(CLSID_ShellLink, NULL,
283 CLSCTX_INPROC_SERVER, IID_IShellLink, (void **) &psl);
284 if (SUCCEEDED(hres))
285 {
286 IPersistFile* ppf;
[1536]287
[1543]288 // Set the path to the shortcut target, and add the
289 // description.
[1536]290 psl->SetPath(iconDestination.c_str());
[1657]291 psl->SetArguments(arguments.c_str());
[1543]292 psl->SetDescription(description.c_str());
293
294 // Query IShellLink for the IPersistFile interface for saving the
[1536]295 // shortcut in persistent storage.
[1543]296 hres = psl->QueryInterface(IID_IPersistFile, (void **) &ppf);
297
298 if (SUCCEEDED(hres))
299 {
300 WCHAR wsz[MAX_PATH];
301
302 // Ensure that the string is ANSI.
303 MultiByteToWideChar(CP_ACP, 0, fullPath->cString(), -1, wsz, MAX_PATH);
[1536]304
305 // store the link in the "persistent" file
[1580]306 reply = (ppf->Save(wsz, TRUE) != E_FAIL);
307
308 // release the persistent file handle
309 ppf->Release();
[1536]310 }
311 psl->Release();
[1543]312 }
313 delete fullPath;
314 }
[1536]315 else
[1580]316 {
[1543]317 // Back to the dark ages with win 3.1!
318
319 // ensure that the requisite group is active; the add icon command will
320 // use the active group.
321 sprintf(buffer, "[ShowGroup(%s,5)]", groupName.c_str());
322 if (!DDEShellCommand(this->instance, buffer))
323 {
324 // TODO: articulate/determine possible errors; expand error handling
325 return false;
326 }
[2063]327
328 // if the item is there already, mark it for replacement; this may fail,
329 // so we don't test for success
330 sprintf(buffer, "[ReplaceItem(%s)]", iconName.c_str());
331 DDEShellCommand(this->instance, buffer);
332
[1543]333 // and now add the item itself to the active (given) group
334 sprintf(buffer, "[AddItem(%s,%s)]", iconDestination.c_str(), iconName.c_str());
335 if (!DDEShellCommand(this->instance, buffer))
336 {
337 // TODO: again work out what the problems could be
[2063]338 MessageBox(0, "Can't create item", "Greenstone Installer", MB_OK);
[1543]339 return false;
340 }
341 reply = true;
[1536]342 }
343
[1543]344 if (reply)
345 {
346 this->logAction("ProgManAddItem", groupName, iconName, iconDestination);
[1536]347 }
348 return reply;
349}
350
351bool gsProgramManager::removeProgramGroup(string groupName)
[1543]352{
353 //CreateGroup(groupName);
354 char buffer[256];
355
[1536]356 if (this->platform->isExplorerShell())
[1543]357 {
358 // add using shell controls
359 LPITEMIDLIST pidl;
360
[1878]361 if (this->platform->isWindowsNT() && this->platform->isUserAdministrator())
[1543]362 {
[1547]363#if defined (__GNUC__)
364 // this is a hack as windows port of gcc doesn't have CSIDL_COMMON_PROGRAMS defined
365 if (SHGetSpecialFolderLocation(0, CSIDL_PROGRAMS, &pidl) != NOERROR)
366#else
[1543]367 if (SHGetSpecialFolderLocation(0, CSIDL_COMMON_PROGRAMS, &pidl) != NOERROR)
[1547]368#endif
[1543]369 {
370 return false;
371 }
372 }
373 else
374 {
375 if (SHGetSpecialFolderLocation(0, CSIDL_PROGRAMS, &pidl) != NOERROR)
376 {
377 return false;
378 }
379 }
380 SHGetPathFromIDList(pidl, buffer);
381
382 // Create the folder as required
383 FilePath *path = new FilePath(buffer, groupName);
384 if (!RemoveDirectory((char *) path->cString()))
385 {
386 // TODO: find cause of failure to remove
387 DWORD reason = GetLastError();
388 if (reason != 0)
389 {
390 return false;
391 }
392 }
[1541]393 }
[1536]394 else
[1543]395 {
396 // add using program manager DDE
397 sprintf(buffer, "[DeleteGroup (%s)]", groupName.c_str());
398 if (!DDEShellCommand(this->instance, buffer))
399 {
400 // flag error by returning false
401 return false;
402 }
[1536]403 }
[1543]404 return true;
[1536]405}
406
[1543]407bool gsProgramManager::removeIcon(string groupName, // group of the icon
408 string iconName) // name of the icon
409{
410 bool reply = false;
411 char buffer[MAX_PATH];
[1536]412
413 if (this->platform->isExplorerShell())
[1580]414 {
415 // create item using the shell
416 LPITEMIDLIST pidl;
417 // HRESULT hres;
418 // IShellLink * psl;
419 FilePath * fullPath;
[1536]420
[1580]421 // if the user is adminstrator, take the folder from the common programs folder
422 // (in the "All Users" profile)
[1878]423 if (this->platform->isWindowsNT() && this->platform->isUserAdministrator())
[1580]424 {
[1547]425#if defined (__GNUC__)
[1580]426 // this is a hack as windows port of gcc doesn't have CSIDL_COMMON_PROGRAMS defined
427 if (SHGetSpecialFolderLocation(0, CSIDL_PROGRAMS, &pidl) != NOERROR)
[1547]428#else
[1580]429 if (SHGetSpecialFolderLocation(0, CSIDL_COMMON_PROGRAMS, &pidl) != NOERROR)
[1547]430#endif
[1543]431 {
432 return false;
433 }
[1580]434 }
435 // otherwise get it from the user's own programs folder in their profile
436 else
437 {
438 if (SHGetSpecialFolderLocation(0, CSIDL_PROGRAMS, &pidl) != NOERROR)
439 {
[1543]440 return false;
441 }
[1580]442 }
[1536]443
[1580]444 // get path of folder from shell
445 SHGetPathFromIDList(pidl, buffer);
446
447 // delete file
448 fullPath = new FilePath(4, buffer, groupName.c_str(), iconName.c_str(), "!.lnk");
449 if (!DeleteFile(fullPath->cString()))
450 { reply = false;
451 }
452 delete fullPath;
453 }
[1536]454 else
[1580]455 {
456 // Back to the dark ages with win 3.1!
[1536]457
[1580]458 // ensure that the requisite group is active; the add icon command will
459 // use the active group.
460 sprintf(buffer, "[ShowGroup(%s,5)]", groupName.c_str());
461 if (!DDEShellCommand(this->instance, buffer))
462 {
463 // TODO: articulate/determine possible errors; expand error handling
464 return false;
465 }
[1536]466
[1580]467 // and now add the item itself to the active (given) group
468 sprintf(buffer, "[DeleteItem(%s)]", iconName.c_str());
469 if (!DDEShellCommand(this->instance, buffer))
470 {
471 // TODO: again work out what the problems could be
472 return false;
473 }
474 }
[1536]475 return reply;
476}
Note: See TracBrowser for help on using the repository browser.