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!

Need help with ending program.

Status
Not open for further replies.

XWalrus2

Programmer
May 27, 2002
47
0
0
US
I need help with how to create a program. I'm trying to write a program that will end and then restart another program every 30 minutes. I know to use a timer, and use shell to start it, but I am lost on how to end the program. Any help would be appreciated. As you can probably tell, I am pretty inexperienced with Visual Basic, and simplest possible explanations would be best.
 
...you can kill a program by using API functions or by using pskill from pstools.
to use API, by the way, you must know the classname and/or title of the program that you want to close...use spy.exe, if you want to get a class name of a program. run Calc.exe then try this:

option explicit

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function ShowWindow Lib "user32" _
(ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long

Sub HideXWindow()
Dim Handle As Long
Dim lpClassName As String
Dim lpCaption As String

Const NILL = 0&
Const WM_SYSCOMMAND = &H112
Const SC_CLOSE = &HF060&

lpClassName = "SciCalc"
lpCaption = "Calculator"

'get handle for the window
'for unknown window caption use this line
Handle = FindWindow(lpClassName, vbNullString)
'for unknown class name use this line
Handle = FindWindow(vbNullString, lpCaption)
'for both known class name and caption use this line
Handle = FindWindow(lpClassName, lpCaption)

If Handle = 0 Then
Debug.Print "specified program is not running"
'some codes here to handle this or Exit Function
End If

'send message to close program(s)
Handle = SendMessage(Handle, WM_SYSCOMMAND, SC_CLOSE, NILL)
End Function

...For using PsKill, check out my previous thread thread222-531574

Now, you know how to shut down a program, i'm sure that you can handle the rest to restart a program.

Have Fun!
 
pstools is just the kind of thing I was looking for! You are my hero!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top