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

Semaphores 1

Status
Not open for further replies.

vernpace

Programmer
Feb 22, 2015
209
US
In the old days, semaphores were created for all kinds of stuff and were usually implemented by the creation of zero byte text files - if the file existed, then do this, else do that. Then, object-orientated programming came along and semaphore usage rode off into the sunset...kinda...

There are still cases where semaphores are usefull and can be easily implemented using a few Windows APIs. For example, have you ever had an EXE that you wanted to force only one instance - make it so that your customers could not create more than one instance? Here is how you do it. Put the following code at the very top of your main.prg (or whatever):

Code:
IF _VFP.StartMode = 4
   LOCAL lcSmp, hSmp
   lcSmp = "The name of your EXE without the extension"

   #DEFINE STANDARD_RIGHTS_REQUIRED 0xf0000

   DECLARE INTEGER CreateSemaphore IN kernel32.dll ;
           INTEGER lcSmAttr, INTEGER lInitialCount, INTEGER lMaximumCount, STRING lpName

   DECLARE INTEGER OpenSemaphore IN kernel32.dll ;
           INTEGER dwDesiredAccess, INTEGER bInheritHandle, STRING lpName

   hSmp = OpenSemaphore(STANDARD_RIGHTS_REQUIRED, 0, lcSmp)

   IF hSmp = 0
      hSmp = CreateSemaphore(0, 1, 1, lcSmp)

      IF hSmp = 0
         RETURN
      ENDIF
   ELSE
      RETURN
   ENDIF
ENDIF

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top