Redirect Dos-Output of a programm on Realtime

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5357
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Redirect Dos-Output of a programm on Realtime

Post by Kwai chang caine »

The code of DoubleDutch works very well,
viewtopic.php?p=78024#p78024
but someone know how read the french characters with accent ? :|

numro = numéro
srie = série
Rpertoire = Répertoire
Le volume dans le lecteur C s'appelle Neptune
Le numro de srie du volume est 2ADC-090B

Rpertoire de c:\

Code: Select all

Global pi.PROCESS_INFORMATION
Global hWritePipe.l,hReadPipe.l,PipeMem.l,PipeString$
Global compx.l,last.s

PipeMem=AllocateMemory(1024)

Procedure GetOutPut()
  bytesread.l = 0
  r.l=PeekNamedPipe_(hreadPipe,0,0,0,@bytesread,0)
  If bytesread>0
    o.l=ReadFile_(hReadPipe,PipeMem,1023,@bytesread,0)
    If o
      For f.l=0 To bytesread-1
        t.b=PeekB(PipeMem+f)
        If t=10
          PrintN(PipeString$)
          PipeString$=""
        Else
          If t>31 And t<128
            PipeString$+Chr(t)
          EndIf
        EndIf
      Next
    EndIf
  EndIf
EndProcedure

