Home All Groups Group Topic Archive Search About

Stop exe program in visual basic

Author
3 Jun 2005 3:56 AM
weetat
Hi all ,

   I am using visual basic 6 . Am using Shell() to execute external
program in VB .

   How to stop\Kill the exe program in code ? Any sample code available ?

Thanks.

Author
3 Jun 2005 7:52 AM
J French
On Fri, 03 Jun 2005 11:56:41 +0800, weetat <t***@wavex-tech.com>
wrote:

>Hi all ,
>
>   I am using visual basic 6 . Am using Shell() to execute external
>program in VB .
>
>   How to stop\Kill the exe program in code ? Any sample code available ?

You need to get the hWnd of the Shelled application

A Google on groups for: Shell reparent "J French"
will come up with most of the code
- including a bit you don't yet want

InstanceToWnd is the core bit

Then to shut down the spawned App send it a WM_CLOSE message

Finding the hWnd is a bit tricky for some Apps, if they use splash
screens or spawn other bits.
Author
3 Jun 2005 2:17 PM
Ken Halter
"weetat" <t***@wavex-tech.com> wrote in message
news:uFZuK9%23ZFHA.3976@TK2MSFTNGP15.phx.gbl...
> Hi all ,
>
>   I am using visual basic 6 . Am using Shell() to execute external program
> in VB .
>
>   How to stop\Kill the exe program in code ? Any sample code available ?
>
> Thanks.

Try....

Safely Shut Down a Running Application
http://www.thescarms.com/vbasic/StopProcess.asp

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
Author
3 Jun 2005 3:05 PM
RB Smissaert
I tried it, but I can't stop a program with it unless it was started with
the same VB app.
Is this how it is meant to be?

RBS

Show quoteHide quote
"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message
news:eIcRDcEaFHA.1448@TK2MSFTNGP09.phx.gbl...
> "weetat" <t***@wavex-tech.com> wrote in message
> news:uFZuK9%23ZFHA.3976@TK2MSFTNGP15.phx.gbl...
>> Hi all ,
>>
>>   I am using visual basic 6 . Am using Shell() to execute external
>> program in VB .
>>
>>   How to stop\Kill the exe program in code ? Any sample code available ?
>>
>> Thanks.
>
> Try....
>
> Safely Shut Down a Running Application
> http://www.thescarms.com/vbasic/StopProcess.asp
>
> --
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
> Please keep all discussions in the groups..
>
>
Author
3 Jun 2005 5:12 PM
Ken Halter
"RB Smissaert" <bartsmissa***@blueyonder.co.uk> wrote in message
news:uBRKt2EaFHA.3364@TK2MSFTNGP09.phx.gbl...
>I tried it, but I can't stop a program with it unless it was started with
>the same VB app.
> Is this how it is meant to be?
>
> RBS

Well... you did say....

>>   I am using visual basic 6 . Am using Shell() to execute external
>> program in VB .
>>
>>   How to stop\Kill the exe program in code ? Any sample code available ?
>>

So, which app do you want to kill? Your app? or the Shelled app?

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
Author
3 Jun 2005 5:56 PM
RB Smissaert
OK, so this is meant for stopping an application that you started first with
Start Process.

RBS

Show quoteHide quote
"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message
news:uhaD59FaFHA.2124@TK2MSFTNGP14.phx.gbl...
> "RB Smissaert" <bartsmissa***@blueyonder.co.uk> wrote in message
> news:uBRKt2EaFHA.3364@TK2MSFTNGP09.phx.gbl...
>>I tried it, but I can't stop a program with it unless it was started with
>>the same VB app.
>> Is this how it is meant to be?
>>
>> RBS
>
> Well... you did say....
>
>>>   I am using visual basic 6 . Am using Shell() to execute external
>>> program in VB .
>>>
>>>   How to stop\Kill the exe program in code ? Any sample code available ?
>>>
>
> So, which app do you want to kill? Your app? or the Shelled app?
>
> --
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
> Please keep all discussions in the groups..
>
Author
3 Jun 2005 8:08 PM
Ken Halter
"RB Smissaert" <bartsmissa***@blueyonder.co.uk> wrote in message
news:u%23w8VWGaFHA.1372@TK2MSFTNGP10.phx.gbl...
> OK, so this is meant for stopping an application that you started first
> with Start Process.
>
> RBS

There must be something here you can use....

Process Related
http://www.thescarms.com/vbasic/VBProcessRelated.asp

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
Author
3 Jun 2005 8:50 PM
RB Smissaert
Thanks, will have a look at that.
For my purpose something like this is good enough:

Function CloseApp(ByVal strApp As String, _
                  ByVal strClass As String) As Long

    Dim hwnd As Long

    On Error GoTo ERROROUT

    hwnd = FindWindowHwndLike(0, _
                              strClass, _
                              strApp, _
                              0)

    If hwnd = 0 Then
        CloseApp = 0
        Exit Function
    End If

    'Post a message to the window to close itself
    '--------------------------------------------
    PostMessage hwnd, WM_CLOSE, 0&, 0&

    CloseApp = hwnd

    Exit Function
ERROROUT:

    On Error GoTo 0
    CloseApp = 0

End Function


Function FindWindowHwndLike(ByVal hWndStart As Long, _
                        ByVal ClassName As String, _
                        ByVal WindowTitle As String, _
                        ByVal level As Long) As Long

    'finds the first window where the class name start with ClassName
    'and where the Window title starts with WindowTitle, returns Hwnd
    '-----------------------------------------------------------------
    Dim hwnd As Long
    Dim sWindowTitle As String
    Dim sClassName As String
    Dim R As Long

    'Initialize if necessary. This is only executed
    'when level = 0 and hWndStart = 0, normally
    'only on the first call to the routine.
    If level = 0 Then
        If hWndStart = 0 Then
            hWndStart = GetDesktopWindow()
        End If
    End If

    'Increase recursion counter
    level = level + 1

    'Get first child window
    hwnd = GetWindow(hWndStart, GW_CHILD)

    Do Until hwnd = 0

        'Search children by recursion
        Call FindWindowHwndLike(hwnd, ClassName, WindowTitle, level)

        'Get the window text
        sWindowTitle = Space$(255)
        R = GetWindowText(hwnd, sWindowTitle, 255)
        sWindowTitle = Left$(sWindowTitle, R)

        'get the class name
        sClassName = Space$(255)
        R = GetClassName(hwnd, sClassName, 255)
        sClassName = Left$(sClassName, R)

        'Check if the window matches the search parameters
        If (sWindowTitle Like WindowTitle & "*") And _
           (sClassName Like ClassName & "*") Then
            FindWindowHwndLike = hwnd
            Exit Function
        End If

        'Get next child window
        hwnd = GetWindow(hwnd, GW_HWNDNEXT)

    Loop

End Function


RBS


Show quoteHide quote
"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message
news:OQ8qyfHaFHA.1092@tk2msftngp13.phx.gbl...
> "RB Smissaert" <bartsmissa***@blueyonder.co.uk> wrote in message
> news:u%23w8VWGaFHA.1372@TK2MSFTNGP10.phx.gbl...
>> OK, so this is meant for stopping an application that you started first
>> with Start Process.
>>
>> RBS
>
> There must be something here you can use....
>
> Process Related
> http://www.thescarms.com/vbasic/VBProcessRelated.asp
>
> --
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
> Please keep all discussions in the groups..
>