Solved

End Task or Application from VB

Posted on 1998-03-21
5
799 Views
Last Modified: 2010-05-18
How i can end task o application from VB 5.0 ?
0
Comment
Question by:49407211
5 Comments
 
LVL 1

Expert Comment

by:stevesm
ID: 1459550
There must be something more. What kind of task?

Put a command button on your main form with the following code.
Call your command button cmdClose as in this example.

Private Sub cmdClose_Click()
    Unload Me   'unload the form
    End         'ends the applications
End Sub

0
 
LVL 2

Expert Comment

by:alokm
ID: 1459551
Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
                 (ByVal lpClassName As String, _
                 ByVal lpWindowName As String) _
                 As Long

 Declare Function SendMessage  Lib "user32" Alias "SendMessageA" _
                 (ByVal hwnd As Long, _
                 ByVal wMsg As Long, _
                 ByVal wParam As Long, _
                 lParam As Long) _
                 As Long

       
   
Dim lpClassName As String
Dim lpCaption As String
Dim Handle As Long
Const NILL = 0&
Const  WM_SYSCOMMAND = &H112
Const SC_CLOSE = &HF060&
lpClassName = "Class name of the program to be terminated"
lpCaption = "The caption of the program to be terminated" .
Handle =FindWindow(lpClassName$, lpCaption$)
Handle = SendMessage(Handle, WM_SYSCOMMAND, SC_CLOSE, NILL)  
0
 
LVL 2

Expert Comment

by:alokm
ID: 1459552
sorry,
one error
Const SC_CLOSE=&HF060 and not with the & at the end.
Also for the class name of the application , you can use vbnullstring.
0
 

Author Comment

by:49407211
ID: 1459553
Don't work, application continue opened
0
 
LVL 8

Accepted Solution

by:
mrmick earned 200 total points
ID: 1459554
49407211,

Here’s an example of how to start a process and terminate it.  Create a new project.  Add two command buttons to Form1 (Command1 & Command2).  Don’t worry about placement or size.  Copy the following code and paste it into the Form1 module.  Run the project.

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 Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long

Const WM_SYSCOMMAND = &H112
Const SC_CLOSE = &HF060
Const GW_HWNDNEXT& = 2

Dim ProcID As Long

Private Sub Form_Load()

   Command1.Caption = "Start (Calc) Process"
   Command1.Move 480, 180, 1875, 435
   Command2.Caption = "End (Calc) Process"
   Command2.Move 480, 660, 1875, 435
   Me.Move 0, 0, 2970, 1635

End Sub
Private Sub Command1_Click()

   ProcID = Shell("Calc.exe", vbNormalFocus)

End Sub
Private Sub Command2_Click()

   CloseShell ProcID

End Sub
Sub CloseShell(ProcessID As Long)
   
   Dim hWnd&, hWndClose&, ID&

   hWnd = FindWindow(0&, 0&)
   Do While hWnd
      GetWindowThreadProcessId hWnd, ID
      hWndClose = IIf(ID = ProcessID, hWnd, 0)
      hWnd = GetWindow(hWnd, GW_HWNDNEXT)
      If hWndClose Then
         SendMessage hWndClose, WM_SYSCOMMAND, SC_CLOSE, 0&
      End If
   Loop

End Sub
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now