Closing an .EXE that is running

A

Thread Starter

Anonymous

Plzzzzzzzzzz help me in getting through this.

I have opened an application using shell command in VB. Can any of u plzz help me how to close the application opened using VB...?
 
J

Jeremy Pollard

<p>In hWindwow = findwindow the winsockclient name should be the name of the
window you are trying to close. The name can be found in the task manager.

<p>There's some stuff here that I use in my app so disregard... like the timers
and forms, etc.

<p>Good luck!

<pre>
'option explicit
Private Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long

Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function PostMessage Lib "user32" _
Alias "PostMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Private Declare Function IsWindow Lib "user32" _
(ByVal hwnd As Long) As Long

Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long

Private Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hwnd As Long, _
lpdwProcessId As Long) As Long

'Constants that are used by the API
Const WM_CLOSE = &H10 ':( As 16-bit Integer ?':( Missing Scope
Const INFINITE = &HFFFFFFFF ':( Missing Scope
Const SYNCHRONIZE = &H100000 ':( Missing Scope
Dim CantFindWindow As Boolean ':( Missing Scope

Private Sub Form_Load()

Dim hWindow As Long
Dim hThread As Long
Dim hProcess As Long
Dim lProcessId As Long
Dim lngResult As Long
Dim lngReturnValue As Long

Timer1.Enabled = False
Timer2.Enabled = False
'Timer3.Enabled = False
frmUpdate.Show
frmUpdate.Caption = "Updating the Winsock Client - Update Ver # : " &
App.Major & ":" & App.Minor
Text2.Text = "Updating : " & AppToUpdate
Text3.Text = "From Directory : " & AppToUpdateDir

DisplayStatus "Attempting to stop WinsockClient.exe "

Delay 1000

hWindow = FindWindow(vbNullString, "Winsockclient")
DisplayStatus "Window #: " & hWindow
hThread = GetWindowThreadProcessId(hWindow, lProcessId)
DisplayStatus "ThreadNum #: " & hThread
hProcess = OpenProcess(SYNCHRONIZE, 0&, lProcessId)
DisplayStatus "Process #: " & hProcess
lngReturnValue = PostMessage(hWindow, WM_CLOSE, 0&, 0&)
DisplayStatus "lngReturnValue #: " & lngReturnValue
lngResult = WaitForSingleObject(hProcess, 5000)


DisplayStatus "lngResult #: " & lngResult
'Does the handle still exist?
DoEvents

' hWindow = FindWindow(vbNullString, "Winsockclient")
If IsWindow(hWindow) = 1 Then
'The handle still exists. Use the TerminateProcess function
'to close all related processes to this handle. See the
'article for more information.
DisplayStatus "Additional instances are still running"

Else 'NOT ISWINDOW(HWINDOW)...
'Handle does not exist.
DisplayStatus "All instances have been closed"

End If
DisplayStatus "Enabling Timer"
Delay 2000
Timer1.Enabled = True

End Sub
</pre>

<p>Cheers from: Jeremy Pollard, CET The Caring Canuckian!<br>
www[.]tsuonline.com
 
B

Brian E Boothe

<p>Link it to a timer or a button and Issue a sendkeys to close an application.
<pre>
Dim LRETVAL
Dim oShell As Object
Dim oRecord
Dim strFileToLaunch
Set oShell = CreateObject("WScript.Shell")
' If (oShell.AppActivate("Acrobat Reader") = False) Then
' End
'End If
oShell.AppActivate "Acrobat Reader"
oShell.SendKeys "%{F4}"
</pre>
<p>Brian E Boothe<br>
IT/Development/<br>
"Services Unlimited,Inc"
 
R

Robert Scott

The method depends on the application you started. An application can be put into a state where it is waiting for very specific input from the user. However, most applications, when sitting at their top-level window will close properly if you send a WM_CLOSE Windows message to the top-level window. Finding that top-level window from what you have from the Shell call is one difficulty. If you can find it, then the SendMessage Win32 API call will allow you to send that window the WM_CLOSE message.

Robert Scott
Real-Time Specialties
Embedded Systems Consulting
 
<p>This script below maybe can solve your problem.
<pre>
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_CLOSE = &H10
Private Sub Form_Load()
Dim winHwnd As Long
Dim RetVal As Long
winHwnd = FindWindow(vbNullString, "Calculator")
If winHwnd <> 0 Then
PostMessage winHwnd, WM_CLOSE, 0&, 0&
Else
MsgBox "The Calculator is not open."
End If
End Sub
</pre>
<p>This sample to close calculator.
 
Top