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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Know that an Instance of an MyApp is Already Running? 2

Status
Not open for further replies.

Docpro777

Programmer
Joined
Jul 1, 2003
Messages
105
Location
US
An instance of MyApp.Exe can run multiple times (desireable).

How might VFP discriminate when there is another instance of MyApp running?

Thanks in advance,


Philip M. Traynor, DPM
 
try this

*:Function to kill second instance of your app
FUNCTION AppMutex
DECLARE INTEGER CreateMutex IN WIN32API INTEGER, INTEGER, STRING @
DECLARE INTEGER CloseHandle IN WIN32API INTEGER
DECLARE INTEGER GetLastError IN WIN32API
DECLARE INTEGER SetProp IN WIN32API INTEGER, STRING @, INTEGER
DECLARE INTEGER GetProp IN WIN32API INTEGER, STRING @
DECLARE INTEGER RemoveProp IN WIN32API INTEGER, STRING @
DECLARE INTEGER IsIconic IN WIN32API INTEGER
DECLARE INTEGER SetForegroundWindow IN WIN32API INTEGER
DECLARE INTEGER GetWindow IN WIN32API INTEGER, INTEGER
DECLARE INTEGER ShowWindow IN WIN32API INTEGER, INTEGER
DECLARE INTEGER GetDesktopWindow IN WIN32API
DECLARE LONG FindWindow IN WIN32API LONG, STRING
#DEFINE SW_RESTORE 9
#DEFINE ERROR_ALREADY_EXISTS 183
#DEFINE GW_HWNDNEXT 2
#DEFINE GW_CHILD 5
LOCAL llRetVal, lcExeFlag, lnExeHwnd, lnHwnd
lcExeFlag = STRTRAN(_screen.caption, " ", "") + CHR(0)
lnExeHwnd = CreateMutex(0, 1, @lcExeFlag)
IF GetLastError() = ERROR_ALREADY_EXISTS
lnHwnd = GetWindow(GetDesktopWindow(), GW_CHILD)
DO WHILE lnHwnd > 0
IF GetProp(lnHwnd, @lcExeFlag) = 1
IF IsIconic(lnHwnd) > 0
ShowWindow(lnHwnd, SW_RESTORE)
ENDIF
SetForegroundWindow(lnHwnd)
EXIT
ENDIF
lnHwnd = GetWindow(lnHwnd, GW_HWNDNEXT)
ENDDO
CloseHandle(lnExeHwnd)
llRetVal = .F.
ELSE
SetProp(FindWindow(0, _screen.caption), @lcExeFlag, 1)
llRetVal = .T.
ENDIF
RETURN llRetVal
ENDFUNC



*:put this in your main program
_SCREEN.Caption = "Your System Unique Title"
IF NOT AppMutex()
QUIT
ENDIF
 
Try this:
Code:
oManager = GETOBJECT("winmgmts:")
oApps = ;
   oManager.InstancesOf("Win32_process")
FOR EACH PROCESS IN oApps
   IF
'MyApp'
Code:
 $ UPPER(PROCESS.NAME)
      ?Process.Name
      *... do whatever else here
   ENDIF
NEXT



-Dave S.-
[cheers]
Even more Fox stuff at:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top