Solved

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

Posted on 2012-04-08
11
526 Views
Last Modified: 2012-04-11
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.
0
Comment
Question by:morrisbo
  • 5
  • 5
11 Comments
 
LVL 17

Expert Comment

by:vb_elmar
ID: 37822351
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
 

Author Comment

by:morrisbo
ID: 37822352
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
 

Author Comment

by:morrisbo
ID: 37822361
el_rnar

Thanks,

question?

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

Thanks

morrisbo
0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 37822375
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
 
LVL 45

Expert Comment

by:Martin Liss
ID: 37822381
Ignore
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 17

Accepted Solution

by:
vb_elmar earned 500 total points
ID: 37822459
>>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
 

Author Comment

by:morrisbo
ID: 37831086
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
 
LVL 17

Expert Comment

by:vb_elmar
ID: 37832363
>>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
 

Author Comment

by:morrisbo
ID: 37834795
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
 
LVL 17

Expert Comment

by:vb_elmar
ID: 37835129
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
 

Author Closing Comment

by:morrisbo
ID: 37835989
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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now