I try to get the result of a nslookup call using purebasic RunProgram() function. Here is how I do:
Code: Select all
#LineBreak = #CRLF$
; run an executable and return his console output
Procedure.s runCmd(program.s, params.s, wd.s = "", lf.s = #LineBreak, Hidden.b = #False)
Protected Compiler.i
If Hidden.b = #True
Compiler.i = RunProgram(program.s, params.s, wd.s, #PB_Program_Open|#PB_Program_Read|#PB_Program_Hide)
Else
Compiler.i = RunProgram(program.s, params.s, wd.s, #PB_Program_Open|#PB_Program_Read)
EndIf
Protected Output.s = ""
If Compiler.i
While ProgramRunning(Compiler.i)
If AvailableProgramOutput(Compiler.i)
Protected Line.s = ReadProgramString(Compiler.i)
Output.s + Line.s + lf.s
EndIf
Wend
CloseProgram(Compiler.i) ; Close the connection to the program
EndIf
ProcedureReturn Output.s
EndProcedure
Debug runCmd("nslookup", "-q=MX google.com.")Got it to work like this, but is this really needed?
Code: Select all
; run an executable and return his console output
Procedure.s runCmd(program.s, params.s, wd.s = "", lf.s = #LineBreak, Hidden.b = #False)
Protected Compiler.i
If Hidden.b = #True
Compiler.i = RunProgram(program.s, params.s, wd.s, #PB_Program_Open|#PB_Program_Read|#PB_Program_Hide)
Else
Compiler.i = RunProgram(program.s, params.s, wd.s, #PB_Program_Open|#PB_Program_Read)
EndIf
Protected Output.s = ""
If Compiler.i
While ProgramRunning(Compiler.i)
Protected LineLength.i = AvailableProgramOutput(Compiler.i)
If LineLength.i > 0
Protected *buffer = AllocateMemory(LineLength.i)
ReadProgramData(Compiler.i, *buffer, LineLength.i)
Protected Line.s = PeekS(*buffer, LineLength.i, #PB_Ascii)
FreeMemory(*buffer)
Output.s + Line.s + lf.s
EndIf
Wend
CloseProgram(Compiler.i) ; Close the connection to the program
EndIf
ProcedureReturn Output.s
EndProcedure
