Only one instance of an application

Dear Friends,
           I am writing a form based application in using VB 6.0 . Now the problem I am stuck with is that I want to find out if an instance of my application is already running so that if it is running then I should be able to maximize the window of the already running application thereby allowing only one instance of my application running at a time. Can this be done ?  
adarsh_bhatAsked:
Who is Participating?
 
setiawanConnect With a Mentor Commented:
if app.previnstance = true then
      end
 end if
 
0
 
MirkwoodCommented:
If App.PrevInstance then End
0
 
caraf_gCommented:
Guys, read the question!
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
caraf_gCommented:
Create an activeX .exe

In this ActiveX .exe put one global multiuse class

This class will contain the following code

Option Explicit
Public Function NewInstance(objForm As Object) As Boolean

If gobjF Is Nothing Then
    Set gobjF = objForm
    NewInstance = True
    Exit Function
End If

NewInstance = False
gobjF.WindowState = vbMaximized

End Function
Public Sub EndInstance()

Set gobjF = Nothing

End Sub


Also, put in a normal VB module with
Option Explicit
Global gobjF As Object



Then, your VB project could contain something like this:
Option Explicit

Private Sub Form_Load()

If Not newinstance(Me) Then
    End
End If


End Sub

Private Sub Form_Terminate()

endinstance

End Sub

(Very roughly....)

0
 
VbmasterCommented:
Is the caption of your main window constant? Then you could use the FindWindow API to find the window and then use the ShowWindow API with the SW_MAXIMIZE parameter to maximize that window. Like this:



Private Const SW_MAXIMIZE = 3
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long

Private Function Maximize(av_Caption As String) As Boolean

    '---------------------------------------------------------
    ' Finds hWnd to Window w/ matching caption then Maximizes
    '---------------------------------------------------------
   
    On Error GoTo Maximze_Error

    Dim lv_hWnd As Long
    Dim lv_RC As Long
   
    lv_hWnd = FindWindow(vbNullString, av_Caption)
   
    If lv_hWnd <> 0 Then
        Call ShowWindow(lv_hWnd, SW_MAXIMIZE)
        Maximize = True
    Else
        Maximize = False
    End If
   
    Exit Function
   
Maximze_Error:
    Maximize = False

End Function


If the caption of the main window on the other hand is not constant you can perhaps save the hWnd of the main window (the one you want maximized) in the registry somewhere at startup of the app and use that value when App.PrevInstance = True.. Just remove the FindWindow API then and set lv_hWnd value to the value stored in the registry.
0
 
caraf_gCommented:
Good idea, VBmaster!
0
All Courses

From novice to tech pro — start learning today.