Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Procedure.s runToolReadProgramString(path.s,args.s = "",workdir.s = "")
- Protected tool.i,out.s,error.s
- Protected bytes,oldBytes
- Protected *buf
- tool = RunProgram(path,args,workdir,#PB_Program_Open|#PB_Program_Read|#PB_Program_Error|#PB_Program_Hide)
- If tool
- While ProgramRunning(tool)
- If AvailableProgramOutput(tool)
- out + ReadProgramString(tool) + #LF$
- EndIf
- error + ReadProgramError(tool)
- Delay(10)
- Wend
- error + ReadProgramError(tool)
- CloseProgram(tool)
- Else
- Debug "failed to start"
- EndIf
- If Len(error)
- Debug "error: " + error
- EndIf
- out = RTrim(RTrim(out,#LF$),#CR$)
- ProcedureReturn out
- EndProcedure
- Procedure.s runToolReadProgramData(path.s,args.s = "",workdir.s = "")
- Protected tool.i,out.s,error.s
- Protected bytes,oldBytes
- Protected *buf
- tool = RunProgram(path,args,workdir,#PB_Program_Open|#PB_Program_Read|#PB_Program_Error|#PB_Program_Hide)
- If tool
- While ProgramRunning(tool)
- bytes = AvailableProgramOutput(tool)
- If bytes
- If Not *buf
- *buf = AllocateMemory(bytes)
- Else
- *buf = ReAllocateMemory(*buf,oldBytes+bytes)
- EndIf
- ReadProgramData(tool,*buf+oldBytes,bytes)
- oldBytes = MemorySize(*buf)
- EndIf
- error + ReadProgramError(tool)
- Delay(10)
- Wend
- error + ReadProgramError(tool)
- If *buf
- out = PeekS(*buf,MemorySize(*buf),#PB_UTF8|#PB_ByteLength)
- FreeMemory(*buf)
- EndIf
- CloseProgram(tool)
- Else
- Debug "failed to start"
- EndIf
- If Len(error)
- Debug "error: " + error
- EndIf
- out = RTrim(RTrim(out,#LF$),#CR$)
- ProcedureReturn out
- EndProcedure
- string.s = "[⛄️✈️😀🚶✳️‼️🌐🏂👤🔍]"
- CompilerIf #PB_Compiler_OS <> #PB_OS_Windows
- ReadProgramDataResult.s = runToolReadProgramData("echo",string)
- ReadProgramStringResult.s = runToolReadProgramString("echo",string)
- CompilerElse
- ReadProgramDataResult.s = runToolReadProgramData("powershell",~"-Command \"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Write-Host " + string + ~"\"")
- ReadProgramStringResult.s = runToolReadProgramString("powershell",~"-Command \"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Write-Host " + string + ~"\"")
- ; fix for older PoSh versions by kenmo
- If (Left(ReadProgramDataResult, 1) = Chr($FEFF))
- ReadProgramDataResult = Mid(ReadProgramDataResult, 2)
- EndIf
- If (Left(ReadProgramStringResult, 1) = Chr($FEFF))
- ReadProgramStringResult = Mid(ReadProgramStringResult, 2)
- EndIf
- CompilerEndIf
- Debug "Raw string:"
- Debug string
- Debug "ReadProgramData:"
- If ReadProgramDataResult = string
- Debug ReadProgramDataResult + " - ok"
- Else
- Debug ReadProgramDataResult + " - failed"
- EndIf
- Debug "ReadProgramString:"
- If ReadProgramStringResult = string
- Debug ReadProgramStringResult + " - ok"
- Else
- Debug ReadProgramStringResult + " - failed"
- EndIf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement