home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 21 / macformat_21.iso / Shareware / Programación / VideoToolbox / VideoToolboxSources / KillEveryoneButMe.c < prev    next >
Text File  |  1995-06-22  |  4KB  |  122 lines

  1. /*
  2. KillEveryoneButMe.c
  3.  
  4. Quits every other application on your machine, including the Finder (and FileSharing). 
  5. Requires at least System 7, otherwise returns 1 and does nothing.
  6. This may be appropriate for an application that will hog the entire machine anyway,
  7. in order to maximize the available memory and minimize the time lost to other 
  8. processes.
  9.  
  10. "Some folks want to kill all the other applications running on a machine for
  11. demos, school situations, kiosks, etc. With System 7 it's easy. Just use the
  12. Process Manager and AppleEvents. This thing shows you how.
  13.  
  14. "PLEASE don't abuse this.  System 7 gives the user full-time multiFinder, and they
  15. like it, our System Software team worked very hard to make this happen.  ONLY do
  16. this in very special circumstances, or you're taking power away from the user and
  17. tweaking the strength of the Mac.
  18.  
  19. "Written by C.K. Haun <TR>, Apple Developer Tech Support
  20.  
  21. "BUG FIX, if only this app and the Finder were running, it didn't do anything. Oops
  22.  
  23. "Of course, Copyright 1991-1992, Apple Computer Inc."
  24.  
  25. C.K.HAUN@applelink.apple.com
  26.  
  27. NOTE:
  28. It is essential that the isHighLevelEventAware bit be set in your application's
  29. SIZE resource, otherwise all your attempts to emit AppleEvents will be ignored.
  30. In CodeWarrior, use the Edit:Preferences menu item to set the Project:SIZE Flags.
  31. In THINK C, use the menu item Project:SetProjectType:SIZE Flags.
  32.  
  33. CAUTION:
  34. This routine has not been tested.
  35.  
  36. SEE ALSO:
  37. VideoToolbox:Demos:Quitter.c
  38.  
  39. ACKNOWLEDGEMENT:
  40. Peter Lennie gave me a similar routine, for inclusion in the VideoToolbox.
  41.  
  42. HISTORY:
  43. 6/6/95 dgp extracted from Apple develop Bookmark CD snippets folder, and edited
  44.     to make it more readable and shorter. Corrected minor bugs in the disposing
  45.     of resources in the event of error.
  46. */
  47. #include "VideoToolbox.h"
  48. #include <AppleEvents.h>
  49.  
  50. OSErr KillEveryoneButMe(void)
  51. {
  52.     ProcessSerialNumber psn={kNoProcess,kNoProcess},ourPSN,finderPSN={kNoProcess,kNoProcess};
  53.     ProcessInfoRec infoRec;
  54.     Str31 processName;
  55.     FSSpec procSpec;
  56.     OSErr error;
  57.     AppleEvent event;
  58.     AEDesc address;
  59.     Boolean isOk,isUs,isFinder,foundFinder=0;
  60.     long value;
  61.     
  62.     error=Gestalt(gestaltAppleEventsAttr,&value);
  63.     if(error || !(value&(1<<gestaltAppleEventsPresent)))return 1;
  64.     GetCurrentProcess(&ourPSN);
  65.     do{
  66.         error=GetNextProcess(&psn);
  67.         switch(error){
  68.         case noErr:
  69.             isOk=1;
  70.             /* Is it us? */
  71.             SameProcess(&ourPSN,&psn,&isUs);
  72.             /* Is it the Finder? */
  73.             infoRec.processInfoLength=sizeof(ProcessInfoRec);
  74.             infoRec.processName=processName;
  75.             infoRec.processAppSpec=&procSpec;
  76.             GetProcessInformation(&psn,&infoRec);
  77.             isFinder=infoRec.processSignature=='MACS' && infoRec.processType=='FNDR';
  78.             if(isFinder){
  79.                 finderPSN=psn;
  80.                 foundFinder=1;
  81.             }
  82.             break;
  83.         case procNotFound:
  84.             isOk=0;
  85.             psn=finderPSN;
  86.             break;
  87.         default:
  88.             return error;
  89.         }
  90.         /*
  91.         Kill the Finder last of all.
  92.         Otherwise non-AppleEvent-aware apps won't get killed because the Finder
  93.         won't be there to convert the AppleEvent to puppet strings.
  94.         And FileShare must be killed before the Finder.
  95.         */
  96.         if(isOk && !isUs && !isFinder || !isOk && foundFinder){
  97.             error=AECreateDesc(typeProcessSerialNumber,(Ptr)&psn,sizeof(psn),&address);
  98.             if(!error){
  99.                 error=AECreateAppleEvent(kCoreEventClass,kAEQuitApplication,&address
  100.                     ,kAutoGenerateReturnID,kAnyTransactionID,&event);
  101.                 AEDisposeDesc(&address);
  102.                 /*
  103.                 The Finder will convert the AppleEvent to puppetstrings if
  104.                 the application is not AppleEvent-aware. But this only
  105.                 happens for the 4 required (oapp,odoc,pdoc,and quit) AppleEvents
  106.                 and only if you use the PSN for the address.
  107.                 */
  108.                 if(!error){
  109.                     AESend(&event,NULL,kAENoReply+kAEAlwaysInteract+kAECanSwitchLayer
  110.                         ,kAENormalPriority,kAEDefaultTimeout,NULL,NULL);
  111.                     AEDisposeDesc(&event);
  112.                 }
  113.             }
  114.             if(error)return error;
  115.         }
  116.     }while(isOk);
  117.     return 0;
  118. }
  119.  
  120.  
  121.  
  122.