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