Board index » delphi » Waiting until Program had finished execution in Delphi 2

Waiting until Program had finished execution in Delphi 2

Hi There

My problem is this ....

I am currently writing a program in Delphi 2 that needs to execute a
certain dos based application, but the Delphi application must wait
until the dos based app has finished doing it's thing before continuing
with execution.

In Delphi 1 this was done with a WinExec and then using GetModuleUsage
until the Dos app has finished with it's execution.

How can i go about doing the same kind of thing in Delphi 2 (Win95) ???

Any help will be appreciated...

Jaco Alberts
(bo...@aztec.co.za)

 

Re:Waiting until Program had finished execution in Delphi 2


Hi Jaco,
        The problem is not Delphi but in the fact that in the 32bit
pre-emptive world of95 and NT,  launching another app is asynchronous.
No way around that. As far as I know.

Later, Andy

Quote
>Hi There
>My problem is this ....
>I am currently writing a program in Delphi 2 that needs to execute a
>certain dos based application, but the Delphi application must wait
>until the dos based app has finished doing it's thing before continuing
>with execution.
>In Delphi 1 this was done with a WinExec and then using GetModuleUsage
>until the Dos app has finished with it's execution.
>How can i go about doing the same kind of thing in Delphi 2 (Win95) ???
>Any help will be appreciated...
>Jaco Alberts
>(bo...@aztec.co.za)

Re:Waiting until Program had finished execution in Delphi 2


Quote
Jaco Alberts wrote:

> I am sure there must be a way around it somehow. :)
> Just have to find it :)

> Jaco Alberts
> (bo...@aztec.co.za)

Start the program with CreateProcess.
Then use GetExitCodeProcess. When the application you started is still
running, it will return STILL_ACTIVE, otherwise it will be something
else (the exit code).

See WinAPI help for more information.
It's not difficult at all.

Regards,
John.
--
***          ******          *******
*  mailto:joh...@tornado.be        *
*  http://www.tornado.be/~johnfg/  *
*******          ******          ***

Re:Waiting until Program had finished execution in Delphi 2


On Sat, 19 Oct 1996 15:46:12 +0200, Jaco Alberts <bo...@aztec.co.za>
wrote:

Quote
>I am currently writing a program in Delphi 2 that needs to execute a
>certain dos based application, but the Delphi application must wait
>until the dos based app has finished doing it's thing before continuing
>with execution.

>How can i go about doing the same kind of thing in Delphi 2 (Win95) ???

Read about CreateProcess and WaitForSingleObject in the online Win32
help, or search for these keywords in a news archive. This is a
frequently asked and answered question.
--
Ray Lischner, Tempest Software, Inc., Corvallis, Oregon, USA
Author of Secrets of Delphi 2 (http://www.tempest-sw.com/secrets/)

Re:Waiting until Program had finished execution in Delphi 2


Quote
Andy Leitzinger wrote:

> Hi Jaco,
>         The problem is not Delphi but in the fact that in the 32bit
> pre-emptive world of95 and NT,  launching another app is asynchronous.
> No way around that. As far as I know.

> Later, Andy

Hi Andy

Thanx for the reply

I am sure there must be a way around it somehow. :)
Just have to find it :)

Jaco Alberts
(bo...@aztec.co.za)

Re:Waiting until Program had finished execution in Delphi 2


Quote
Jaco Alberts (bo...@aztec.co.za) wrote:
: Andy Leitzinger wrote:

: >         The problem is not Delphi but in the fact that in the 32bit
: > pre-emptive world of95 and NT,  launching another app is asynchronous.
: > No way around that. As far as I know.

: I am sure there must be a way around it somehow. :)
: Just have to find it :)

WaitForSingleObject perhaps?

---------------------------------------------------------------
 John English              | mailto:j...@brighton.ac.uk
 Senior Lecturer           | http://www.comp.it.bton.ac.uk/je
 Dept. of Computing        | fax: (+44) 1273 642405
 University of Brighton    |
---------------------------------------------------------------

Re:Waiting until Program had finished execution in Delphi 2


Well, this is some code that was used in win16 with VB do to this.  I'm not
sure if it works in a win32 environment though.  Tell me if it works once
you try it in Delphi.

btw, I am 14, so if this is totally wrong, at least I tried :)

Scott Sipe

