Link to home
Start Free TrialLog in
Avatar of morrisbo
morrisbo

asked on

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.
Avatar of vb_elmar
vb_elmar
Flag of Germany image

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
Avatar of morrisbo
morrisbo

ASKER

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.
el_rnar

Thanks,

question?

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

Thanks

morrisbo
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).
Avatar of Martin Liss
Ignore
ASKER CERTIFIED SOLUTION
Avatar of vb_elmar
vb_elmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
>>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

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
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

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