We help IT Professionals succeed at work.

kill process started with Shell

motokent
motokent asked
on
I want to start a web browser with the Shell command, wait a few seconds, and then kill the window.  I can't kill the window.  Any ideas?  (The msgboxs give valid numbers.  For example, on one run:  5076, 736, 1)
=================
=================

Private Const SYNCHRONIZE = &H100000
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _
            ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Sub LaunchWebBrowser(Optional URL As String = vbNullString)
.
.
.
  pointerPID = Shell(CallMe, vbMinimizedNoFocus)
Msgbox pointerPID  
  MyHnd = OpenProcess(SYNCHRONIZE, 0, pointerPID)
  Sleep 5
MsgBox MyHnd
  x = CloseHandle (MyHnd)
MsgBox x

Open in new window

Comment
Watch Question

Try instead:

=) Cheers,
Danny





Private _MyHnd As SafeCloseHandle
   ...
_MyHnd=MyHnd
_MyHnd.Close

Open in new window

Author

Commented:
Two problems...

1. "Private _MyHnd as SafeCloseHandle" gives an "invalid character" message for the "_"
2. Using another name without underbar doesn't show "SafeCloseHandle" as an option.

Do I need to add a reference?  I'm using VB6.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:

Author

Commented:
to Idle_Mind... one of the options works fine, except that it kills both instances when I'm using it to open web browsers (I also tested with Notepad.exe opening a second instance of Notepad manually, and it correctly kills only the one instance that the code opened).  I want to open a browser that immediately opens a new window via javascript so that I can adjust window size, etc.  Then I want to close the first window to cut down on clutter.  Any idea how to kill only one of the windows?  Page1.htm is the blank window that simply opens Page2.htm.  After Page2.htm comes up, I want to kill Page1.htm.  The code below kills both.  Here's what I use:
Option Explicit

Private Const PROCESS_ALL_ACCESS = &H1F0FFF

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

Private pid As Long

Private Sub Command1_Click()
    pid = Shell("C:\Program Files\Mozilla Firefox\firefox.exe http://www.mynetwork.com/page1.htm", vbNormalFocus)
End Sub

Private Sub Command2_Click()
    Dim lngProcess As Long
    If pid <> 0 Then
        lngProcess = OpenProcess(PROCESS_ALL_ACCESS, 0&, pid)
        If lngProcess <> 0 Then
            Call TerminateProcess(lngProcess, 0&)
        End If
    End If
End Sub
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
...hmmmm, if the second window is opened via javascript then it really isn't a "standalone" window as it is technically being "hosted" by the main page.  I think...I'm not a web developer.

Maybe you could just hide the main window?

Use Ark's PidToHwnd() function here:
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_20246724.html

Then use that hWnd with the ShowWindow() API and SW_HIDE:

    Private Const SW_HIDE As Long = 0
    Private Declare Function ShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Example:

    ShowWindow someHwnd, SW_HIDE

*just don't forget to close out the window/process when you're done or you'll have hidden orphaned processes.

Author

Commented:
Hiding the first window seems to be the best answer so far.  Actually, just using vbHide in the shell command gets it done.  Thanks for your help.

Does it make sense to keep track of all the PIDs until the user closes the VB application, and then  running TerminateProcess on all of them?
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
"Does it make sense to keep track of all the PIDs until the user closes the VB application, and then  running TerminateProcess on all of them?"

Definitely...especially since they are now hidden windows.  Otherwise they will be left open but not visible...