Consulting

Results 1 to 5 of 5

Thread: Solved: VB6: Only 1 program instance

  1. #1
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location

    Solved: VB6: Only 1 program instance

    This is probably fairly simple... using VB6 how do you code to allow only 1 instance of a .exe @ a time. I'm assuming that you would place the code in the form load code to prevent the form from loading if another instance is already running. If anyone has a suggestion or a link I would appreciate the assistance. Dave

  2. #2
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    This is rather harsh, but works real good.
    [VBA]If App.PrevInstance = True Then
    MsgBox "Warning !!! A previous Instance of ThisExE is already Running. This Instance will be Terminated!!"
    End
    End If[/VBA]

  3. #3
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location
    Here's an informative link...
    http://vbnet.mvps.org/index.html?cod...pprocesses.htm

    Tommy I haven't tried that yet but I'm sure it's easier than the following code which I won't pretend to understand the guts of. Crazy lack of syntax thing created a workaround for me. Thought I might as well post it as I was fairly pleased with accomplishing this days' project. It seems your code just issues a warning without shutting the application off ie. how to allow only 1 instance from there? Thanks for your help and a big thanks to the coder on the link. Dave

    VB6 form code...

    [vba]
    Private Sub Form_Load()
    'this is VB6 form code
    'TmpStr = Program name as shown in program folder...
    ' Ucase/lcase/spacing MUST be correct for TmpStr
    ' TmpLen = Program name char length eg. "BaRt" = 4.
    If TwoRun("BaRt", 4) Then
    Unload Form1
    Exit Sub
    End If
    'do load stuff here
    End Sub

    [/vba]

    VB6 Module Code...
    [vba]
    Public Const TH32CS_SNAPPROCESS As Long = 2&
    Public Const MAX_PATH As Long = 260
    Public Type PROCESSENTRY32
    dwSize As Long
    cntUsage As Long
    th32ProcessID As Long
    th32DefaultHeapID As Long
    th32ModuleID As Long
    cntThreads As Long
    th32ParentProcessID As Long
    pcPriClassBase As Long
    dwFlags As Long
    szExeFile As String * MAX_PATH
    End Type

    Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" _
    (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
    Public Declare Function ProcessFirst Lib "kernel32" _
    Alias "Process32First" _
    (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
    Public Declare Function ProcessNext Lib "kernel32" _
    Alias "Process32Next" _
    (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
    Public Declare Sub CloseHandle Lib "kernel32" _
    (ByVal hPass As Long)



    Public Function TwoRun(TmpStr As String, TmpLen As Integer) As Boolean
    ' use with form load
    ' allows only 1 program instance
    ' TmpStr = Program name as shown in program folder...
    ' Ucase/lcase/spacing MUST be correct for TmpStr
    ' TmpLen = Program name char length eg. "BaRt" = 4.

    Dim hSnapShot As Long
    Dim uProcess As PROCESSENTRY32
    Dim success As Long
    Dim AppCnt As Integer
    hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
    If hSnapShot = -1 Then Exit Function
    uProcess.dwSize = Len(uProcess)
    success = ProcessFirst(hSnapShot, uProcess)
    If success = 1 Then
    TwoRun = False
    Do
    If Left(uProcess.szExeFile, TmpLen) = TmpStr Then
    AppCnt = AppCnt + 1
    End If
    If AppCnt = 2 Then
    MsgBox Left(uProcess.szExeFile, TmpLen) & _
    " is already running!"
    TwoRun = True
    Exit Do
    End If
    Loop While ProcessNext(hSnapShot, uProcess)
    End If
    Call CloseHandle(hSnapShot)
    End Function

    [/vba]

  4. #4
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    I did forget to say this needs to be in your start up code ie: sub Main.
    [vba]App.PrevInstance[/vba]
    from the help files
    Returns a value indicating whether a previous instance of an application is already running

    ie. how to allow only 1 instance from there?
    This one Key word terminates the application.
    [vba]
    End
    [/vba]

  5. #5
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location
    Works perfect Tommy. MY VB6 newbiness is obvious. Using the object browser I did trial PrevInstance, and path and a few other app things before going the alternative route posted. I couldn't get PrevInstance to work. I now realize that the project has to be made into a .exe, packaged and deployed, and installed before PrevInstance will work ie. you can't test its' use by running the program from VB6 while having an instance of the program running. Thanks for getting rid of all that extra code that I didn't need. Have a great wkend! Dave

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •