For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > December 2005 > Help with TerminateProcess! Please!









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Help with TerminateProcess! Please!
Lisa Forde

2005-12-13, 7:55 am

Hi. I'm really no good at using the API stuff but i open a number of
processes from my applications using ShellExecute and now I want to close
them from the application.

From searching I have come across TerminateProcess but I'm really not sure
how to use it to close the processes (.exe's) I've opened.

Any help would be much appreciated.


Karl E. Peterson

2005-12-13, 6:56 pm

Lisa Forde wrote:
> Hi. I'm really no good at using the API stuff but i open a number of
> processes from my applications using ShellExecute and now I want to
> close them from the application.
>
> From searching I have come across TerminateProcess but I'm really not
> sure how to use it to close the processes (.exe's) I've opened.
>
> Any help would be much appreciated.


You might find the example, and linked articles, at
http://vb.mvps.org/samples/TaskList of some value. In particular, there's
an MTermProc.bas module that might be drop-in ready. You need only know the
process ID to kill it, with the approaches outlined in that sample.
--
Working without a .NET?
http://classicvb.org/


J French

2005-12-14, 3:55 am

On Tue, 13 Dec 2005 07:59:08 -0400, "Lisa Forde"
<1forde@caribsurf.com> wrote:

>Hi. I'm really no good at using the API stuff but i open a number of
>processes from my applications using ShellExecute and now I want to close
>them from the application.


>From searching I have come across TerminateProcess but I'm really not sure
>how to use it to close the processes (.exe's) I've opened.


In addition to what Karl has said, be very careful of
TerminateProcess, it pulls the rug out from under the App and may not
unload DLLs

Normally one identifies the Window and pumps WM_CLOSE messages at it

Try this first :-

Option Explicit

' J French - 27th Nov 2003 / 1st Aug 2004
' Shell and Re-Parent
' hacked from MS and KPD
' Add Two Command Buttons

Private Declare Function MoveWindow _
Lib "user32" _
(ByVal hWnd As Long, _
ByVal x As Long, _
ByVal y As Long, _
ByVal nWidth As Long, _
ByVal nHeight As Long, _
ByVal bRepaint As Long) As Long

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

Private Declare Function GetWindow _
Lib "user32.dll" _
(ByVal hWnd As Long, _
ByVal wCmd As Long) As Long

Private Declare Function GetParent _
Lib "user32" (ByVal hWnd As Long) As Long

Private Declare Function SetParent _
Lib "user32" _
(ByVal hWndChild As Long, _
ByVal hWndNewParent As Long) As Long

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

Private Declare Function LockWindowUpdate _
Lib "user32" _
(ByVal hwndLock As Long) As Long

Private Declare Function GetDesktopWindow _
Lib "user32" () As Long

Private Declare Function Putfocus _
Lib "user32" _
Alias "SetFocus" _
(ByVal hWnd As Long) As Long

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

Private Declare Function WaitForInputIdle _
Lib "user32.dll" _
(ByVal hProcess As Long, _
ByVal dwMilliseconds As Long) As Long

Private Declare Sub Sleep _
Lib "kernel32" _
(ByVal dwMilliseconds As Long)


Private Const STARTF_FORCEONFEEDBACK As Long = &H40
Private Const STARTF_USESHOWWINDOW As Long = &H1
Private Const SW_SHOWMINIMIZED As Long = 2

Private Const GW_HWNDFIRST As Long = 0
Private Const GW_HWNDNEXT = 2
Private Const WM_QUIT As Long = &H12
Private Const WM_CLOSE = &H10
Private Const WM_SYSCOMMAND As Long = &H112
Private Const SC_CLOSE As Long = &HF060&

Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type

Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type


Private Declare Function CreateProcessA _
Lib "kernel32" _
(ByVal lpApplicationName As String, _
ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As Long, _
ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As String, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long

Private Declare Function WaitForSingleObject _
Lib "kernel32" _
(ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long

Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long

Private Const NORMAL_PRIORITY_CLASS = &H20&

Private Type TCMN
hWnd As Long
End Type
Dim cmn As TCMN

Function InstanceToWnd(ByVal target_pid As Long) As Long
Dim This_hWnd As Long, _
This_pId As Long, _
This_thread_id As Long

'Find the first window top level window
This_hWnd = FindWindow(ByVal 0&, ByVal 0&)
This_hWnd = GetWindow(This_hWnd, GW_HWNDFIRST)

Do While This_hWnd <> 0
' Top level windows have Parents (? TBSO)
If GetParent(This_hWnd) = 0 Then
'Get the window's thread
This_thread_id = GetWindowThreadProcessId(This_hWnd,
This_pId)
If This_pId = target_pid Then
InstanceToWnd = This_hWnd
Exit Do
End If
End If
'retrieve the next window
This_hWnd = GetWindow(This_hWnd, GW_HWNDNEXT)
Loop
End Function

Public Function ExecCmd(cmdline$) As Long
Dim proc As PROCESS_INFORMATION
Dim SI As STARTUPINFO
Dim Ret&

' Initialize the STARTUPINFO structure:
SI.cb = Len(SI)
' --- Window's attempt to get the thing running
SI.dwFlags = STARTF_FORCEONFEEDBACK
' --- Minimized to prevent Flashing
SI.dwFlags = SI.dwFlags Or STARTF_USESHOWWINDOW
SI.wShowWindow = SW_SHOWMINIMIZED

' Start the shelled application:
Ret& = CreateProcessA(vbNullString, cmdline$, _
0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, _
0&, vbNullString, _
SI, proc)
' --- let it start - this seems important
''Call Sleep(9000)
Call WaitForInputIdle(proc.hProcess, 9500)
' we should check the result of this

If Ret Then
ExecCmd = InstanceToWnd(proc.dwProcessID)
Me.Print Ret, ExecCmd
Call CloseHandle(proc.hThread)
Call CloseHandle(proc.hProcess)
End If

End Function


Private Sub Command1_Click()
'KPD-Team 1999
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
' ---
Me.AutoRedraw = True

'Lock the window update - not for XP
'LockWindowUpdate GetDesktopWindow

'Execute notepad.Exe
cmn.hWnd = ExecCmd("notepad.exe")
'cmn.hWnd = ExecCmd("c:\dev\amacc\amacc.exe")
'If mWnd = 0 Then MsgBox "Error starting the app"

Me.Print Str$(cmn.hWnd)
' Set the notepad's parent
If cmn.hWnd Then
SetParent cmn.hWnd, Me.hWnd
' -
Me.ScaleMode = vbPixels
Call MoveWindow(cmn.hWnd, 0, 0, _
Me.ScaleWidth, _
Me.ScaleHeight, 1)
' Put the focus on notepad
Putfocus cmn.hWnd
End If

' Unlock window update
'LockWindowUpdate False
End Sub


Private Sub Command2_Click()
Call SendMessage(cmn.hWnd, WM_CLOSE, 0, ByVal 0&)
'Call SendMessage(cmn.hWnd, WM_QUIT, 0, ByVal 0&)
'Call SendMessage(cmn.hWnd, WM_SYSCOMMAND, SC_CLOSE, ByVal 0&)

End Sub

Private Sub Form_Load()
Command1.Caption = "Start Notepad"
Command2.Caption = "Close Notepad"
End Sub



Karl E. Peterson

2005-12-14, 6:57 pm

J French wrote:
> In addition to what Karl has said, be very careful of
> TerminateProcess, it pulls the rug out from under the App and may not
> unload DLLs
>
> Normally one identifies the Window and pumps WM_CLOSE messages at it


Yep, hence my treatise on how to "Kill an App Gently"...

http://vb.mvps.org/articles/dd200109.asp
--
Working without a .NET?
http://classicvb.org/


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com