?
Solved

End Task or Application from VB

Posted on 1998-03-21
5
Medium Priority
?
823 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 400 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

Independent Software Vendors: 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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Suggested Courses

764 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