Results 1 to 5 of 5

Thread: Close an application that was started using shellexecute

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    8

    Close an application that was started using shellexecute

    i wish to close another program that was started by using the shell execute. i looked at send message and postmessage API's but it doesnt seem to work. I have attached sample code below. an help that you can provide will be greately appriciated.
    thanks





    Option Explicit
    'Private Declare Function GetForegroundWindow Lib "user32" () 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

    Private Declare Function SendMessage Lib "user32"
    Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

    Private Const WM_CLOSE = &H10

    Private Sub Command1_Click()
    tHwnd = ShellExecute(Me.hwnd, vbNullString, "c:\temp.jpg", vbNullString, "C:\", 1)
    End Sub

    Private Sub Command2_Click()
    PostMessage tHwnd, WM_CLOSE, 0, 0

    'i have also tried this
    SendMessage tHwnd, WM_CLOSE, 0, 0

    End Sub


    both the functions dont work. please help.
    thanks once again

  2. #2
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    After: Private Const WM_CLOSE = &H10, put: Private tHwnd as Long

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    8
    oops, i did not notice that.
    but inspite of adding the variable declaration i am unable to close the window that i had opened using shell execute. the code i am using now is attached with the variable declaration. thanks once again for trying to help.



    Option Explicit

    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

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

    Private Const WM_CLOSE = &H10

    Private thwnd As Long


    Private Sub Command1_Click()
    thwnd = ShellExecute(Me.hwnd, vbNullString, "c:\temp.jpg", vbNullString, "C:\", 1)
    End Sub

    Private Sub Command2_Click()
    'PostMessage thwnd, WM_CLOSE, 0, 0
    SendMessage thwnd, WM_CLOSE, 0, 0
    End Sub

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    ShellExecute does not return a window handle. Look up ShellExecuteEx. Using that, you can obtain a process handle that you can use to terminate the process that is created.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    8
    so do i use ShellExecuteEx to open the file instead of just ShellExecute? or use it in conjunction to find the window handle. Can you please point me to some example or give me a snippet if you already have some.
    thanks alot

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