ReFox Help – Launching from VFP

How to launch ReFox from Visual FoxPro
avoiding the disadvantages of RUN command

***
***
*
* REFOX.EXE and MAKEFILE.MAK in C:\Program Files\REFOX
*
***
***
= MESSAGEBOX ("Launching ReFox", 64)
* quotation marks
= ExecProg(["C:\Program Files\ReFox XII\ReFox.exe" @MAKEFILE], "C:\Program Files\ReFox XII" )
RETURN
*
FUNCTION ExecProg(CmdLine as String, CurrDir as String) as VOID
*
* see MSDN for description
*
DECLARE LONG CreateProcess IN kernel32.dll ;
    LONG   lpApplicationName, ;
    STRING lpCommandLine, ;
    LONG   lpProcessAttributes, ;
    LONG   lpThreadAttributes, ;
    LONG   bInheritHandles, ;
    LONG   dwCreationFlags, ;
    LONG   lpEnvironment, ;
    STRING lpCurrentDirectory, ;
    STRING @lpStartupInfo, ;
    STRING @lpProcessInformation
*
DECLARE LONG GetExitCodeProcess IN kernel32.dll ;
    LONG hProcess, ;
    LONG @lpExitCode
*
DECLARE LONG WaitForSingleObject IN kernel32.dll ;
    LONG hHandle, ;
    LONG dwMilliseconds
*
DECLARE LONG CloseHandle IN kernel32.dll ;
    LONG hObject
*
DECLARE LONG GetLastError IN kernel32.dll
*
*
#DEFINE NORMAL_PRIORITY_CLASS 0x20
#DEFINE WAIT_TIMEOUT 0x0102
#DEFINE WAIT_INTERVAL 100
#DEFINE FALSE 0
*
* STARTUPINFO is 68 byte, first long is size of structure
startup_info = long2str(68) + REPLICATE(CHR(0), 64)

* PROCESSINFO is 4x long = 16 byte,
process_info = REPLICATE(CHR(0), 16)

* CreateProcess, handle in process_info
* CmdLine and CurrDir need NULL
RetCode = CreateProcess(0, CmdLine+CHR(0), 0, 0, 1, ;
 NORMAL_PRIORITY_CLASS, 0, CurrDir+CHR(0), ;
 @startup_info, @process_info)

* finish when error
IF RetCode = FALSE
= MESSAGEBOX( "ERROR"+CHR(10)+"number:"+STR(GetLastError()), 16)
RETURN
ENDIF

* handle of process and thread in PROCESS_INFORMATION structure
hProcess = str2long(SUBSTR(process_info, 1, 4))
hThread = str2long(SUBSTR(process_info, 5, 4))

* each WAIT_INTERVAL [msec] do DOEVENTS, refresh display
DO WHILE (WaitForSingleObject(hProcess, WAIT_INTERVAL) = WAIT_TIMEOUT)
DOEVENTS
ENDDO
* get process exit code
= GetExitCodeProcess(hProcess, @RetCode)
 
* close handle process and thread
= CloseHandle(hProcess)
= CloseHandle(hThread)
 
* now we can say goodbye ...
= MESSAGEBOX ("Program finished"+CHR(10)+"code:"+STR(RetCode), 64)
 
RETURN
*
ENDFUNC
*
FUNCTION long2str(longval as Long) as String
RETURN CHR
(BITAND(BITRSHIFT(longval, 0),0xFF)) ;
     + CHR(BITAND(BITRSHIFT(longval, 8),0xFF)) ;
     + CHR(BITAND(BITRSHIFT(longval,16),0xFF)) ;
     + CHR(BITAND(BITRSHIFT(longval,24),0xFF))
ENDFUNC
*
FUNCTION str2long(longstr as String) as Long
RETURN ASC
(SUBSTR(m.longstr,1))*(2^ 0) ;
     + ASC(SUBSTR(m.longstr,2))*(2^ 8) ;
     + ASC(SUBSTR(m.longstr,3))*(2^16) ;
     + ASC(SUBSTR(m.longstr,4))*(2^24)
ENDFUNC
*

---
rex_run.html