Solved

End Task or Application from VB

Posted on 1998-03-21
5
820 Views
Last Modified: 2010-05-18
How i can end task o application from VB 5.0 ?
0
Comment
Question by:49407211
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 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