#include once "window9.bi"

Dim Shared As PROCESS_INFORMATION Ptr g_pi
Dim Shared As STARTUPINFO Ptr g_si

Function Create_Process(ByRef FileName As USTRING, _
						ByRef DirDefault As USTRING, _
						ByVal flag As Integer, _
						ByVal STARTUPINFO_ As STARTUPINFO Ptr, _
						ByVal PROCESS_INFORMATION_ As PROCESS_INFORMATION Ptr) As Integer Export
	
	Dim p As PROCESS_INFORMATION
	Dim s As STARTUPINFO
	If STARTUPINFO_<>0 Then
		g_si=STARTUPINFO_
	Else
		g_si=@s
	End If
	If PROCESS_INFORMATION_<>0 Then
		g_pi=PROCESS_INFORMATION_
	Else
		g_pi=@p
	End If
	#ifdef UNICODE
		If DirDefault<>"" Then
			CreateProcess(0,cast(any ptr , *FileName),0,0,0,flag,0,*DirDefault,g_si, g_pi)
		Else
			CreateProcess(0,cast(any ptr , *FileName),0,0,0,flag,0,0, g_si, g_pi)
		End If  	
	#else	
		If DirDefault<>"" Then
			CreateProcess(0,FileName,0,0,0,flag,0,DirDefault,g_si, g_pi)
		Else
			CreateProcess(0,FileName,0,0,0,flag,0,0, g_si, g_pi)
		End If  	
	#EndIf	
	If g_pi=@p Then
		CloseHandle(g_pi->hProcess)
		CloseHandle(g_pi->hThread)
	End If
	Return g_pi->dwProcessId
End Function

Function Open_Process(ByVal pid As Integer, ByVal Access_ As Integer, ByVal flag As BOOL) As HANDLE  Export
	Return OpenProcess(Access_, flag, pid)
End Function

Sub KillProcess(ByVal hprocess As HANDLE, ByVal ExitCode As Integer) Export
	TerminateProcess(hprocess, ExitCode)
End Sub

Function WaitExitProcess(ByVal hprocess As HANDLE, ByVal WaitTime As Integer) As bool Export
	Return WaitForSingleObject(hprocess, WaitTime)
End Function

Function WaitLoadProcess(ByVal hprocess As HANDLE, ByVal WaitTime As Integer) As bool Export
	Return WaitForInputIdle(hprocess, WaitTime)
End Function

Function GetExitCode(ByVal hprocess As HANDLE) As Integer  Export
	Dim iExit As long
	GetExitCodeProcess(hprocess, @iExit)
	Return iExit
End Function