----------------------------------------------------------------------------
----------------------------------------------------------------------------
-------------

' Win16 API Declarations
Declare Function WinExec Lib "Kernel" (ByVal lpCmdLine As _   String, ByVal
nCmdShow As Integer) As Integer
Declare Function GetModuleUsage Lib "Kernel" (ByVal _
        hModule As Integer) As Integer

' ShowWindow() Constants
Global Const SW_HIDE = 0
Global Const SW_SHOWNORMAL = 1
Global Const SW_SHOWMINIMIZED = 2
Global Const SW_SHOWMAXIMIZED = 3
Global Const SW_SHOWNOACTIVATE = 4
Global Const SW_SHOW = 5
Global Const SW_MINIMIZE = 6
Global Const SW_SHOWMINNOACTIVE = 7
Global Const SW_SHOWNA = 8
Global Const SW_RESTORE = 9

Function ShellAndWait (ByVal CmdLine$, ByVal CmdShow%) As_      Integer
        Dim hInstShell%
        '
        ' Trim any leading or trailing CmdLine spaces,
        ' and make sure CmdShow is valid.
        '
        CmdLine = Trim$(CmdLine)
        If CmdShow < SW_HIDE Or CmdShow > SW_RESTORE Then
                CmdShow = SW_SHOWNORMAL
        End If
        '
        ' Issue shell directive.
        '
        hInstShell = WinExec(CmdLine, CmdShow)
        If hInstShell >= 32 Then
                '
                ' Program executed normally!  Use Win16
                ' method of waiting for task to complete.
                '
                Do While GetModuleUsage(hInstShell)
                        DoEvents
                Loop
                '
                ' Indicate success.
                '
                ShellAndWait = True
        End If
End Function

listing 2

' Required Win16 API declarations
Declare Function FindWindow Lib "User" (ByVal lpClassName _   As Any, ByVal
lpWindowName As Any) As Integer
Declare Function GetParent Lib "User" (ByVal hWnd%) As _      Integer
Declare Function GetWindowWord Lib "User" (ByVal hWnd%, _     ByVal nIndex%)
As Integer
Declare Function GetWindow Lib "User" (ByVal hWnd%, ByVal _   wCmd%) As
Integer

' Constant used by GetWindowWord to find next window
Global Const GW_HWNDNEXT = 2

Function GetHWndByInst (hInstFind%) As Integer
        Dim hWndTmp%
        '
        ' Find first window and loop through all subsequent
        ' windows in master window list.
        '
        hWndTmp = FindWindow(0&, 0&)
        Do Until hWndTmp = 0
                '
                ' Make sure this window has no parent.
                '
                If GetParent(hWndTmp) = 0 Then
                        '
                        ' Compare passed hInst against this window's hInst.
                        ' If a match, then return hWnd for current window.
                        '
                        If hInstFind% = GetWindowWord(hWndTmp, _
        GWW_HINSTANCE) Then
                                GetHWndByInst = hWndTmp
                                Exit Do
                        End If
                End If
                '
                ' Get next window in master window list and continue.
                '
                hWndTmp = GetWindow(hWndTmp, GW_HWNDNEXT)
        Loop
End Function

{listing 3}
' Required Win16 API declarations
Declare Function FindWindow Lib "User" (ByVal lpClassName _   As Any, ByVal
lpWindowName As Any) As Integer
Declare Function SetActiveWindow Lib "User" (ByVal hWnd _     As Integer) As
Integer
Declare Function GetWindow Lib "User" (ByVal hWnd As _        Integer, ByVal wCmd
As Integer) As Integer
Declare Function GetWindowText Lib "User" (ByVal hWnd As _    Integer, ByVal
lpString As String, ByVal aint As _
        Integer) As Integer
Declare Function GetParent Lib "User" (ByVal hWnd As _        Integer) As Integer

' Constant used by GetWindowWord to find next window
Global Const GW_HWNDNEXT = 2

' Constants used by FindWindowPartial
Global Const FWP_STARTSWITH = 0
Global Const FWP_CONTAINS = 1

Sub AppActivatePartial (TitleContains$, Method%)
        Dim hWndApp As Integer
        Dim nRet As Integer
        '
        ' Retrieve window handle for first top-level window
        ' that starts with or contains the passed string.
        '
        hWndApp = FindWindowPartial(TitleContains, Method)
        If hWndApp Then
                '
                ' Switch to it.
                '
                nRet = SetActiveWindow(hWndApp)
        Else
                '
                ' Alert user that request failed.
                '
                MsgBox "No matching applications found."
        End If
