Newsgroup: it.comp.lang.visual-basic
Titolo: detecting close of a shelled dos program in vb5

hi, my name is adam and i am making a simple installer in vb5 which
shells out to a dos program to do the installing
i need to run a DOS program with command line parameters and have it
hidden like it is with shell(command,vbhide) - doesnt show up on screen,
not in alt-tab,  not on taskbar, etc.
then i need it to sit in a loop (with a doevents in it) and wait for the
program to end
i've been able to run a windows program and detect when the program is
closed, but i havent been able to run the program hidden
also, it never detects the close of a DOS program because the dos
program sits there and says for instance "Unpack - Finished" but never
closes so my program sits there forever in an infinite loop
so i was wondering if you could please help me get the dos program
hidden with createprocess and get it to close upon completion (like you
can do if you made a .pif file and checked the close upon exit box, but
i cant have it use .pif files)
if you could help me with this or direct me to where i might find help,
i would greatly appreciate it, and if you do have something for me
please email it to 
ada...@san.rr.com
thank you very much,
adam m.
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp
   Create Your Own Free Member Forum

This should get you started...
(Declarations)
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 Const INFINITE = -1&
Private Declare Function WaitForSingleObject Lib "kernel32" ( _
                    ByVal hHandle As Long, _
                    ByVal dwMilliseconds As Long) _
                As Long
Private Declare Function CloseHandle Lib "kernel32" ( _
                    ByVal hObject As Long) _
                As Long
' strCmd will be something like:
' "C:\MyDosDir\MyDosApp.exe /switches AdditionalParam"
Private Sub ShellMyApp(ByVal strCmd As String)
    Dim iTaskID As Long
    iTaskID = Shell(strCmd, vbMinimizedFocus)
    If (iTaskID <> 0) Then
        Call WaitForTermination(iTaskID)
    End If
End Sub
Private Sub WaitForTermination(ByVal TaskID As Long)
    Dim iTaskHandle As Long
    iTaskHandle = OpenProcess(SYNCHRONIZE, 0, TaskID)
    If (iTaskHandle <> 0) Then
        Call WaitForSingleObject(iTaskHandle, INFINITE)
        Call CloseHandle(iTaskHandle)
    End If
End Sub
--
Mark E Alsop
mal...@elite.net

Hello
Suppose the scenario is just a bit different. What if you just want to
know if the Dos (or Windows) program is still running. I tried the
following without success . It always returned True. Any help
would be greatly appreciated.  Thanks, Murray
AppProcessId = Shell ...
isThisAppRunning AppProcessID
Public Function isThisAppRunning(AppProcessId as Long) As Boolean
    Dim iTaskHandle As Long
    isThisAppRunning = False
    iTaskHandle = OpenProcess(SYNCHRONIZE, 0, AppProcessId)
    If iTaskHandle <> 0 Then isThisAppRunning = True
End Function

...
:Hello

:

Private Sub cmdShell_Click()
Dim lPid As Long
Dim lHnd As Long
Dim lRet As Long
' The WaitForSingleObject function returns when one of the following
occurs:
'   -   The specified object is in the signaled state.
'   -   The time-out interval elapses.
'
' The dwMilliseconds parameter specifies the time-out interval, in
milliseconds.
' The function returns if the interval elapses, even if the object’s
state is
' nonsignaled. If dwMilliseconds is zero, the function tests the object’
s state
' and returns immediately. If dwMilliseconds is INFINITE, the function’s
time-out
' interval never elapses.
'
' This example waits an INFINITE amount of time for the process to end.
As a
' result this process will be frozen until the shelled process
terminates. The
' down side is that if the shelled process hangs, so will this one.
'
' A better approach is to wait a specific amount of time.  Once the
time-out
' interval expires, test the return value. If it is WAIT_TIMEOUT, the
process
' is still not signaled.  Then you can either wait again or continue
with your
' processing.
'
' DOS Applications:
'    Waiting for a DOS application is tricky because the DOS window
never goes
'    away when the application is done.  To get around this, prefix the
app that
'    you are shelling to with "
command.com
 /c".
'
lPid = Shell("
command.com
 /c " & YourApp , vbHide)
If lPid <> 0 Then
    lHnd = OpenProcess(SYNCHRONIZE, 0, lPid)       'Get a handle to the
shelled process.
    If lHnd <> 0 Then                              'If successful, wait
for the
        lRet = WaitForSingleObject(lHnd, INFINITE) ' application to end.
        CloseHandle (lHnd)                         'Close the handle.
    End If
    MsgBox "Just terminated.", vbInformation, "Shelled Application"
End If
End Sub
BAS MODULE STUFF
--------------------------------
Public Const SYNCHRONIZE = &H100000
Public Const INFINITE = &HFFFF    'Wait forever
Public Const WAIT_OBJECT_0 = 0    'The state of the specified object is
signaled.
Public Const WAIT_TIMEOUT = &H102 'The time-out interval elapsed, and
the
                                  'object’s state is nonsignaled.
Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As
Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As
Long, ByVal dwMilliseconds As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As
Long

Hello
 
What does it mean when an object is in the 'signalled' state.
Thanks
Murray

That does not use DoEvents & it also does not solve the problem of Unpack -
Finished.
--
James Johnston
E-mail:  
johnsto...@usa.net
Mark E Alsop wrote in message ...
