Results 1 to 5 of 5

Thread: Closing apps opened with Shell

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Closing apps opened with Shell

    If you open another application by using Shell (or ShellExecute) - I want to make sure that, when I close my VB app, the application I opened closes as well.

    How can I do this please?

    Thanks for any help.

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Closing apps opened with Shell

    I thought maybe the Long returned from either could be used to the close it by using PostMessage API. But its not the same long as FindWindow returns so it doesnt work.

    What app are you starting?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Closing apps opened with Shell

    Try this:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    4. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    5. '
    6. Private Const PROCESS_TERMINATE As Long = (&H1)
    7. '
    8. Dim hProcess As Long
    9.  
    10. '------------------------------------------------------------------
    11.  
    12. Sub Shell2(strCommand As String, WindowStyle As VbAppWinStyle)
    13.     Dim lProcessId As Long
    14.  
    15.     'Shell and get the PID
    16.     lProcessId = Shell(strCommand, WindowStyle)
    17.     '
    18.     ' Get the process handle
    19.     hProcess = OpenProcess(PROCESS_TERMINATE, False, lProcessId)
    20.  
    21. End Sub
    22.  
    23. '------------------------------------------------------------------
    24.  
    25. Private Sub Command1_Click()
    26.     Shell2 "D:\Windows\system32\cmd.exe", vbNormalFocus
    27.    
    28.     If hProcess <> 0 Then Command1.Enabled = False
    29. End Sub
    30.  
    31. '------------------------------------------------------------------
    32.  
    33. Private Sub Form_Unload(Cancel As Integer)
    34.     'Close the opened process
    35.     If hProcess <> 0 Then
    36.         TerminateProcess hProcess, 1
    37.     End If
    38. End Sub

    found the original code here. Modified it for your need.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Closing apps opened with Shell

    Nice!
    im saving that one
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: Closing apps opened with Shell

    Thanks very much for that. Worked first time!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width