Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Run an EXE and wait for it to finish
Message
De
25/08/2004 12:03:13
 
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Divers
Thread ID:
00936327
Message ID:
00936339
Vues:
18
>I need to run an exe from our application and wait for it to finish, then continuing running our application. I have tried using the RUN command but it doesn’t wait for the other exe to finish. This needs to work with all versions of Windows from 95 to XP.
>
>Any ideas?

haa.. John, I also recall this (I got here before)
#DEFINE normal_priority_class 32
#DEFINE idle_priority_class 64
#DEFINE high_priority_class 128
#DEFINE realtime_priority_class 1600

* Return code from WaitForSingleObject() if
* it timed out.
#DEFINE wait_timeout 0x00000102

* This controls how long, in milli secconds, WaitForSingleObject()
* waits before it times out. Change this to suit your preferences.
#DEFINE wait_interval 200

DECLARE INTEGER CreateProcess IN kernel32.DLL ;
    INTEGER lpApplicationName, ;
    STRING lpCommandLine, ;
    INTEGER lpProcessAttributes, ;
    INTEGER lpThreadAttributes, ;
    INTEGER bInheritHandles, ;
    INTEGER dwCreationFlags, ;
    INTEGER lpEnvironment, ;
    INTEGER lpCurrentDirectory, ;
    STRING @lpStartupInfo, ;
    STRING @lpProcessInformation

DECLARE INTEGER WaitForSingleObject IN kernel32.DLL ;
    INTEGER hHandle, INTEGER dwMilliseconds

DECLARE INTEGER CloseHandle IN kernel32.DLL ;
    INTEGER hObject

DECLARE INTEGER GetLastError IN kernel32.DLL

* STARTUPINFO is 68 bytes, of which we need to
* initially populate the 'cb' or Count of Bytes member
* with the overall length of the structure.
* The remainder should be 0-filled
START = long2str(68) + REPLICATE(CHR(0), 64)

* PROCESS_INFORMATION structure is 4 longs,
* or 4*4 bytes = 16 bytes, which we'll fill with nulls.
process_info = REPLICATE(CHR(0), 16)

* Start a copy of NOTEPAD (EXE name must be null-terminated)
file2run = "C:\WINNT\NOTEPAD.EXE" + CHR(0)

* Call CreateProcess, obtain a process handle. Treat the
* application to run as the 'command line' argument, accept
* all other defaults. Important to pass the start and
* process_info by reference.
retcode = createprocess(0, file2run, 0, 0, 1, ;
    normal_priority_class, 0, 0, @START, @process_info)

* Unable to run, exit now.
IF retcode = 0
    =MESSAGEBOX("Error occurred. Error code: ", getlasterror())
    RETURN
ENDIF

* Extract the process handle from the
* PROCESS_INFORMATION structure.
hprocess = str2long(SUBSTR(process_info, 1, 4))

DO WHILE .T.
    * Use timeout of TIMEOUT_INTERVAL msec so the display
    * will be updated. Otherwise, the VFP window never repaints until
    * the loop is exited.
    IF waitforsingleobject(hprocess, wait_interval) != wait_timeout
        EXIT
    ELSE
        DOEVENTS
    ENDIF
ENDDO

* Show a message box when we're done.
=MESSAGEBOX ("Process completed")

* Close the process handle afterwards.
retcode = closehandle(hprocess)
RETURN


********************
FUNCTION long2str
    ********************
    * Passed : 32-bit non-negative numeric value (m.longval)
    * Returns : ASCII character representation of passed
    *           value in low-high format (m.retstr)
    * Example :
    *    m.long = 999999
    *    m.longstr = long2str(m.long)

    PARAMETERS m.longval

    PRIVATE i, m.retstr

    m.retstr = ""
    FOR i = 24 TO 0 STEP -8
        m.retstr = CHR(INT(m.longval/(2^i))) + m.retstr
        m.longval = MOD(m.longval, (2^i))
    NEXT
    RETURN m.retstr


    *******************
FUNCTION str2long
    *******************
    * Passed:  4-byte character string (m.longstr)
    *   in low-high ASCII format
    * returns:  long integer value
    * example:
    *   m.longstr = "1111"
    *   m.longval = str2long(m.longstr)

    PARAMETERS m.longstr

    PRIVATE i, m.retval

    m.retval = 0
    FOR i = 0 TO 24 STEP 8
        m.retval = m.retval + (ASC(m.longstr) * (2^i))
        m.longstr = RIGHT(m.longstr, LEN(m.longstr) - 1)
    NEXT
    RETURN m.retval
"Now to him who is able to do immeasurably more than all we ask or imagine, according to his power that is at work within us, Ephesians 3:20
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform