Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Only one instance of an application

API Functions

Only one instance of an application

by  Mike Gagnon  Posted    (Edited  )
Here are two methods to do that. One using an API call and one using the Foxtools.fll library.
#1
Code:
Procedure DupStart
#DEFINE SW_NORMAL    1
#DEFINE SW_MAXIMIZE  3
#DEFINE SW_MINIMIZE  6

DECLARE Long FindWindow in user32 String, String
DECLARE Long BringWindowToTop in user32 Long
DECLARE Long ShowWindow in user32 Long, Long

Local lnHWND,lcTitle
lcTitle = _screen.Caption
_screen.Caption = Sys(3)
lnHWND = FindWindow(null,m.lcTitle)
_Screen.Caption = m.lcTitle

IF m.lnHWND >0
  BringWindowToTop(m.lnHWND)
  ShowWindow(m.lnHWND,SW_MAXIMIZE)
  qUIT
ENDIF
ENDPROC

_________________________________________________________
#2
Code:
SET LIBRARY TO SYS(2004)+'foxtools.fll' ADDITIVE
   * FindWindow() takes two parameters and returns the handle of the
   * window, HWND, if it was found.
   GetWind = RegFn('FindWindow', 'CC', 'I')
   *Set the first parameter of the call to getwind to 0, null.
   wclass=0
   winname='My Application'
   apphand=CallFn(GetWind,wclass ,winname)
   *If the call was successful, stop processing.
   IF apphand<>0
      WAIT WINDOW ;
        'You cannot start another instance of the window 'My Application'!'
      QUIT
   ENDIF
   MODIFY WINDOW screen TITLE 'My Application'
   WAIT WINDOW ;
     'The first instance of the window 'My Application' is running.'

_________________________________________________________
#3

&& This is a variant of faq184-2442. Important. If the application closes normally, use GlobalDeleteAtom to remove the atom.


Code:
#Define AtomStrLength   512
Local lcAtomName
lcAtomName = "mon application"
Declare Integer GlobalAddAtom In win32api String
Declare Integer GlobalDeleteAtom In win32api Integer
Declare Integer GlobalGetAtomName In kernel32;
     INTEGER  nAtom,;
     STRING @ lpBuffer,;
     INTEGER  nSize
findAtom(lcAtomName)
Function findAtom(tcAtom)
Create Cursor cs (atom N(12), strlen N(5), Name C(100))
Index On Allt(Name) Tag Name
For nAtom = 49152 To 65535
     lpBuffer = Repli(Chr(0), AtomStrLength)
     lnResult = GlobalGetAtomName (nAtom, @lpBuffer, AtomStrLength)

     If lnResult > 0
          Insert Into cs Values (nAtom, lnResult, Left(lpBuffer, lnResult))
     Endif
Endfor
Select cs
If Seek(tcAtom)
     Messagebox("Application is already running")
     Quit
Else
     = GlobalAddAtom(tcAtom)
Endif


Mike Gagnon
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top