Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is a program running?

Status
Not open for further replies.

gvt

Programmer
Jul 19, 2000
14
SE
I would like to to find out if, for instance, Explorer or Netscape is already running. I have heard that&nbsp;&nbsp;FindWindow API function can be used to search for the Window Title specified but the problem is that the Window Title can change depending on the site or file name which is open. Is there a way to go around this? <br>I have also heard that something called &quot;App&quot;&nbsp;&nbsp;can be used instead but unfortunately I can not find any example about this App on VB help topics. <br>If you know anything about these or any other way to find out if a program is already running please let me know. <br><br>Thank you for your time and effort. <br><br>&nbsp;&nbsp;&nbsp;Vio
 
your are right App is for application object in VB but that is for your VB application is currently running. You can check instanances and other properties of that application. As far as Explore or NetEscape is concerned you can use VBA application objects. I have no idea exactly what you have to do for that but I am trying to give an idea. I no this will not resovle your problem immediately but just for an idea so you can further explore in that.
 
For programs that are in the registry as an ActiveX Server, you can use the GetObject command.&nbsp;&nbsp;Here is some sample code.&nbsp;&nbsp;Create a new project and add a command button.&nbsp;&nbsp;Paste the following into the click event:<br><br>Private Sub Command1_Click()<br>'If Word is open, get a pointer to it.<br>'If not, open it.<br>&nbsp;&nbsp;&nbsp;&nbsp;Const cstrWordApp = &quot;word.application.8&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim bolRetry As Boolean<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim WordApp As Object<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;On Error GoTo Command1_Click_Err<br><br>&nbsp;&nbsp;&nbsp;&nbsp;bolRetry = False<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;Set WordApp = GetObject(, cstrWordApp)<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;If bolRetry = True Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set WordApp = CreateObject(cstrWordApp)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WordApp.Visible = True<br>&nbsp;&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;Exit Sub<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>Command1_Click_Err:<br><br>&nbsp;&nbsp;&nbsp;&nbsp;If Err.Number = 429 And bolRetry = False Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bolRetry = True<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Resume Next<br>&nbsp;&nbsp;&nbsp;&nbsp;Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Handle other errors<br>&nbsp;&nbsp;&nbsp;&nbsp;End If<br><br>End Sub<br><br><br>This code will see if Word is running, and if so it will get a pointer to it.&nbsp;&nbsp;If it is not, it will start it and make it visible.<br><br>Either way, once you have this pointer, you can do many things, such as open documents, save them, close Word, etc, by using the ActiveX interface Word provides.<br><br>For example to close Word you would use:<br><br>WordApp.Quit<br><br>IE and Netscape should have similar ActiveX interfaces available (I know IE does, I'm not sure about Netscape).<br><br>Hope that helps.<br>Steve<br>
 
I didn't read FAQ above before writing this so if it's really off base (and I doubt it will be) then that's why. <br><br>I too have lived throught the same situation. In the API reference and in many (if not all) technical articals on locating a window using the API, typically 'Notepad.exe' and &quot;GetWindowText()&quot;, will be used in the example. <br><br>The reality is that if Notepad.exe is running with a document loaded then the name &quot;Notepad&quot; will appear after the name of the loaded text file and a dash. (Incidently , have you noticed that most of the help examples in Microsofts' help files do not reflect real-world situations?). <br><br>Some time after posting the solution to this (the solution used the API call to &quot;GetWindowText()&quot;). After the joy of discovering this solution wore off, about an hour or so later. The computing world realized that a program such as &quot;Notepad.exe&quot; (and probably 80% of Windows programs) would almost never have the same title in the text of the Caption bar all of the time! Meaning that using &quot;GetWindowText()&quot;, supplying the text that you're after to find an executing application<br>is pretty useless in real world situations. For the solution (you can find the actual code at Microsoft's MSDN web site) you'd have to plan to &quot;GetWindowText()&quot; for every open window in order to locate the window with the Caption bar which contains the text that you're looking for. <br><br>This is at best 99.00% reliable for obvious reasons. But this is the only way to identify an open window accurately from within your application.<br><br>To make this 100% fool proof. Open the (external) application from within your application. This way you have a handle to that specific window.<br><br>With NT (I'm not sure about Win98) one can get a list of executing processes which will return the name of the executable itself. Of course if you write the program this way it becomes OS specific. I currently only have NT machines at my disposal and cannot comment on whether this is possable with Win98.<br><br><br>: )<br>Happy computing ! <p>Amiel<br><a href=mailto:amielzz@netscape.net>amielzz@netscape.net</a><br><a href= > </a><br>
 
Try the CreateToolhelpSnapshot function listed in tclere's FAQ. It is very helpful if you know the name of the Windows EXE or DLL and it eliminates the need to search through the enumerated GetWindowText(s) for a possible Instr instance of &quot;Notepad&quot;.<br><br>Note to Amiel:<br>We are all overworked and underpaid. We are just happy to be computing!<br><img src= <p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top