CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2001
    Posts
    24

    How to Terminate a running application from another application?

    I want to close a application
    from another applicatioon so plz help me
    out.
    Expecting details....as the level is beginner
    rgds,

    :-)

  2. #2
    Join Date
    Apr 2000
    Posts
    737

    Re: How to Terminate a running application from another application?

    if you know the window caption,

    you can try

    http://vblib.virtualave.net, there is a function call ShutDownApp in vbSystem to terminate it.

    hope this help.


  3. #3
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: How to Terminate a running application from another application?

    You save the handle to the window that you created and then kill it later. The following code should help you.


    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

    'Constants used by the API functions
    Const WM_CLOSE = &H10
    Const INFINITE = &HFFFFFFFF

    private Sub Form_Load()
    Command1.Caption = "Start the Calculator"
    Command2.Caption = "Close the Calculator"
    End Sub

    private Sub Command1_Click()
    'Starts the Windows Calculator
    Shell "calc.exe", vbNormalNoFocus
    End Sub

    private Sub Command2_Click()
    'Closes the Windows Calculator
    Dim hWindow as Long
    Dim lngResult as Long
    Dim lngReturnValue as Long

    hWindow = FindWindow(vbNullString, "Calculator")
    lngReturnValue = PostMessage(hWindow, WM_CLOSE, vbNull, vbNull)
    lngResult = WaitForSingleObject(hWindow, INFINITE)

    'Does the handle still exist?
    If IsWindow(hWindow) = 0 then
    'The handle still exists. Use the TerminateProcess function
    'to close all related processes to this handle.
    MsgBox "Handle still exists."
    else
    'Handle does not exist.
    MsgBox "Program closed."
    End If
    End Sub





  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: How to Terminate a running application from another application?

    This is worth an excelent - but I am already out of votes. Someone else, please?...
    ;-)

    Have a nice day and happy coding.


    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

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

Featured