Page 1 sur 1

[RESOLU]Récupérer la sortie d'un programme

Publié : lun. 14/janv./2008 21:57
par Progi1984
Bonjour,

Voilà, pour un programme, j'ai besoin de récupérer la sortie d'un programme lancé via RunProgram. Mais cela n'a pas l'air de fonctionner.

Voilà le code suivant utilise mc.exe (Message Compiler) de Microsoft SDK de Windows.

Code : Tout sélectionner

  SDKPath = "D:\Microsoft SDK\"
  Compil_MC = RunProgram(SDKPath + "Bin\mc", " /?",SDKPath+"Bin\",#PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
  Debug Compil_MC
  If Compil_MC 
    Debug "---"
    While ProgramRunning(Compil_MC)
      ReadMC.s = ReadProgramString(Compil_MC)
      If ReadMC <>""
        Debug "< "+ ReadMC
      EndIf
    Wend
    Debug "Exitcode: " + Str(ProgramExitCode(Compil_MC))
    Debug "---"
  EndIf
  CloseProgram(Compil_MC)
Et le problème, c'est que cela ne sort rien au lieu de sortir environ cela :

Code : Tout sélectionner

Microsoft (R) Message Compiler  Version 1.12.3790
Copyright (c) Microsoft Corporation. All rights reserved.

usage: MC [-?aAcdnosuUvw] [-m maxmsglen] [-h dirspec] [-e extension] [-r dirspec
] [-x dbgFileSpec] filename.mc
       -? - displays this message
       -a - input file is ANSI (default).
       -A - messages in .BIN file should be ANSI.
       -b - .BIN filename should have .mc filename_ included for uniqueness.
       -c - sets the Customer bit in all the message Ids.
       -d - FACILTY and SEVERITY values in header file in decimal.
            Sets message values in header to decimal initially.
       -e extension - Specify the extension for the header file.
                      From 1 - 3 chars.
       -h pathspec - gives the path of where to create the C include file
                     Default is .\
       -m maxmsglen - generate a warning if the size of any message exceeds
                      maxmsglen characters.
       -n - terminates all strings with null's in the message tables.
       -o - generate OLE2 header file (use HRESULT definition instead of
            status code definition)
       -r pathspec - gives the path of where to create the RC include file
                     and the binary message resource files it includes.
                     Default is .\
       -s - insert symbolic name as first line of each message.
       -u - input file is Unicode.
       -U - messages in .BIN file should be Unicode (default).
       -v - gives verbose output.
       -w - warns if message text contains non-OS/2 compatible inserts.
       -x pathspec - gives the path of where to create the .dbg C include
                        file that maps message Ids to their symbolic name.
       filename.mc - gives the names of a message text file
                     to compile.
       Generated files have the Archive bit cleared.
Merci d'avance.

Publié : jeu. 17/janv./2008 9:57
par brossden
Avec la Droopy's Lib tu as GetProgramResult !

Code : Tout sélectionner

result.s = GetProgramResult("cmd /c dir")
MessageRequester("Retour",result)

Publié : jeu. 17/janv./2008 18:34
par Gillou
Effectivement je viens de regarder et impossible d'obtenir le texte de retour du programme avec les commande PureBasic.

Et même en essayant de retourner le texte avec la commande "MC.exe /? >c:\retour.txt" en ligne de commande à partir du dossier, rien n'est retourné?

A croire que le retour n'est pas envoyé sur le canal de communication, mais uniquement en texte sur l'interface.

Avec ce code ça marche
Structure MySTARTUPINFO
    cb.l
    lpReserved.l
    lpDesktop.l
    lpTitle.l
    dwX.l
    dwY.l
    dwXSize.l
    dwYSize.l
    dwXCountChars.l
    dwYCountChars.l
    dwFillAttribute.l
    dwFlags.l
    wShowWindow.w
    cbReserved2.w
    lpReserved2.l
    hStdInput.l
    hStdOutput.l
    hStdError.l
EndStructure

ProcedureDLL.s GetExePath() ; Retourne l'adresse du programme
    ExeName.s = Space (255) : GetModuleFileName_ (0, @ExeName, 255)
     ProcedureReturn GetPathPart (ExeName)
EndProcedure

Global proc.PROCESS_INFORMATION, hReadPipe, hWritePipe
ProcedureDLL Error(message$, fatal) ; retourne 1 si la commande réussi
    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 : ProcedureReturn : EndIf
     ProcedureReturn 1
EndProcedure

ProcedureDLL.s ExecuteProgram(ExeName$, Parameter$, Hide = 0) ; 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

SDKPath.s = "C:\Program Files\Microsoft SDKs\Windows\v6.0\"
Compil_MC.s = ExecuteProgram(SDKPath + "Bin\mc.exe" , " /?" , 0)
Debug Compil_MC

Publié : ven. 18/janv./2008 11:07
par Progi1984
Yep, meme Freak a le probleme, et a reprise le bogue dans sa BugList :p

Yep, j'ai résolu le problème avec GetProgramResult.

Merci les gens.