Run only one instance of your application: Difference between revisions

Updated "createmutex succeeded" test to test for NULL not ERROR_INVALID_HANDLE as per Frank Cheng's recommendation see https://support.dataaccess.com/Forums/showthread.php?67791-Limiting-Dataflex-Instances-to-1-per-Laptop&p=368346#post368346
No edit summary
(Updated "createmutex succeeded" test to test for NULL not ERROR_INVALID_HANDLE as per Frank Cheng's recommendation see https://support.dataaccess.com/Forums/showthread.php?67791-Limiting-Dataflex-Instances-to-1-per-Laptop&p=368346#post368346)
 
(7 intermediate revisions by 3 users not shown)
Line 3: Line 3:
# Using mutexes
# Using mutexes
# Using FindWindow WinAPI
# Using FindWindow WinAPI
# Using Atoms


=== Using mutexes ===
=== Using mutexes ===
Line 11: Line 12:


In the SRC file within the panel object put this piece of code:
In the SRC file within the panel object put this piece of code:
<pre>
<source lang="dataflex">
Procedure Exit_Application
Procedure Exit_Application
     integer iVoid
     integer iVoid
Line 18: Line 19:
     Forward Send Exit_Application
     Forward Send Exit_Application
End_Procedure
End_Procedure
</pre>
</source>
 
And in a package that gets hit on startup:
And in a package that gets hit on startup:
<pre>
<source lang="dataflex">
// Constants
// Constants
Define ERROR_INVALID_HANDLE        for 6    //  taken from error.h of VS7
Define ERROR_INVALID_HANDLE        for 6    //  taken from error.h of VS7
Define ERROR_ALREADY_EXISTS        for 183<br/>
Define ERROR_ALREADY_EXISTS        for 183
Handle ghMuteX<br/>
 
Handle ghMuteX
 
// external functions
// external functions
#IFNDEF Get_CreateMuteX
#IFNDEF Get_CreateMuteX
Line 31: Line 35:
#IFNDEF Get_CloseHandle
#IFNDEF Get_CloseHandle
   External_Function CloseHandle "CloseHandle" Kernel32.dll Integer i1 Returns Integer
   External_Function CloseHandle "CloseHandle" Kernel32.dll Integer i1 Returns Integer
#ENDIF<br/>
#ENDIF
 
// function to create a mutex
// function to create a mutex
Procedure Create_MuteX_Object
Procedure Create_MuteX_Object
Line 38: Line 43:
   Move "Unique String For My Application" To sID
   Move "Unique String For My Application" To sID
   Move (CreateMuteX(0,1,AddressOf(sID))) To ghMuteX
   Move (CreateMuteX(0,1,AddressOf(sID))) To ghMuteX
   If (ghMuteX <> ERROR_INVALID_HANDLE) Begin
   If (ghMuteX <> 0) Begin
       Move (GetLastError()) To iErr
       Move (GetLastError()) To iErr
       If (iErr = ERROR_ALREADY_EXISTS) Begin  // program is already  
       If (iErr = ERROR_ALREADY_EXISTS) Begin  // program is already  
Line 51: Line 56:
   End
   End
   Else Send None  // rare error; object could not be created
   Else Send None  // rare error; object could not be created
End_Procedure  // Create_MuteX_Object<br/>
End_Procedure  // Create_MuteX_Object
 
// create the mutex
// create the mutex
Send Create_MuteX_Object
Send Create_MuteX_Object
</pre>
</source>
 
Dave Robinson says -
A warning: We found CodeJock skinning broke this, though YMMV.


=== Using FindWindow WinAPI ===
=== Using FindWindow WinAPI ===
Line 61: Line 70:


Usage in SRC:
Usage in SRC:
<pre>
<source lang="dataflex">
  Use myPanel,pkg
  Use myPanel,pkg
  Object Main is a myPanel
  Object Main is a myPanel
</pre>
</source>


Source myPanel.pkg
Source myPanel.pkg
<pre>
<source lang="dataflex">
  ////////////////////////////////////////////
  ////////////////////////////////////////////
  //
  //
Line 104: Line 113:
       End_Procedure // End_Construct_Object
       End_Procedure // End_Construct_Object
  End_class
  End_class
</pre>
</source>
 
The FindWindow technique is also discussed in the book [http://starzen.com/products/books-and-magazines/mastering-visual-dataflex/ Mastering Visual DataFlex] from Starzen.
 
Dave Robinson says:
The FindWindow method depends on the Title in the program taskbar being known and consistent.


The FindWindow technique is also discussed in the book [http://www.starzen.com/Products/DataFlexVDF/Books/tabid/56/Default.aspx Mastering Visual DataFlex] from Starzen.


[[Category: Tutorials]]
[[Category: Tutorials]]