Procedure RunCommand(cmd.s,path.s) 
  sa.SECURITY_ATTRIBUTES
  sa\nLength=SizeOf(SECURITY_ATTRIBUTES)
  sa\bInheritHandle=1
  sa\lpSecurityDescriptor=0

  xx.l=CreatePipe_(@hReadPipe,@hWritePipe,@sa,1)
  si.STARTUPINFO
  si\cb=SizeOf(STARTUPINFO)
  si\dwFlags=#STARTF_USESTDHANDLES|#STARTF_USESHOWWINDOW
  si\hStdOutput=hWritePipe
  si\hStdError=hWritePipe

  cmdcom$=Space(255)
  GetEnvironmentVariable_("comspec",@cmdcom$,255)
  If cmdcom$="command.com"
    cmdcom$+" "
  Else
    cmdcom$+" /C "
  EndIf
  If FileSize(path)=-2
    pathaddr=@path
  Else
    pathaddr=0
  EndIf
  ProcedureReturn CreateProcess_(0,cmdcom$+cmd,@sa,@sa,1,#CREATE_NEW_CONSOLE,0,pathaddr,@si,@pi)
EndProcedure

OpenConsole()

PipeString$=""
If RunCommand("dir","c:/")
  exitcode.l=259
  While exitcode=259
    t=GetExitCodeProcess_(pi\hProcess,@exitcode)
    If exitcode=259
      GetOutPut()
    EndIf
  Wend
  PrintN("*** Finished - Press Enter ***")
  CloseHandle_(hWritePipe) 
Else
  Debug GetLastError_()
EndIf

Input()

CloseConsole() 
ImageThe happiness is a road...
Not a destination
breeze4me
Enthusiast
Enthusiast
Posts: 527
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: Redirect Dos-Output of a programm on Realtime

Post by breeze4me »

Kwai chang caine wrote:The code of DoubleDutch works very well,
viewtopic.php?p=78024#p78024
but someone know how read the french characters with accent ? :|

numro = numéro
srie = série
Rpertoire = Répertoire
Le volume dans le lecteur C s'appelle Neptune
Le numro de srie du volume est 2ADC-090B

Rpertoire de c:\
It seems to work fine on Windows 10.

Code: Select all

CompilerIf #PB_Compiler_Version < 570
  CompilerError "Use PB v5.70+"
CompilerEndIf

Procedure RunCMD(param$ ,Unicode = 0)
  Protected cmd, CmdUnicode$
  Protected Flag, CmdFlag
  
  If Unicode
    Flag = #PB_Unicode
    CmdFlag = #PB_Program_Unicode
    CmdUnicode$ = "/u "
  Else
    Flag = #PB_Ascii
    CmdFlag = #PB_Program_Ascii
  EndIf
  
  cmd = RunProgram("cmd", CmdUnicode$ + "/c " + #DQUOTE$ + param$ + #DQUOTE$, "", #PB_Program_Hide | #PB_Program_Open | #PB_Program_Read | CmdFlag)
  
  If cmd
    While ProgramRunning(cmd)
      If AvailableProgramOutput(cmd)
        PrintN(ReadProgramString(cmd, Flag))
      EndIf
    Wend
    
    CloseProgram(cmd)
    
    PrintN("*** Finished - Press Enter ***")
    
  EndIf
  
EndProcedure

OpenConsole("test", #PB_Unicode)

RunCMD("dir " + #DQUOTE$ + "c:\" + #DQUOTE$, 1)

Input()

CloseConsole()

Using OpenConsole() without the unicode flag is working too.

Code: Select all

Procedure RunCMD(param$ ,Unicode = 0)
  Protected cmd, CmdUnicode$
  Protected CmdFlag
  
  If Unicode
    CmdFlag = #PB_Program_Unicode
    CmdUnicode$ = "/u "
  Else
    CmdFlag = #PB_Program_Ascii
  EndIf
  
  cmd = RunProgram("cmd", CmdUnicode$ + "/c " + #DQUOTE$ + param$ + #DQUOTE$, "", #PB_Program_Hide | #PB_Program_Open | #PB_Program_Read | CmdFlag)
  
  If cmd
    While ProgramRunning(cmd)
      If AvailableProgramOutput(cmd)
        PrintN(ReadProgramString(cmd))
      EndIf
    Wend
    
    CloseProgram(cmd)
    
    PrintN("*** Finished - Press Enter ***")
    
  EndIf
  
EndProcedure

OpenConsole("test")

RunCMD("dir " + #DQUOTE$ + "c:\" + #DQUOTE$, 1)
;RunCMD("ping -w 100 128.0.0.1")

Input()

CloseConsole()
Last edited by breeze4me on Thu Oct 11, 2018 8:33 pm, edited 3 times in total.
User avatar
DoubleDutch
Addict
Addict
Posts: 3219
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Re: Redirect Dos-Output of a programm on Realtime

Post by DoubleDutch »

:)
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5357
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Redirect Dos-Output of a programm on Realtime

Post by Kwai chang caine »

Thanks a lot DoubleDutch, your two codes works perfectly 8)

In fact i have choose this code, because i want use this style of code for several others softwares stdout DIRECT reading (Php, Node, Java, Python, Ruby, etc ...)

Code: Select all

RunProgram("Php.exe", "" , "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Write|#PB_Program_Error|#PB_Program_UTF8|#PB_Program_Hide)
and without passing by cmd :idea:

Code: Select all

RunProgram(GetEnvironmentVariable("comspec"), "Php.exe" , "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Write|#PB_Program_Error|#PB_Program_UTF8|#PB_Program_Hide)

Or

RunProgram(GetEnvironmentVariable("Cmd.exe"), "\c Php.exe" , "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Write|#PB_Program_Error|#PB_Program_UTF8|#PB_Program_Hide)
In fact sometime ReadProgramString() works, sometime no, i don't know why, but otlers API methods exist for reading/writing StdOut (Like PIPE, or may be others) and perhaps works when Pb native not

And i wondering, if ReadProgramString() using exactely the same method at your advice ?
It's the reason why i have search this style of code with PIPE, for read this StdOut :wink:
But like nothing is really simple in programming, they are always an history of UNICODE or ANSI, that i can resolve...., for lock me :|
For return nothing, or this time for not return french accents :? :|

Before asking this question, i have search and try to adding several codes, i not really understand :oops: ) like this

Code: Select all

Procedure Uni2Ansi(srcStr$)
  Protected bufLen = StringByteLength(srcStr$, #PB_Ascii) + 1
  Protected *outBuf = AllocateMemory(bufLen)
 
  If *outBuf
    PokeS(*outBuf, srcStr$, -1, #PB_Ascii)
    ProcedureReturn *outBuf
  EndIf
EndProcedure
or

Code: Select all

OemToChar_(Oem$, Oem$)
or modify #PB_Ascii, #PB_Unicode, #PB_Utf8 everywhere i see this style of flags and count to the chance
Imagebut apparently.... the gods, like KCC too, ...have not really understand all this historys of characters convertion, about "UNICODE & CO" for can't help KCC, even one time in all the try :lol:

It's the reason why, i have asking a new time at my MASTERS of the forum, for help me to read all the StdOut possible and often using of all the amazing EXE (PHP, NODE, PYTHON, etc ...) obviously if it have an StdOut. :mrgreen:

So if you, or someone, can say to me, if it's not useful to search to convert the breeze4me code because it's exactely the same than ReadProgramString()
Or if they are a chance than ReadProgramString() not use the PIPE in the same conditions, can help me for read accent in the breeze4me code return :D

Again thanks for your precious above codes, helps and interest for the movie of "KCC in the bad world of characters convert" 8)
ImageThe happiness is a road...
Not a destination
Post Reply