Just starting out? Need help? Post your questions and find answers here.
percy_b
User
Posts: 72 Joined: Mon Jan 12, 2015 10:25 am
Post
by percy_b » Sat Jul 17, 2021 6:08 pm
Hi Folks,
I'm trying to use the KillProgram command to kill a process I deliberately left open, but it's not working. I know I'm doing something wrong.
Can someone please tell me where I'm messing up?
Thanks.
Code: Select all
Define iI.i
Define sProgramOutput.s
Procedure.s RunExternalProgram(sPathExecutable.s, sWorkingDirectory.s, sParameter.s, *iExitCode1.integer)
Protected iRunResultCode.i, iKillResultCode.i = 0
Protected sLineFeed.s, sOutput.s
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
sLineFeed = #CR$ + #LF$
CompilerDefault
sLineFeed = #LF$
CompilerEndSelect
Debug sPathExecutable
Debug sWorkingDirectory
Debug sParameter
iRunResultCode = RunProgram(sPathExecutable, sParameter, sWorkingDirectory, #PB_Program_Open | #PB_Program_Read | #PB_Program_Error) ; #PB_Program_Wait
iMDL_UI_Process_ID = -1
sOutput = ""
If iRunResultCode
While ProgramRunning(iRunResultCode)
If AvailableProgramOutput(iRunResultCode)
sOutput = sOutput + ReadProgramString(iRunResultCode) + sLineFeed
EndIf
Wend
sOutput = sOutput + sLineFeed + sLineFeed
sOutput = sOutput + "Exit Code: " + Str(ProgramExitCode(iRunResultCode)) + sLineFeed
KillProgram(iRunResultCode)
Debug "Ran Kill command!"
CloseProgram(iRunResultCode) ; Close the connection to the program
EndIf
*iExitCode1\i = iRunResultCode
ProcedureReturn sOutput
EndProcedure
sProgramOutput = RunExternalProgram("cmd.exe", "C:\Windows\", " /K for %c in (C:\Windows\*.*) do echo " + Chr(34) + "%c" + Chr(34), @iI)
Debug "Return code is: " + iI
Debug "Program output = " + sProgramOutput
End
infratec
Always Here
Posts: 6817 Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany
Post
by infratec » Sat Jul 17, 2021 10:45 pm
Do you really need to kill a program?
Your example wthout kill:
Code: Select all
EnableExplicit
Define iI.i
Define sProgramOutput.s
Procedure.s RunExternalProgram(sPathExecutable.s, sWorkingDirectory.s, sParameter.s, *iExitCode1.integer)
Protected iRunResultCode.i, iKillResultCode.i, Bytes.i
Protected sLineFeed.s, sOutput.s
Protected *Buffer
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
sLineFeed = #CR$ + #LF$
CompilerDefault
sLineFeed = #LF$
CompilerEndSelect
Debug sPathExecutable
Debug sWorkingDirectory
Debug sParameter
iRunResultCode = RunProgram(sPathExecutable, sParameter, sWorkingDirectory, #PB_Program_Open | #PB_Program_Read | #PB_Program_Error) ; #PB_Program_Wait
If iRunResultCode
*Buffer = AllocateMemory(4096)
If *Buffer
While ProgramRunning(iRunResultCode)
Bytes = AvailableProgramOutput(iRunResultCode)
If Bytes
ReadProgramData(iRunResultCode, *Buffer, Bytes)
sOutput + PeekS(*Buffer, Bytes, #PB_Ascii)
;Debug sOutput
EndIf
Wend
FreeMemory(*Buffer)
EndIf
;
;
;
; KillProgram(iRunResultCode)
; Debug "Ran Kill command!"
; While ProgramRunning(iRunResultCode)
; Delay(3)
; Wend
sOutput = sOutput + sLineFeed + sLineFeed
sOutput = sOutput + "Exit Code: " + Str(ProgramExitCode(iRunResultCode)) + sLineFeed
CloseProgram(iRunResultCode) ; Close the connection to the program
EndIf
*iExitCode1\i = iRunResultCode
ProcedureReturn sOutput
EndProcedure
sProgramOutput = RunExternalProgram("cmd.exe", "C:\Windows\", " /C for %c in (C:\Windows\*.*) do echo " + #DQUOTE$ + "%c" + #DQUOTE$, @iI)
Debug "Return code is: " + iI
Debug "Program output = " + sProgramOutput
End
infratec
Always Here
Posts: 6817 Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany
Post
by infratec » Sun Jul 18, 2021 3:58 pm
With kill:
Code: Select all
EnableExplicit
Define iI.i
Define sProgramOutput.s
Procedure.s RunExternalProgram(sPathExecutable.s, sWorkingDirectory.s, sParameter.s, *iExitCode1.integer)
Protected iRunResultCode.i, iKillResultCode.i, Bytes.i, Timeout.i
Protected sLineFeed.s, sOutput.s, Error$
Protected *Buffer
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
sLineFeed = #CR$ + #LF$
CompilerDefault
sLineFeed = #LF$
CompilerEndSelect
Debug sPathExecutable
Debug sWorkingDirectory
Debug sParameter
iRunResultCode = RunProgram(sPathExecutable, sParameter, sWorkingDirectory, #PB_Program_Open | #PB_Program_Read | #PB_Program_Error) ; #PB_Program_Wait
If iRunResultCode
*Buffer = AllocateMemory(4096)
If *Buffer
Timeout = ElapsedMilliseconds() + 3000
While ProgramRunning(iRunResultCode) And Timeout > ElapsedMilliseconds()
Bytes = AvailableProgramOutput(iRunResultCode)
If Bytes
ReadProgramData(iRunResultCode, *Buffer, Bytes)
sOutput + PeekS(*Buffer, Bytes, #PB_Ascii)
;Debug sOutput
EndIf
Error$ + ReadProgramError(iRunResultCode, #PB_Ascii)
Wend
; Debug "sOutput " + sOutput
; Debug "Error: " + Error$
KillProgram(iRunResultCode)
Debug "Ran Kill command!"
While ProgramRunning(iRunResultCode)
Bytes = AvailableProgramOutput(iRunResultCode)
If Bytes
ReadProgramData(iRunResultCode, *Buffer, Bytes)
sOutput + PeekS(*Buffer, Bytes, #PB_Ascii)
; Debug "K " + sOutput
EndIf
Error$ + ReadProgramError(iRunResultCode, #PB_Ascii)
Wend
; Debug "K " + Error$
sOutput = sOutput + sLineFeed + sLineFeed
sOutput = sOutput + "Exit Code: " + Str(ProgramExitCode(iRunResultCode)) + sLineFeed
FreeMemory(*Buffer)
EndIf
CloseProgram(iRunResultCode) ; Close the connection to the program
EndIf
*iExitCode1\i = iRunResultCode
ProcedureReturn sOutput
EndProcedure
sProgramOutput = RunExternalProgram("cmd.exe", "C:\Windows\", " /K for %c in (C:\Windows\*.*) do echo " + #DQUOTE$ + "%c" + #DQUOTE$, @iI)
Debug "Return code is: " + iI
Debug "Program output = " + sProgramOutput
End
You need to ensure that the output channels are empty, else the program does not die.
percy_b
User
Posts: 72 Joined: Mon Jan 12, 2015 10:25 am
Post
by percy_b » Mon Jul 19, 2021 12:47 pm
Thanks, Infratect. This is very helpful.
In this particular case, killing the program or issuing a Ctrl-C is necessary. This is because the program continues to run until stopped. Any future calls to RunProgram will produce duplicate copies of the program.
Thanks again.