Friday, December 17, 2004

Programmer's Corner - Prevent multiple instances of your application from running Source Code Sample

Programmer's Corner - Prevent multiple instances of your application from running Source Code Sample

Option Explicit

Private Declare Function ShowWindow Lib "User32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" _
(ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Private Const SW_SHOWMAXIMIZED = 3

Private Sub Form_Load()
'What: Find a previous instance of an app, and maximize it.
'Who: Eric D. Burdo, Sean Solo
'When: 6/28/2000 8:35

Dim hWnd As Long
Dim sTitle As String

If App.PrevInstance Then 'Is there a previous instance?
sTitle = Caption 'Store the current caption of this form
Caption = "Test ABC" 'Change the caption to something else
hWnd = FindWindow(0&, "Test 123") 'Find the window
If hWnd = 0 Then 'If the window is not found
Exit Sub '>>>>>>>>>>>> Something is wrong, so leave
Else 'Otherwise
AppActivate sTitle 'Activate the program
ShowWindow hWnd, SW_SHOWMAXIMIZED 'And maximize the window
End If
Unload frmMain 'Unload this form (ends the program)
End If
End Sub

0 Comments:

Post a Comment

<< Home