Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to close other programms

Status
Not open for further replies.

harmmeijer

Programmer
Mar 1, 2001
869
CN
I am looking for a way to close other running programs (if active) like you would with the tasmaniger -> end task.
This should be done unattended so windows cannot ask me if I am sure or if I want to save any unsaved documents.

Is this possible in Visual basic??
 
Hi,

Create a command button then cut and paste


Option Explicit

'This API is for the SendMessage function call - basically
sends a message to the specified hWnd
Private Declare Function SendMessage Lib "user32"
Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As
Long, ByVal wParam As Long, ByVal lParam As Any) As Long
'This API is for the FindWindow call - is used to Find a
window based ona given Window class name or Window title
(caption)
Private Declare Function FindWindow Lib "user32"
Alias "FindWindowA" (ByVal lpClassName As String, ByVal
lpWindowName As String) As Long
'This API will return a formatted string for a give DLL
error
Private Declare Function FormatMessage Lib "kernel32"
Alias "FormatMessageA" (ByVal dwFlags As Long, ByVal
lpSource As Long, ByVal dwMessageId As Long, ByVal
dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize
As Long, Arguments As Any) As Long
'const required for FormatMessage
Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000

'Our string buffer length
Private Const BUFFER_LENGTH As Long = 255
'This const is used with the SendMessage call
Private Const WM_CLOSE As Long = &H10
'Our custom consts (for readability)
Private Const NO_ERROR As Long = 0
Private Const BYVAL_NULL As Long = 0&

Private Sub Command1_Click()
'Function call
'in this case we are looking for a window with the
caption "KillMe"
'change it to suit your needs
KillWindow , "KillMe"
End Sub

Public Function KillWindow(Optional WindowClass As String =
vbNullString, Optional WindowTitle As String = vbNullString)

'***********************************************************
*****
'* Function Name:
KillWindow *
'* Author: Lewis Cornick
(code@developerexchange.co.uk) *
'* Description: Use to find and close a specific
Window. *
'* Date Created: 18th January
2001 *
'* Date
Modified: *
'*
Mods:
*
'***********************************************************
*****

Dim lhWnd As Long
Dim lRes As Long
Dim lErrorNumber As Long
Dim sErrorDescription As String * BUFFER_LENGTH
Dim lFlags As Long

On Error GoTo Err_Handler

'Ensure our strings are null terminated
WindowClass = WindowClass & vbNullString
WindowTitle = WindowTitle & vbNullString

'In this case we are going to try and find the the
WindowHandle,
'if we already knew the hWnd we could just pass that
straight to
'the SendMessage call.

'Call the FindWindow API, this will return us a valid
WindowHandle,
'If its not a valid window (window isn't found)
FindWindow will return
'0 and Err.LastDLLError will be populated with an
Windows error number
'(see the FormatMessage call)
lhWnd = FindWindow(WindowClass, WindowTitle)

'We assume we have a valid WindowHandle, now its time
to send the
'WM_CLOSE message to tell the window \ app to close.
lRes = SendMessage(lhWnd, WM_CLOSE, BYVAL_NULL,
BYVAL_NULL)

Err_Handler:

'Our Error Handler.
'Check the standard windows error number *and* the DLL
Error to ensure
'that neither the FindWindow or the SendMessage call
failed.

If Err.Number <> NO_ERROR Or Err.LastDllError <>
NO_ERROR Then

'Something failed - need to decide what it was

If Err.Number <> NO_ERROR Then
'Must be a straight VB error
lErrorNumber = Err.Number
sErrorDescription = Err.Description
Else
'An API failed - probably FindWindow due to the
Window not being found.
lErrorNumber = Err.LastDllError
'This API will take the Error number generated
from the API call and
'Format it into a English string for us to
report back to the user.
lRes = FormatMessage
(FORMAT_MESSAGE_FROM_SYSTEM, BYVAL_NULL, lErrorNumber,
BYVAL_NULL, sErrorDescription, BUFFER_LENGTH, BYVAL_NULL)
End If

'Raise the error up to whoever called the function.
Err.Raise lErrorNumber, &quot;KillWindow&quot;,
sErrorDescription
End If

End Function


Ciao
Phathi >:):O>
 
Sorry, after taking out all the returns in the code (there were a lot of returns where there should not be) I called my form KillMe and tried it but I get an error invalid window handle.

Allso I have to kill a proces, a processes do not have a window handler.

I am using NT and have an exel document open, closing this document does not close the process excel.exe in the processes, do you know how to close that?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top