CodeItBetter Programming Another VB Programming Blog

How do we close a program forcefully without going through then Unload Procedure? (in two ways)

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'System & API - How do we close a program forcefully without going through then Unload Procedure? (in two ways)
'Method 1:

'We can use the SendMessage API and the WM_CLOSE Message to tell an Application to Close

Private Type POINTAPI
    x As Long
    y As Long
End Type
 
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, _
    ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
 
Private Const WM_CLOSE = &H10
 
Private Sub Command1_Click()
    Dim tPoint As POINTAPI
    Dim lHwnd As Long
 
    Call GetCursorPos(tPoint)
    lHwnd = WindowFromPoint(tPoint.x, tPoint.y)
    Call SendMessage(lHwnd, WM_CLOSE, 0, 0)
End Sub
 
'Method 2:

'We can use the TerminateProcess API, but be warned, this will Forcefully 
'Shutdown whatever you pass it, no questions asked.

Private Type POINTAPI
    x As Long
    y As Long
End Type
 
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, _
    ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _
    ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, _
    lpdwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _
    ByVal uExitCode As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, _
    lpExitCode As Long) As Long
 
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
 
Private Sub Command1_Click()
    Dim tPoint As POINTAPI
    Dim lHwnd As Long
    Dim lPID As Long
    Dim lCode As Long
 
    Call GetCursorPos(tPoint)
    lHwnd = WindowFromPoint(tPoint.x, tPoint.y)
    Call GetWindowThreadProcessId(lHwnd, lPID)
    lPID = OpenProcess(PROCESS_ALL_ACCESS, 0&, lPID)
    Call GetExitCodeProcess(lPID, lCode)
    Call TerminateProcess(lPID, lCode)
End Sub
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.