PDA

View Full Version : Solved: VB6: Only 1 program instance



Dave
12-01-2006, 08:08 AM
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

Tommy
12-01-2006, 07:57 PM
This is rather harsh, but works real good. :)
If App.PrevInstance = True Then
MsgBox "Warning !!! A previous Instance of ThisExE is already Running. This Instance will be Terminated!!"
End
End If

Dave
12-01-2006, 10:32 PM
Here's an informative link...
http://vbnet.mvps.org/index.html?code/system/toolhelpprocesses.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...


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



VB6 Module Code...

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

Tommy
12-02-2006, 07:24 AM
I did forget to say this needs to be in your start up code ie: sub Main.
App.PrevInstance
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.

End

Dave
12-02-2006, 08:57 AM
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