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
Solved

End Task or Application from VB

Posted on 1998-03-21
5
811 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

829 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