home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / utility / UProcessUtils.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  4.8 KB  |  173 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. //    UProcessUtils.cp
  20.  
  21.  
  22. #ifdef PowerPlant_PCH
  23. #include PowerPlant_PCH
  24. #endif
  25.  
  26. #include <LString.h>
  27. #include "UProcessUtils.h"
  28.  
  29. #ifndef _STRING
  30. #include <string.h>
  31. #endif
  32.  
  33. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  34. //    Ñ    LocateApplication
  35. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  36.  
  37. Boolean UProcessUtils::LocateApplication(OSType inSignature, FSSpec &outSpec)
  38. {
  39.     Boolean bAppFound = false;
  40.     FSSpec theMostRecentSpec;
  41.     Uint32 theMostRecentDate = 0;
  42.     TString<Str63> theAppName;
  43.     Int16 theIndex = 0;
  44.     
  45.     HParamBlockRec hpb;
  46.  
  47.     do {
  48.         DTPBRec dpb;
  49.         CInfoPBRec cpb;
  50.  
  51.         memset(&hpb, 0, sizeof(HParamBlockRec));
  52.         memset(&dpb, 0, sizeof(DTPBRec));
  53.  
  54.         hpb.volumeParam.ioVolIndex = ++theIndex;
  55.         if (PBHGetVInfoSync(&hpb) != noErr)
  56.             break;
  57.  
  58.         dpb.ioVRefNum = hpb.volumeParam.ioVRefNum;
  59.         if (PBDTGetPath(&dpb) == noErr)
  60.             {
  61.             dpb.ioFileCreator = inSignature;
  62.             dpb.ioNamePtr = theAppName;
  63.  
  64.             if (PBDTGetAPPLSync(&dpb) == noErr)
  65.                 {
  66.                 memset(&cpb, 0, sizeof(CInfoPBRec));
  67.                 cpb.hFileInfo.ioNamePtr = theAppName;
  68.                 cpb.hFileInfo.ioVRefNum = dpb.ioVRefNum;
  69.                 cpb.hFileInfo.ioDirID = dpb.ioAPPLParID;
  70.  
  71.                 if ((PBGetCatInfoSync(&cpb) == noErr) && (cpb.hFileInfo.ioFlMdDat > theMostRecentDate))
  72.                     {
  73.                     theMostRecentSpec.vRefNum = dpb.ioVRefNum;
  74.                     theMostRecentSpec.parID = dpb.ioAPPLParID;
  75.                     ::BlockMoveData(&theAppName[0], theMostRecentSpec.name, theAppName.Length() + 1);
  76.                     theMostRecentDate = cpb.hFileInfo.ioFlMdDat;
  77.                     }
  78.                     
  79.                 bAppFound = true;
  80.                 }
  81.             }
  82.         }
  83.     while (!hpb.volumeParam.ioResult);
  84.  
  85.     if (bAppFound)
  86.         outSpec = theMostRecentSpec;
  87.  
  88.     return bAppFound;
  89. }
  90.  
  91. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  92. //    Ñ    ApplicationRunning
  93. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  94.  
  95. Boolean UProcessUtils::ApplicationRunning(
  96.     OSType                 inSignature,
  97.     ProcessSerialNumber &outPSN)
  98. {
  99.     Boolean bRunning = false;
  100.     //    spin through the process list to determine whether there's a
  101.     //    process with our desired signature.
  102.     outPSN.highLongOfPSN = 0;
  103.     outPSN.lowLongOfPSN = 0;
  104.  
  105.     ProcessInfoRec info;    //    information from the process list.
  106.     info.processInfoLength = sizeof(ProcessInfoRec);
  107.     info.processName = nil;
  108.     info.processAppSpec = nil;
  109.  
  110.     while ((GetNextProcess(&outPSN) == noErr) && (GetProcessInformation(&outPSN, &info) == noErr))
  111.         {
  112.         if (info.processSignature == inSignature)
  113.             {
  114.             bRunning = true;
  115.             break;
  116.             }
  117.         }
  118.  
  119.     return bRunning;
  120. }
  121.  
  122. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  123. //    Ñ    LaunchApplication
  124. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  125.  
  126. OSErr UProcessUtils::LaunchApplication(
  127.     FSSpec&                 inSpec,
  128.     Uint16                    inLaunchFlags,
  129.     ProcessSerialNumber&     outPSN)
  130. {
  131.     LaunchParamBlockRec lpb;
  132.  
  133.     lpb.launchBlockID = extendedBlock;
  134.     lpb.launchEPBLength = extendedBlockLen;
  135.     lpb.launchFileFlags = 0;
  136.     lpb.launchControlFlags = inLaunchFlags;
  137.     lpb.launchAppSpec = &inSpec;
  138.     lpb.launchAppParameters = 0L;
  139.  
  140.     OSErr theResult = ::LaunchApplication(&lpb);
  141.     outPSN = lpb.launchProcessSN;
  142.     return theResult;
  143. }
  144.  
  145. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  146. //    Ñ    PullAppToFront
  147. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  148.  
  149. void UProcessUtils::PullAppToFront(const ProcessSerialNumber& inWhichProcess)
  150. {
  151.     EventRecord scratch;
  152.  
  153.     //    SetFrontProcess(), then call EventAvail so that it actually happens.
  154.     ::SetFrontProcess(&inWhichProcess);
  155.     ::EventAvail(nullEvent, &scratch);
  156. }
  157.  
  158. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  159. //    Ñ    IsFrontProcess
  160. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  161.  
  162. Boolean UProcessUtils::IsFrontProcess(const ProcessSerialNumber& inWhichProcess)
  163. {
  164.     ProcessSerialNumber theFrontProcess;
  165.     ::GetFrontProcess(&theFrontProcess);
  166.  
  167.     Boolean isInFront;
  168.     ::SameProcess(&theFrontProcess, &inWhichProcess, &isInFront);
  169.  
  170.     return isInFront;
  171. }
  172.  
  173.