REPOST: Batch file runs inside Server Console

Hi:BACKGROUND:

I have a LS agent that calls a batch file.

PROBLEM:

When this batch file is called it calls the agent in the console window rather than an external DOS window.

SOLUTION?

Can you explain this as it is causing problems as I want the agent to wait until the external window looses focus and then continues.

Here is my code for the agent to wait until the batch file cmd screen looses focus:

RunApp:

Sub RunApp (program$)

structure:

start.cb = Len(start)

ret& = CreateProcessA(0&, program$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

ret& = WaitForSingleObject(proc.hProcess, INFINITE)

ret& = CloseHandle(proc.hProcess)

End Sub

Initilaze:

'Execute java

Print ( " ")

Print(" *********** Date greater - running java extractor. ***************** ")

'Set current directory using UNC path so extractor can be run from clients

Chdrive “c”

Chdir “c:\utils\doti\extractor\src”

’ taskId% = Shell(“extractor.bat”, 1)

Print " ******* Extractor Opened *********"

RunApp(“c:\utils\doti\extractor\src\extractor.bat”)

Print " ******* Extractor Closed *********"

qryediflag.SQL = “DELETE FROM XML_FLAG”

resultediflag.Execute

'Vaildation of xml document is performed from extractor program. Therefore the xml file is not created is not valid.

Print " ********** XML FILE Successfully Generated ******* "

End If

Any ideas would be greatly appreciated.

james

Subject: REPOST: Batch file runs inside Server Console

*DeclareDeclare Function OpenProcess Lib _

“kernel32” (Byval dwDesiredAccess As Long, _

Byval bInheritHandle As Long, _

Byval dwProcessID As Long) As Long

Declare Function WaitForSingleObject _

Lib “kernel32” (Byval hHandle As Long, _

Byval dwMilliseconds As Long) As Long

Declare Function CloseHandle Lib _

“kernel32” (Byval hObject As Long) As Long

Private Const SYNCHRONIZE = &H100000

Private Const INFINITE = -1&

'Error on call

Private Const WAIT_FAILED = -1&

'Normal completion

Private Const WAIT_OBJECT_0 = 0

'Timeout period elapsed

Private Const WAIT_TIMEOUT = &H102&

*Function

Public Function WaitForProcessClose(Byval PID _

As Long, Byval TimeOut As Long) As Boolean

’ Timeout needs to be passed in milliseconds!

Dim hProc As Long

Dim nRet As Long

Const fdwAccess = SYNCHRONIZE

’ Try opening process; wait on it to close.

hProc = OpenProcess(fdwAccess, False, PID)

If hProc Then

nRet = WaitForSingleObject(hProc, TimeOut)

Select Case nRet

Case WAIT_TIMEOUT

’ Still open.

WaitForProcessClose = False

Case WAIT_OBJECT_0

’ Process has closed.

WaitForProcessClose = True

Case Else

’ Error on call.

WaitForProcessClose = False

’ ApiErrorDump Err.LastDllError, _

’ “WaitForSingleObject”

End Select

Else

’ Nothing we can do, at this point.

’ Process may be gone already?

’ ApiErrorDump Err.LastDllError, _

’ “OpenProcess”

WaitForProcessClose = True

End If

’ Clean up.

Call CloseHandle(hProc)

End Function

What I also did was to used with the following section in another routine where I had a list of batchfiles to execute, it looks like this:

Dim taskId As Variant

Forall i In BatchFileNameList

Print BatchFileNameList(Listtag(i))

taskid = Shellid(BatchFileNameList(Listtag(i)))

TaskIdList(Listtag(i)) = taskid

End Forall

After this i only need to step thru the list I used anoter function to do this:

Function ExternalCommandsFinished(TaskIdList List As String) As Boolean

ExternalCommandsFinished = False

Forall i In TaskIdList

If WaitForProcessClose(TaskIdList(Listtag(i)),60000) Then

Print TaskIdList(Listtag(i)) & " is finished"

Else

Print TaskIdList(Listtag(i)) & " is hanging"

ExternalCommandsFinished = False

Exit Function

End If

End Forall

ExternalCommandsFinished = True

End Function

To call this function I used:

If ExternalCommandsFinished(TaskIdList) Then

Call whatever

Else

Call whatever

End If

Subject: REPOST: Batch file runs inside Server Console

This is just a shot in the dark, but did you try adding the “start” command to the beginning of your command line?

“Start” forces a separate window to run a specified program or command. Type “help start” in a command prompt for more info.

RunApp:

Sub RunApp (program$)

structure:

start.cb = Len(start)

ret& = CreateProcessA(0&, "start " + program$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

ret& = WaitForSingleObject(proc.hProcess, INFINITE)

ret& = CloseHandle(proc.hProcess)

End Sub