End Sub

Function FindWindowPartial (ByVal TitlePart$, Method%) As _     Integer
        Dim hWndTmp As Integer
        Dim nRet As Integer
        Dim TitleTmp As String
        '
        ' Alter partial title for case-insensitive compares.
        '
        TitlePart = UCase(TitlePart)
        '
        ' Find first window and loop through all subsequent
        ' windows in master window list.
        '
        hWndTmp = FindWindow(0&, 0&)
        Do Until hWndTmp = 0
                '
                ' Make sure this window has no parent.
                '
                If GetParent(hWndTmp) = 0 Then
                        '
                        ' Retrieve caption text from current window.
                        '
                        TitleTmp = Space(256)
                        nRet = GetWindowText(hWndTmp, TitleTmp, Len(TitleTmp))
                        If nRet Then
                                '
                                ' Clean up return string, preparing for
                                ' case-insensitive comparison.
                                '
                                TitleTmp = UCase(Left(TitleTmp, nRet))
                                '
                                ' Use appropriate method to determine if
                                ' current window's caption either starts
                                ' with or contains passed string.
                                '
                                Select Case Method
                                        Case FWP_STARTSWITH
                                                If InStr(TitleTmp, TitlePart) = 1 Then
                                                        FindWindowPartial = hWndTmp
                                                        Exit Do
                                                End If
                                        Case FWP_CONTAINS
                                                If InStr(TitleTmp, TitlePart) Then
                                                        FindWindowPartial = hWndTmp
                                                        Exit Do
                                                End If
                                End Select
                        End If
                End If
                '
                ' Get next window in master window list and continue.
                '
                hWndTmp = GetWindow(hWndTmp, GW_HWNDNEXT)
        Loop
End Function

{listing 4}
' Win16 Declarations
Declare Function ShowWindow Lib "User" (ByVal hWnd As _       Integer, ByVal
nCmdShow As Integer) As Integer
Declare Function FindWindow Lib "User" (ByVal lpClassName _   As Any, ByVal
lpWindowName As Any) As Integer
Declare Function GetParent Lib "User" (ByVal hWnd%) As _      Integer
Declare Function GetWindow Lib "User" (ByVal hWnd%, ByVal _   wCmd%) As
Integer
Declare Function GetWindowWord Lib "User" (ByVal hWnd%, _     ByVal nIndex%)
As Integer
Declare Function GetModuleFileName Lib "Kernel" (ByVal _      hModule As
Integer, ByVal lpFilename As String, ByVal _    nSize As Integer) As Integer

' GetWindow and GetWindowWord Constants
Const GW_HWNDNEXT = 2
Const GWW_HINSTANCE = -6

Function GetModuleHWnd (ByVal Module$) As Integer
        Dim hWndTmp%
        Dim hInstTmp%
        Dim ModTmp$
        Dim nRet%
        '
        ' Clean up module name, and create buffer to
        ' receive names of running modules.
        '
        Module = Trim$(UCase$(Module))
        ModTmp = Space$(128)
        '
        ' Find first window and loop through all subsequent
        ' windows in master window list.
        '
        hWndTmp = FindWindow(0&, 0&)
        Do Until hWndTmp = 0
                '
                ' Make sure this window has no parent.
                '
                If GetParent(hWndTmp) = 0 Then
                        '
                        ' Retrieve the instance handle, and use that
                        ' to retrieve the module name.
                        '
                        hInstTmp = GetWindowWord(hWndTmp, GWW_HINSTANCE)
                        nRet = GetModuleFileName(hInstTmp, ModTmp, 128)
                        '
                        ' Compare this window's module name to that
                        ' passed in.  Exit if match found.
                        '
                        If Right$(Left$(ModTmp, nRet), Len(Module)) = Module _  Then
                                GetModuleHWnd = hWndTmp
                                Exit Do
                        End If
                        Debug.Print Left$(ModTmp, nRet)
                End If
                '
                ' Get next window in master window list and continue.
                '
                hWndTmp = GetWindow(hWndTmp, GW_HWNDNEXT)
        Loop
End Function

Other Threads