` Printed Icetips Article

Icetips Article



Par2: Check for multiple instances of a program
1998-04-22 -- Jeff Berlinghoff
 
After Global INCLUDEs:
ERROR_ALREADY_EXISTS   EQUATE(183)
SECURITY_ATTRIBUTES     GROUP,TYPE
nLength                  ULONG
lpSecurityDescriptor     ULONG
bInheritHandle           BOOL
                         END
Inside the Global Map:
 MODULE('Kernal32')
   CreateSemaphore(<*SECURITY_ATTRIBUTES lpSemaphoreAttributes>, LONG
 lInitialCount, LONG lMaximumCount, *CSTRING
lpName),LONG,RAW,PASCAL,NAME('CreateSemaphoreA')
   CloseHandle(LONG hObject),BOOL,PASCAL
   GetLastError(),ULONG,PASCAL
   FindWindow(<*CSTRING lpClassName>, *CSTRING
lpWindowName),UNSIGNED,PASCAL,RAW,NAME('FindWindowA')
   SetWindowText(UNSIGNED,*CSTRING),PASCAL,RAW,NAME('SetWindowTextA')
   IsIconic(UNSIGNED),BOOL,PASCAL
   ShowWindow(UNSIGNED, SIGNED nCmdShow),BOOL,PASCAL
   SetForegroundWindow(UNSIGNED),BOOL,PASCAL
 END
 On your FRAME, After Opening the Window:
  IF ~StartNewAppInstance(0{prop:text}) THEN RETURN.
 Create New Procedure named StartNewAppInstance
 Prototype: (STRING),SHORT
 Parameters: (WndTitle)

 Data Section:
 RetVal SHORT(True)
 cTitle  CSTRING(255)
 cTemp   CSTRING(255)
 SemName CSTRING(255)
 hSem    LONG
 hMe     LONG

 Processed Code:
  SemName = 'Semaphore Name'
  cTitle  = WndTitle

  hSem = CreateSemaphore(, 0, 1, SemName)
  IF hSem AND (GetLastError() = ERROR_ALREADY_EXISTS)
    n#=CloseHandle(hSem)
    hMe = FindWindow(, cTitle)     !rename new instance so we can find another
    cTemp='Terminating Duplicate Instance'; SetWindowText(hMe, cTemp)
    hMe = FindWindow(, cTitle)
    IF hMe
      IF IsIconic(hMe) THEN n#=ShowWindow(hMe, 1) ELSE         
n#=SetForegroundWindow(hMe).
      RetVal=False !Cannot start a new instance, use already running  instance
    END
  END



Printed November 21, 2024, 1:07 pm
This article has been viewed/printed 35185 times.