How to tell in a vb6 program is another program (not prev instance) is running.

I need to know if another program is running so that my vb6 program will not attempt to shell to the program if it is already running.  The other program has code to prevent two instances from running at the same time.  If my program just attempts to shell to the other program and it is already running it (the other program)will give error message and terminate.

A code snippet  or maybe a link to code example would really help.  I have googled this but no results that give code example.

Thanks here for any help.
morrisboAsked:
Who is Participating?
 
vb_elmarCommented:
>>My vb6 program places a data file in the other programs app path. The
>>other program, if running, is constantly polling for the data file

You can try to open the data file in "For Append" mode.
-If open fails then the file is in use.

Function FileAlreadyOpen(ByVal myPath As String) As Boolean
    On Error GoTo 90
    ff = FreeFile
    Open myPath For Append As ff
    Close #ff
    Exit Function
90:
    FileAlreadyOpen = True
End Function

Private Sub Form_Load() 
    MsgBox FileAlreadyOpen("C:\myFile.txt")
End Sub

Open in new window

0
 
vb_elmarCommented:
If the program has the title "MyApplication" then you can use "FindWindow" to check if the program is running,
because VB6 programs always have the class name "ThunderRT6FormDC". Try this:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Sub Timer1_Timer()
    Caption = FindWindow("ThunderRT6FormDC", "MyApplication")
End Sub
0
 
morrisboAuthor Commented:
I forgot to explain further.  

My vb6 program places a data file in the other programs app path.  The other program, if running, is constantly polling for the data file as it is performing many other functions.

If it is not currently running, as soon as it is executed it looks for the data file, and processes it if it is found.

If the other program is already running, the shell command causes it to display the error message and terminate, since the shell command just executes another copy of the program.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
morrisboAuthor Commented:
el_rnar

Thanks,

question?

Is "MyApplication" the name of the executible e.g. ABC-XYZ.EXE ?

Thanks

morrisbo
0
 
vb_elmarCommented:
It's the visible title.

Sample: If I open "C:\autoexec.bat" using notepad
then the Notepad window is visible and notepad's title is:
"c:\autoexec.bat - Notepad"

(It's not identical to the name of the executible).
0
 
Martin LissOlder than dirtCommented:
Ignore
0
 
morrisboAuthor Commented:
vb_elmar

I will try your suggestion about checking to see if the data file is either open or not there after waiting for about 10 seconds after writing it out.  If it is not there that means the other program is running and has processed the file. It deletes the data file as soon as it is through with it.  If it is open the other program is in the process of handlying the file.
If it is there but not open I know to shell to the other program so that it will start running. It grabs the file and processes it (opens it) as soon as it starts.

I do have a question about your first suggestion

My earlier question was - Is "MyApplication" the name of the executible e.g. ABC-XYZ.EXE ?

You indicated - It's the visible title. You example was where notepad was used toopen a Bat file "c:\autoexec.bat - Notepad".  My program is just a stand alone vb6 executible file.  I may be reading that the example would not work here.  Is that correct.  The actual name of the executible that is being checked to see if it is running is "ProcessIFile.exe"

Thanks,  I think checking on the existence of the data file will work, but just curious about your first suggestion.  Might be useful in the future, when there is not "data" file to look for.

Also, I have a question about what happens when a program is minimized and just closed while in the minimized state.  When you right click on it, you get a windows option to close it.  Do you know if any code is executed in the program being closed in this situation?

Really appreciate your help with this.

morrisbo
0
 
vb_elmarCommented:
>>The actual name of the executible that is ... "ProcessIFile.exe"
If you want to detect a process based on its "executable name" try this sample :


Private Sub Command1_Click()

mySearchName = "ProcessIFile.exe"

For Each App1 In Split(mySearchName, ",")
    Set objProcs = GetObject("winmgmts:\\.\root\cimv2").ExecQuery("select * from Win32_Process where Name='" & App1 & "'")
    For Each process In objProcs

        MsgBox "Application found. Executable name is :" & vbCrLf & vbCrLf & process.Name
        i = MsgBox("Do you want to kill the Application?", vbYesNo)
        If i = vbYes Then process.Terminate
        
    Next
Next

End Sub

Open in new window

0
 
morrisboAuthor Commented:
Hi  vb_elmar

Just a few questions about your last post.

If I can make this work, this might be the best approach.


Should App1 be dimmed as a string? Also how should I dim objProcs.  What about the "process" in For Each process, how should it be dimmed?

I appreciate your help.

morrisbo
0
 
vb_elmarCommented:
Here is a function determining whether "ProcessIFile.exe" is currently
running (the variables are already dimensioned) .

Private Sub Command1_Click()
    Dim retval As Boolean
    retval = myFunction
    MsgBox "Is 'processifile.exe' running:  " & retval
End Sub

Function myFunction() As Boolean
    Dim mySearchName, app1, objProcs, process, i
    mySearchName = "ProcessIFile.exe"

For Each app1 In Split(mySearchName, ",")
    Set objProcs = GetObject("winmgmts:\\.\root\cimv2").ExecQuery("select * from Win32_Process where Name='" & app1 & "'")
    For Each process In objProcs
    If LCase(process.Name) = "processifile.exe" Then myFunction = True
    Next
Next
End Function

Open in new window

0
 
morrisboAuthor Commented:
vb_elmar

Thanks.

Your input was a big help - two possible solutions - I know the solution of checking if the data file is there and open or not will work, and I want to try the other solution you posted as your last comment. If it works I think it is the preferable solution.

morrisbo
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.