Microsoft KB Archive/109059

= How To Prevent Multiple Instances of an Application =

Article ID: 109059

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition
 * Microsoft Visual FoxPro 5.0 Standard Edition

-



This article was previously published under Q109059



SUMMARY
The program shown below demonstrates how to stop a second instance of the same FoxPro window from being started by using the Windows application programming interface (API) function FindWindow to test if a given window name already exists. The FindWindow function can be accessed by including FOXTOOLS.FLL, the library file that is supplied with FoxPro for Windows and documented in the Foxtools.hlp file.

NOTE: This method does not prevent multiple instances of FoxPro for Windows from starting.



MORE INFORMATION
NOTE: This works in Windows for Workgroups 3.00, Windows 95, and Windows NT.

Copy this code into a program file in FoxPro for windows. The first time you call the program, you will notice the main FoxPro window title change. The second time you run it, you will get the message that you cannot start another instance of the window.

NOTE: Set the window name after you check for its existence.

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."

