File: n_cst_runandwait.sru
Size: 6057
Date: Sat, 08 Dec 2007 15:38:25 +0100
$PBExportHeader$n_cst_runandwait.sru
$PBExportComments$Functions to allow waiting for executed program to finish
forward
global type n_cst_runandwait from nonvisualobject
end type
type str_processinformation from structure within n_cst_runandwait
end type
type str_startupinfo from structure within n_cst_runandwait
end type
end forward

type str_processinformation from structure
   ulong    hprocess
   ulong    hthread
   long     dwprocessid
   long     dwthreadid
end type

type str_startupinfo from structure
   long     cb
   string      lpreserved
   string      lpdesktop
   string      lptitle
   long     dwx
   long     dwy
   long     dwxsize
   long     dwysize
   long     dwxcountchars
   long     dwycountchars
   long     dwfillattribute
   long     dwflags
   long     wshowwindow
   long     cbreserved2
   long     lpreserved2
   ulong    hstdinput
   ulong    hstdoutput
   ulong    hstderror
end type

global type n_cst_runandwait from nonvisualobject
end type
global n_cst_runandwait n_cst_runandwait

type prototypes
Function boolean CreateProcessA(string AppName, string CommLine, long l1, long l2, boolean binh, long creationflags, long l3, string dir, str_startupinfo startupinfo, ref str_processinformation pi ) Library 'kernel32.dll' alias for "CreateProcessA"
Function long WaitForSingleObject ( ulong ul_Notification, long lmillisecs ) Library "kernel32.dll"
Function boolean CloseHandle (ulong file_hand) Library "kernel32.dll"

end prototypes

type variables
CONSTANT long INFINITE = -1

CONSTANT integer SW_HIDE      = 0
CONSTANT integer SW_SHOWNORMAL   = 1
CONSTANT integer SW_NORMAL    = 1
CONSTANT integer SW_SHOWMINIMIZED   = 2
CONSTANT integer SW_SHOWMAXIMIZED   = 3
CONSTANT integer SW_MAXIMIZE     = 3
CONSTANT integer SW_SHOWNOACTIVATE  = 4
CONSTANT integer SW_SHOW      = 5
CONSTANT integer SW_MINIMIZE     = 6
CONSTANT integer SW_SHOWMINNOACTIVE = 7
CONSTANT integer SW_SHOWNA    = 8
CONSTANT integer SW_RESTORE      = 9
CONSTANT integer SW_SHOWDEFAULT  = 10
CONSTANT integer SW_FORCEMINIMIZE   = 11
CONSTANT integer SW_MAX    = 11

CONSTANT integer STARTF_USESHOWWINDOW  = 1
CONSTANT integer STARTF_USESIZE        = 2
CONSTANT integer STARTF_USEPOSITION    = 4
CONSTANT integer STARTF_USECOUNTCHARS  = 8
CONSTANT integer STARTF_USEFILLATTRIBUTE  = 16
CONSTANT integer STARTF_RUNFULLSCREEN     = 32
CONSTANT integer STARTF_FORCEONFEEDBACK   = 64
CONSTANT integer STARTF_FORCEOFFFEEDBACK  = 128
CONSTANT integer STARTF_USESTDHANDLES  = 256
CONSTANT integer STARTF_USEHOTKEY      = 512

CONSTANT long CREATE_DEFAULT_ERROR_MODE   = 67108864
CONSTANT long CREATE_FORCEDOS    = 8192
CONSTANT long CREATE_NEW_CONSOLE    = 16
CONSTANT long CREATE_NEW_PROCESS_GROUP = 512
CONSTANT long CREATE_NO_WINDOW      = 134217728
CONSTANT long CREATE_SEPARATE_WOW_VDM  = 2048
CONSTANT long CREATE_SHARED_WOW_VDM = 4096
CONSTANT long CREATE_SUSPENDED      = 4
CONSTANT long CREATE_UNICODE_ENVIRONMENT  = 1024
CONSTANT long DEBUG_PROCESS         = 1
CONSTANT long DEBUG_ONLY_THIS_PROCESS  = 2
CONSTANT long DETACHED_PROCESS      = 8

CONSTANT long HIGH_PRIORITY_CLASS      = 128
CONSTANT long IDLE_PRIORITY_CLASS      = 64
CONSTANT long NORMAL_PRIORITY_CLASS    = 32
CONSTANT long REALTIME_PRIORITY_CLASS     = 256

end variables

forward prototypes
public function integer of_run (string as_exepath, windowstate a_windowstate)
public function integer of_run (string as_exepath, trigevent a_windowstate)
private function integer of_run (string as_exefullpath, integer ai_showwindow)
public function integer of_run (string as_exepath)
end prototypes

public function integer of_run (string as_exepath, windowstate a_windowstate);// This function takes the Normal!, Maximized and Minimized!
// enumerated values and passes the corresponding value
// to the function that actually does the processing.

Integer li_rtn

CHOOSE CASE a_windowstate
   CASE Normal!
      li_rtn = this.of_run(as_exepath, SW_SHOWNORMAL)
   CASE Maximized!
      li_rtn = this.of_run(as_exepath, SW_SHOWMAXIMIZED)
   CASE Minimized!
      li_rtn = this.of_run(as_exepath, SW_SHOWMINIMIZED)
END CHOOSE

Return li_rtn

end function

public function integer of_run (string as_exepath, trigevent a_windowstate);// This function takes the Hide! enumerated value and passes
// SW_HIDE to the function that actually does the processing.

Integer li_rtn

CHOOSE CASE a_windowstate
   CASE Hide!
      // run the passed program
      li_rtn = this.of_run(as_exepath, SW_HIDE)
   CASE ELSE
      // valid trigevent but invalid windowstate
      SetNull(li_rtn)
END CHOOSE

Return li_rtn

end function

private function integer of_run (string as_exefullpath, integer ai_showwindow);str_StartupInfo lstr_si
str_ProcessInformation lstr_pi
Long ll_null, ll_CreationFlags
String ls_null
Integer li_rtn

// return if any arguments are null
If IsNull(as_exefullpath) Or IsNull(ai_showwindow) Then
   SetNull(li_rtn)
   Return li_rtn
End If

// initialize arguments
SetNull(ll_null)
SetNull(ls_null)
lstr_si.cb = 72
lstr_si.dwFlags = STARTF_USESHOWWINDOW
lstr_si.wShowWindow = ai_showwindow
ll_CreationFlags = CREATE_NEW_CONSOLE + NORMAL_PRIORITY_CLASS

// create process/thread and execute the passed program
If CreateProcessA(ls_null, as_exefullpath, ll_null, ll_null, False, &
      ll_CreationFlags, ll_null, ls_null, lstr_si, lstr_pi) Then
   // wait for the process to complete
   WaitForSingleObject(lstr_pi.hProcess, INFINITE)
   // close process and thread handles
   CloseHandle(lstr_pi.hProcess)
   CloseHandle(lstr_pi.hThread)
   // return success
   li_rtn = 1
Else
   // return failure
   li_rtn = -1
End If

Return li_rtn

end function

public function integer of_run (string as_exepath);// This function takes the Normal!, Maximized and Minimized!
// enumerated values and passes the corresponding value
// to the function that actually does the processing.

Integer li_rtn

li_rtn = this.of_run(as_exepath, 0)

Return li_rtn

end function

on n_cst_runandwait.create
call super::create
TriggerEvent( this, "constructor" )
end on

on n_cst_runandwait.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on