Solved

End Task or Application from VB

Posted on 1998-03-21
5
795 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Don't work, application continue opened
0
 
LVL 8

Accepted Solution

by:
mrmick earned 200 total points
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

772 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

11 Experts available now in Live!

Get 1:1 Help Now