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!

API call needed to end Application Unconditionally

Status
Not open for further replies.

Brian10

Programmer
Jul 24, 2001
3
ZA
I have written the following code that will end a windows application. The problem is that when I send the WM_CLOSE message, the program tries to close. But if any changes have been made to the application another window appears first asking if you would like to save your changes. This window then prevents the application from closing.

I am looking for an API call that like Task Manager closes the application unconditionally.

Code sample.
Public Const WS_CANCELMODE = &H1F
Public Const WM_CLOSE = &H10

rc = PostMessage(TargetHwnd, WS_CANCELMODE, 0, 0&)
rc = PostMessage(TargetHwnd, WM_CLOSE, 0, 0&)
 
Have a look at the ExitProcess and/or TerminateProcess API calls. There is a slight but significant difference between the two - and you'll need to understand how to get the relevant process handle. For now, I leave that as an exercise for the reader...
 
I think this is what you want
Option Explicit
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal Hwnd As Long, lpdwProcessId As Long) As Long
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

Public Function KillProcess(wCaption As String) As Boolean
' Kill it very silently
Dim ProcID, Hwnd, hProc As Long
Dim ret

Hwnd = FindWindow(vbNullString, wCaption)
ret = GetWindowThreadProcessId(Hwnd, ProcID)

hProc = OpenProcess(0, 0, ProcID)

ret = TerminateProcess(hProc, 0)

KillProcess = True
End Function


If you want to terminte all process, you should use EnumProcess API


Jimmy Le
nhan_tiags@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top