Récupérer la sortie d'une console ?

Programmation d'applications complexes
nico
Messages : 3702
Inscription : ven. 13/févr./2004 0:57

Récupérer la sortie d'une console ?

Message par nico »

Je ne trouve pas d'exemple de code pour récupérer la sortie d'une console d'un autre process que le prog lance?

Quelqu'un à fait des essais là-dessus?
Gillou
Messages : 373
Inscription : sam. 28/août/2004 17:35
Localisation : Bretagne, 22
Contact :

Message par Gillou »

A mon tour de te refiler une source ;)


ProcedureDLL Error(message$, fatal)
Global proc.PROCESS_INFORMATION, hReadPipe, hWritePipe
wError = GetLastError_ ()
If wError
*ErrorBuffer = AllocateMemory (1024)
FormatMessage_ ( #FORMAT_MESSAGE_FROM_SYSTEM , 0, wError, 0, *ErrorBuffer, 1024, 0)
message$+ Chr (10)+ PeekS (*ErrorBuffer)
FreeMemory (*ErrorBuffer)
EndIf
MessageRequester ( "Error" , message$)
If fatal: End : EndIf
EndProcedure

ProcedureDLL.s ExecuteProgram(ExeName$, Parameter$, Hide) ; Retourne le résultat du programme, Si hide=1 le programme sera caché et affiché sinon
If Hide : AF= #SW_HIDE : Else : AF= #SW_SHOW : EndIf
mCommand.s = ExeName$
If Parameter$
mCommand+ " " +Parameter$
EndIf
start.MySTARTUPINFO
sa.SECURITY_ATTRIBUTES
lngBytesread.l
*strBuff = AllocateMemory (1024)
sa\nLength = SizeOf(SECURITY_ATTRIBUTES)
sa\bInheritHandle = 1
sa\lpSecurityDescriptor = 0
If CreatePipe_ (@hReadPipe, @hWritePipe, @sa, 0)=0
FreeMemory (*strBuff)
Error( "Unable to connect with " +ExeName$, 0)
ProcedureReturn ""
EndIf
start\cb = SizeOf(MySTARTUPINFO)
start\dwFlags = #STARTF_USESTDHANDLES|#STARTF_USESHOWWINDOW
start\wShowWindow = AF
start\hStdOutput = hWritePipe
start\hStdError = hWritePipe
Folder$ = GetExePath()
If CreateProcess_ (0, mCommand, sa, sa, 1, #NORMAL_PRIORITY_CLASS , 0, @Folder$, @start, @proc)<>#True
FreeMemory (*strBuff)
CloseHandle_ (hWritePipe)
CloseHandle_ (hReadPipe)
Error(mCommand+ " not found." , 0)
ProcedureReturn ""
EndIf
CloseHandle_ (hWritePipe)
mOutputs.s = ""
While ReadFile_ (hReadPipe, *strBuff, 1023, @lngBytesread, 0)<>0
If lngBytesread
mOutputs+ PeekS (*strBuff, lngBytesread)
EndIf
Wend
FreeMemory (*strBuff)
CloseHandle_ (proc\hProcess)
CloseHandle_ (proc\hThread)
CloseHandle_ (hReadPipe)
ProcedureReturn mOutputs
EndProcedure
Avatar de l’utilisateur
flaith
Messages : 1487
Inscription : jeu. 07/avr./2005 1:06
Localisation : Rennes
Contact :

Message par flaith »

J'avais fait un programme, je pense equivalent à ta demande, ICI : ca récupère les infos venant de la console !!
nico
Messages : 3702
Inscription : ven. 13/févr./2004 0:57

Message par nico »

Le programme ne fonctionne pas! :(
Avatar de l’utilisateur
flaith
Messages : 1487
Inscription : jeu. 07/avr./2005 1:06
Localisation : Rennes
Contact :

Message par flaith »

Lequel ?
nico
Messages : 3702
Inscription : ven. 13/févr./2004 0:57

Message par nico »

Celui de Gilou, le tiens faut GCC alors ...
Gillou
Messages : 373
Inscription : sam. 28/août/2004 17:35
Localisation : Bretagne, 22
Contact :

Message par Gillou »

nico a écrit :Celui de Gilou, le tiens faut GCC alors ...
Il se passe quoi?

ex :
Debug ExecuteProgram("ping.exe","forum.purebasic.fr",0)
Avatar de l’utilisateur
flaith
Messages : 1487
Inscription : jeu. 07/avr./2005 1:06
Localisation : Rennes
Contact :

Message par flaith »

mais le gcc c'était pour mes tests a moi, dans la valeur commande, tu mets ce que tu veux, un "dir" par exemple

Code : Tout sélectionner

Procedure proc_Do(TheVal.l)
  Select TheVal
    Case 1
      AddGadgetItem(#Gadget_Form_String,-1, "Compiling...")
      Commande.s="type c:\whatever.txt"
      text.s = GetProgramResult(Commande)
      AddGadgetItem(#Gadget_Form_String,-1, text)
    Case 2 
      Commande.s="ping www.google.fr"
      text.s = GetProgramResult(Commande)
      AddGadgetItem(#Gadget_Form_String,-1, text)
    Case 3
      Commande.s="cmd /c dir *.exe" 
      text.s = GetProgramResult(Commande)
      AddGadgetItem(#Gadget_Form_String,-1, text)
  EndSelect
EndProcedure 
:?
Gillou
Messages : 373
Inscription : sam. 28/août/2004 17:35
Localisation : Bretagne, 22
Contact :

Message par Gillou »

J'ai regardé la commande de la droopy's lib, c'est quasiment pareil :


ProcedureDLL.s GetProgramResult(Command.s)
  
  proc.PROCESS_INFORMATION ;Process info filled by CreateProcessA
  ret.l ;long variable For get the Return value of the
  start.MySTARTUPINFO ;StartUp Info passed To the CreateProceeeA
  sa.SECURITY_ATTRIBUTES ;Security Attributes passeed To the
  hReadPipe.l ;Read Pipe handle created by CreatePipe
  hWritePipe.l ;Write Pite handle created by CreatePipe
  lngBytesread.l ;Amount of byte Read from the Read Pipe handle
  strBuff.s= Space (256) ;String buffer reading the Pipe
  
   ;Consts For functions
   #NORMAL_PRIORITY_CLASS = $20
   #STARTF_USESTDHANDLES = $100
   #STARTF_USESHOWWINDOW = $1
  
   ;Create the Pipe
  sa\nLength =SizeOf(SECURITY_ATTRIBUTES) ;Len(sa)
  sa\bInheritHandle = 1
  sa\lpSecurityDescriptor = 0
  ret = CreatePipe_ (@hReadPipe, @hWritePipe, @sa, 0)
   If ret = 0
     ;If an error occur during the Pipe creation exit
     MessageRequester ( "info" , "CreatePipe failed. Error: " ,0)
     End
   EndIf
  
  
  start\cb = SizeOf(MySTARTUPINFO)
  start\dwFlags = #STARTF_USESHOWWINDOW | #STARTF_USESTDHANDLES
  
   ;set the StdOutput And the StdError output To the same Write Pipe handle
  start\hStdOutput = hWritePipe
  start\hStdError = hWritePipe
  
   ;Execute the command
  ret = CreateProcess_ (0, Command, sa, sa, 1, #NORMAL_PRIORITY_CLASS , 0, 0, @start, @proc)
  
   If ret <> 1
    retour.s= ""
   Else
    
     ;Now We can ... must close the hWritePipe
    ret = CloseHandle_ (hWritePipe)
    
    mOutputs.s = ""
    
     ;Read the ReadPipe handle
     While ret<>0
      ret = ReadFile_ (hReadPipe, strBuff, 255, @lngBytesread, 0)
       If lngBytesread>0
        mOutputs = mOutputs + Left (strBuff, lngBytesread)
       EndIf
     Wend
    
     ;Close the opened handles
    ret = CloseHandle_ (proc\hProcess)
    ret = CloseHandle_ (proc\hThread)
    ret = CloseHandle_ (hReadPipe)
     ;ret=CloseHandle_(hWritePipe)
    
    retour.s=mOutputs
    
   EndIf
  
   ProcedureReturn ConformationAsciiEtenduVersAscii(mOutputs)
EndProcedure
Avatar de l’utilisateur
flaith
Messages : 1487
Inscription : jeu. 07/avr./2005 1:06
Localisation : Rennes
Contact :

Message par flaith »

c'est normal
flaith a écrit :ps: Il faut la lib de Droopy (merci à lui pour cette lib) :wink:
Sujet complet : http://purebasic.hmt-forum.com/viewtopi ... 0768#40768


:wink:
Dernière modification par flaith le mer. 11/janv./2006 23:10, modifié 1 fois.
nico
Messages : 3702
Inscription : ven. 13/févr./2004 0:57

Message par nico »

flaith,

merci mais ça m'embrouille ton prog avec compile, link.....


Gillou,
Debug ExecuteProgram("c:\console.exe","forum.purebasic.fr",0)

Là ça marche bien mais avec un prog fait avec Pure...???

Voici mon code test:

Code : Tout sélectionner

Parametre$ = ProgramParameter()

OpenConsole() 

PrintN("HeI_alle_!") 
PrintN("HeI_alle_!") 
PrintN("HeI_alle_!") 

PrintN("HeI_alle_!") 
PrintN("HeI_alle_!") 
PrintN("HeI_alle_!") 

Input()
CloseConsole()
La console plante avec cet exemple!
Avatar de l’utilisateur
flaith
Messages : 1487
Inscription : jeu. 07/avr./2005 1:06
Localisation : Rennes
Contact :

Message par flaith »

nico a écrit :flaith,

merci mais ça m'embrouille ton prog avec compile, link.....
Image
nico
Messages : 3702
Inscription : ven. 13/févr./2004 0:57

Message par nico »

ça plante aussi avec la lib droopy!
Avatar de l’utilisateur
Droopy
Messages : 1151
Inscription : lun. 19/juil./2004 22:31

Message par Droopy »

Il y a aussi cette procédure qui est utilisée :

Code : Tout sélectionner

Procedure.s ConformationAsciiEtenduVersAscii(Text.s)
  ReplaceString(Text,Chr(130),"é",2)
  ReplaceString(Text,Chr(135),"ç",2)
  ReplaceString(Text,Chr(131),"â",2)
  ReplaceString(Text,Chr(133),"à",2)
  ReplaceString(Text,Chr(136),"ê",2)
  ReplaceString(Text,Chr(137),"ë",2)
  ReplaceString(Text,Chr(138),"è",2)
  ReplaceString(Text,Chr(140),"î",2)
  ReplaceString(Text,Chr(150),"û",2)
  ReplaceString(Text,Chr(151),"ù",2)
  ReplaceString(Text,Chr(240),"-",2)
  ReplaceString(Text,Chr(242),"=",2)
  ReplaceString(Text,Chr(255)," ",2)
  
  ; Nettoie des caractères < 31
  For n=1 To 31
    If n=9 : Continue : EndIf
    If n=10 : Continue : EndIf ; LF fout la zone je le supprime
    ; If n=13 : Continue : EndIf 
    ReplaceString(Text,Chr(n),"",2)
  Next
  
  ProcedureReturn Text
EndProcedure
Qui permet de mettre en forme la chaine de sortie
Gillou
Messages : 373
Inscription : sam. 28/août/2004 17:35
Localisation : Bretagne, 22
Contact :

Message par Gillou »

Je corrige la commande, (c'est le input qui fait tout planter :roll: ) et je la reposte :)
Répondre