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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I find out running programs? 1

Status
Not open for further replies.

mkefeli

Programmer
Sep 6, 2003
4
0
0
TR
I want to start calc.exe in Visual Basic with shell command. But I want to check there is a calc.exe already running or not. If there is, then setfocus to it; if not, start a calc.exe program. How can I do this?
 

You can accomplish what you want with FindWindow and SetWindowPos, or you can EnumWindows.

VB Forum222 or VB Win32API Forum711 as suggestions for the future.

Good Luck

 
Hi mkefeli,
From your profile it seems that you have just joined Tek-Tips and this is your first post here.

Welcome to Tek-Tips.
Hope you will enjoy your time here with the Tek-Tips community.

Being a new member, I will advise you to see FAQ222-2244, "How to get the best answers". It will provide you very useful guidelines for getting the best out of this forum.

And here is the solution to your problem.

You need a command button (Command1) on your form to test this code.
___
[tt]
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const SW_NORMAL = 1

Private Sub Command1_Click()
Dim hwndCalc As Long
hwndCalc = FindWindow("SciCalc", "Calculator")
If hwndCalc = 0 Then
Shell "calc.exe", vbNormalFocus
Else
ShowWindow hwndCalc, SW_NORMAL
SetForegroundWindow hwndCalc
End If
End Sub
[/tt]
 
Hello Hypetia,

Thanks for the example code. The FindWindow is asking for Class Name and Window Name. I tried that with Excel using "Excel" as the class name and "Microsoft Excel" as the window name and it didn't find it.

I am very new to API calls so please forgive the elementary level questions. I infer that class name is the Library name that shows up in the object browser. Is that correct? Then I just used the title bar hoping that would work.

Thanks for any help anyone can give me!

Have a great day!

j2consulting@yahoo.com
 
Class name is not the library name. This Class name is the name of the registered window class from which the subject window is created. For Excel windows, this class name is "XLMAIN" (without quotes). So when you call the FindWindow function, pass this string as the lpClassName argument.

The other parameter, is in this case optional. You should pass this parameter only if you exactly know the title of the target window. The title of the Excel window is normally "Microsoft Excel" if no workbook or other document is currently opened.

However, this caption is not fixed and may vary if you open a document in Excel, for example, it may change to "Microsoft Excel - SER.dbf", where SER.dbf is the filename of the currently opened document.

In this case, you should ignore the lpWindowName parameter and pass vbNullString in its place. FindWindow function then looks only for the correct class name when finding windows.

The title of the Calculator application does not change, that is why we specified title as well when finding the Calculator window.

So the code for your requirement becomes this.
___
[tt]
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const SW_NORMAL = 1

Private Sub Command1_Click()
Dim hWndXL As Long, PathXL As String
PathXL = "D:\Program Files\Microsoft Office\Office10\Excel.exe"
hWndXL = FindWindow("XLMAIN", vbNullString)
If hWndXL = 0 Then
Shell PathXL, vbNormalFocus
Else
ShowWindow hWndXL, SW_NORMAL
SetForegroundWindow hWndXL
End If
End Sub[/tt]
___

Note that you need to substitute the correct path of Excel.exe on your computer or better retrieve it from the registry.
 
Hello Hypetia,

Sorry it took me so long to come back and say thank you but I was under the gun. Your code was exactly what I needed. After you got me pointed in the right direction, here is some code I found and modified slightly to return the class name based on the Window title. It came from the AllAPI site at
Based on your answer above you probably already know this but just in case I thought I would post it.

I also found a similar thread on the VB API forum:
thread711-663482.

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

Private Declare Function GetClassName Lib "user32" Alias _
"GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName _
As String, ByVal nMaxCount As Long) As Long

Private Declare Function ShowWindow Lib "user32" _
(ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Const SW_SHOWNORMAL = 1
Const gcClassnameMSWord = "OpusApp"
Const gcClassnameMSExcel = "XLMAIN"
Const gcClassnameNotePad = "Notepad"

Public Sub GetClassNameFromTitle()
Dim sInput As String, lpClassName As String
Dim nMaxCount As Long, lresult As Long, hwnd As Long
nMaxCount = 256
lpClassName = Space(nMaxCount)
sInput = InputBox("Enter the exact window title:" + _
Chr$(13) + Chr$(10) + "Note: must be an exact match")
hwnd = FindWindow(vbNullString, sInput)
If hwnd = 0 Then
MsgBox "Couldn't find the window."
Else
lresult = GetClassName(hwnd, lpClassName, nMaxCount)
MsgBox "Window: " + sInput + Chr$(13) + Chr$(10) + _
"Classname: " + Left$(lpClassName, lresult)
Debug.Print sInput & ": " & Left$(lpClassName, lresult)
End If
End Sub

Thanks again for your helpfulness!


Have a great day!

j2consulting@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top