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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
morrisboAuthor Commented:
el_rnar

Thanks,

question?

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

Thanks

morrisbo
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.