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

How do I close a running program?

Status
Not open for further replies.

EnS

Programmer
Oct 29, 2002
58
US
Hello,
I have a program that, ideally, should not be run concurrently with EXCEL. I have been able to determine whether or not a session of EXCEL is running using the FindWindow function but now I need to know how to close the session programmically. I have been able to get the ThreadId using the GetWindowThreadProcessId and tried terminating the session with TerminateProcess and TerminateThread with no luck. Does anyone have any suggestions?

Thanks

ERM
 
Use the Hwnd that is returned from the FindWindow function and use it in the SendMessage Api.

SendMessage(Hwnd, WM_CLOSE, 0, 0)



If you have the Process ID you can also do this:

Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

Const PROCESS_TERMINATE = &H1

Private function KillProcess(pid)
Dim hProcess As Long
hProcess = OpenProcess(PROCESS_TERMINATE, 0, pid)
TerminateProcess hProcess, 0
CloseHandle hProcess
End function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top