home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 21 / macformat_21.iso / Shareware / Programación / VideoToolbox / VideoToolboxSources / IsFileSharingOn.c < prev    next >
C/C++ Source or Header  |  1995-06-24  |  4KB  |  119 lines

  1. /*
  2. IsFileSharingOn.c
  3.  
  4.     if(IsFileSharingOn()) ...
  5.  
  6. Checks for running AppleShare server or File Sharing Extension. Returns 1 if either is running,
  7. or 0 if neither is running.
  8.  
  9. The AppleShare part is based on information in gestalt-selectors-29.etx
  10. The FileShare part assumes that the Type and Creator of the File Sharing Extension will never
  11. change. 
  12.  
  13. It would be nice to know if there is an Apple-approved way of determining the same things.
  14.  
  15.     error=KillFileSharing();
  16.  
  17. Kills the File Sharing Extension process (Apple's "personal file share" built-in to System 7), 
  18. if it's running. This won't affect AppleShare server software (which you buy separately).
  19.  
  20. SEE ALSO:
  21. ftp://sumex-aim.stanford.edu/info-mac/dev/info/gestalt-selectors-29.hqx
  22.  
  23. HISTORY:
  24. 6/21/95 dgp wrote it, after discussing it with David Brainard.
  25. 6/23/95    dhb    removed main().  Added KillFileSharing().
  26. 6/24/95 dgp modified KillFileSharing to do nothing if the FInder isn't running.
  27.             Check gestaltAppleEventsPresent.
  28. */
  29. #include "VideoToolbox.h"
  30. #include <Processes.h>
  31. #include <AppleEvents.h>
  32. enum{
  33.     gestaltASFileServerAttr= 'hgfd',
  34.     gestaltASFileServerPresent=0
  35. };
  36.  
  37. Boolean IsFileSharingOn(void)
  38. {
  39.     ProcessSerialNumber psn={kNoProcess,kNoProcess};
  40.     ProcessInfoRec infoRec;
  41.     Str31 processName;
  42.     FSSpec procSpec;
  43.     int error;
  44.     long value;
  45.  
  46.     // Is the AppleShare File Server running?
  47.     error=Gestalt(gestaltASFileServerAttr,&value);
  48.     if(!error && value&(1L<<gestaltASFileServerPresent))return 1;    // Yes
  49.     // Check all processes running on this machine.
  50.     while(1){
  51.         error=GetNextProcess(&psn);
  52.         if(error)return 0;    // No more processes to check: go home.
  53.         /* Is this process the File Sharing Extension? */
  54.         infoRec.processInfoLength=sizeof(ProcessInfoRec);
  55.         infoRec.processName=processName;
  56.         infoRec.processAppSpec=&procSpec;
  57.         GetProcessInformation(&psn,&infoRec);
  58.         if(infoRec.processSignature=='hhgg' && infoRec.processType=='INIT')return 1; // Yes.
  59.     }
  60. }
  61.  
  62. OSErr KillFileSharing(void)
  63. {
  64.     ProcessSerialNumber psn={kNoProcess,kNoProcess},fileSharingPSN;
  65.     ProcessInfoRec infoRec;
  66.     Str31 processName;
  67.     FSSpec procSpec;
  68.     AppleEvent event;
  69.     AEDesc address;
  70.     int error;
  71.     Boolean foundFinder=0,foundFileSharing=0;
  72.     long value;
  73.     
  74.     error=Gestalt(gestaltAppleEventsAttr,&value);
  75.     if(error || !(value&(1<<gestaltAppleEventsPresent)))return 1;
  76.  
  77.     // Check all processes running on this machine.
  78.     while(1){
  79.         error=GetNextProcess(&psn);
  80.         if(error)break;    // No more processes to check.
  81.         infoRec.processInfoLength=sizeof(ProcessInfoRec);
  82.         infoRec.processName=processName;
  83.         infoRec.processAppSpec=&procSpec;
  84.         GetProcessInformation(&psn,&infoRec);
  85.         
  86.         /* Is this process the Finder? */
  87.         if(infoRec.processSignature=='MACS' && infoRec.processType=='FNDR')
  88.             foundFinder=1;
  89.  
  90.         /* Is this process the File Sharing Extension? */
  91.         if(infoRec.processSignature=='hhgg' && infoRec.processType=='INIT'){
  92.             fileSharingPSN=psn;
  93.             foundFileSharing=1;
  94.         }
  95.     }
  96.     if(foundFinder && foundFileSharing){
  97.         // According to a comment by C.K.Haun in KillEveryoneButMe.c, "FileSharing must
  98.         // be killed before the Finder". I'm not clear on why. Is it bad to leave FileSharing
  99.         // running without a Finder? Or is it bad to kill FileSharing when there is no Finder?
  100.         // These different interpretations have opposite implications for what to do if we
  101.         // discover that FileSharing is running and the Finder isn't.
  102.         // Not knowing the answer, this code conservatively only tries to kill FileSharing if
  103.         // the Finder IS running.
  104.         error=AECreateDesc(typeProcessSerialNumber,(Ptr)&fileSharingPSN,sizeof(psn),&address);
  105.         if(!error){
  106.             error=AECreateAppleEvent(kCoreEventClass,kAEQuitApplication,&address
  107.                 ,kAutoGenerateReturnID,kAnyTransactionID,&event);
  108.             AEDisposeDesc(&address);
  109.             if(!error){
  110.                 AESend(&event,NULL,kAENoReply+kAEAlwaysInteract+kAECanSwitchLayer
  111.                     ,kAENormalPriority,kAEDefaultTimeout,NULL,NULL);
  112.                 AEDisposeDesc(&event);
  113.             }
  114.         }
  115.     }
  116.     return error;
  117. }
  118.  
  119.