Les Snippets

Connexion

Recuperer le text d'un programme console

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 26/03/2006 00:04:17 et initié par EBArtSoft [Liste]
Vue : 18051
Catégorie(s) : API, Système
Langages dispo pour ce code :
- VB6
- C# 1.x, C# 2.x
- VB6
- VB 2005, VB 2008



Langage : VB6
Date ajout : 26/03/2006
Posté par EBArtSoft [Liste]

Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const STARTF_USESTDHANDLES = &H100&
Private Const STARTF_USESHOWWINDOW = &H1
Private Const SW_HIDE = 0
      
Private Type SECURITY_ATTRIBUTES
    nLength                 As Long
    lpSecurityDescriptor    As Long
    bInheritHandle          As Long
End Type

Private Type STARTUPINFO
    cb                  As Long
    lpReserved          As Long
    lpDesktop           As Long
    lpTitle             As Long
    dwX                 As Long
    dwY                 As Long
    dwXSize             As Long
    dwYSize             As Long
    dwXCountChars       As Long
    dwYCountChars       As Long
    dwFillAttribute     As Long
    dwFlags             As Long
    wShowWindow         As Integer
    cbReserved2         As Integer
    lpReserved2         As Long
    hStdInput           As Long
    hStdOutput          As Long
    hStdError           As Long
End Type
      
Private Type PROCESS_INFORMATION
    hProcess            As Long
    hThread             As Long
    dwProcessId         As Long
    dwThreadID          As Long
End Type

Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, ByVal lpBuffer As String, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As Any, ByVal nSize As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function CreateProcessA Lib "kernel32" (ByVal lpApplicationName As Long, ByVal lpCommandLine As String, lpProcessAttributes As Any, lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, lpStartupInfo As Any, lpProcessInformation As Any) As Long

Public Function ShellEx(ByVal PathName As String, Optional ByVal WinStyle As VbAppWinStyle = vbHide) As String
    Dim proc            As PROCESS_INFORMATION
    Dim sa              As SECURITY_ATTRIBUTES
    Dim start           As STARTUPINFO
    Dim sBuffer         As String * 256
    Dim hReadPipe       As Long
    Dim hWritePipe      As Long
    Dim ret             As Long
    Dim lngBytesRead    As Long
    sa.nLength = Len(sa)
    sa.bInheritHandle = True
    ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)
    If (ret = 0) Then Exit Function
    start.cb = Len(start)
    start.wShowWindow = WinStyle
    start.hStdError = hWritePipe
    start.hStdOutput = hWritePipe
    start.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
    ret = CreateProcessA(0, PathName, sa, sa, True, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
    If (ret = 0) Then Exit Function
    CloseHandle hWritePipe
    Do
        ret = ReadFile(hReadPipe, sBuffer, Len(sBuffer), lngBytesRead, 0&)
        ShellEx = ShellEx & Left$(sBuffer, lngBytesRead)
    Loop While ret
    CloseHandle proc.hProcess
    CloseHandle proc.hThread
    CloseHandle hReadPipe
End Function


Langage : C# 1.x , C# 2.x
Date ajout : 27/03/2006
Posté par Lutinore [Liste]

public string StartProcess( string filename, string arguments )
{
    ProcessStartInfo info = new ProcessStartInfo( );
    info.FileName = filename;
    info.Arguments = arguments;
    info.UseShellExecute = false;
    info.RedirectStandardOutput = true;
    info.CreateNoWindow = true;

    string output = string.Empty;

    try
    {
        Process p = Process.Start( info );
        p.Start( );
        output = p.StandardOutput.ReadToEnd( );
        p.WaitForExit( /* 10000 */ );
        p.Close( );
    }
    catch ( Exception ex )
    {
        MessageBox.Show( ex.ToString( ) );
    }

    return output;
}

Langage : VB6
Date ajout : 02/10/2006
Posté par rvblog [Liste]
Private Sub Command1_Click()
Dim wshShell As IWshRuntimeLibrary.wshShell
Dim exExec As IWshRuntimeLibrary.WshExec
    'instancie un objet shell
    Set wshShell = New IWshRuntimeLibrary.wshShell
    'crée un objet exécution en ouvrant l'interprêteur
    'de commande DOS
    Set exExec = wshShell.Exec("cmd")
    'exécute la commande dir
    exExec.StdIn.WriteLine "dir"
    'ferme le canal
    exExec.StdIn.Close
    'lit toutes les lignes du canal de sortie
    strtest = exExec.StdOut.ReadAll
    'termine l'exécution
    exExec.Terminate
    MsgBox strtest
    'libère les ressources
    Set exExec = Nothing
    Set wshShell = Nothing
End Sub

Remarque :
Utilise une référence à l'objet Windows Scrip Host Object Model (wshom.ocx). Testé uniquement sur W2K.
Ne nécessite pas de connaissance particulière en API :).
Langage : VB 2005 , VB 2008
Date ajout : 23/12/2008
Posté par sturtrid [Liste]
Public Function StartProcess$(ByVal filename$, ByVal arguments$)
 
     Dim info As New ProcessStartInfo(filename, arguments)
     Dim output$ = String.Empty
 
     info.UseShellExecute = False
     info.RedirectStandardOutput = True
     info.CreateNoWindow = True
 
     Try
         Dim p As Process = Process.Start(info)
         output = p.StandardOutput.ReadToEnd
         p.WaitForExit(10000) : p.Close()
     Catch ex As Exception
         MessageBox.Show(ex.ToString)
     End Try
 
     Return output
 End Function
  

Snippets en rapport avec : Console, Stdio



Codes sources en rapport avec : Console, Stdio

{C / C++ / C++.NET} SHOP MANAGER CONSOLE SUR WINDOWS
Avec ce logiciel vous pouvez trier vos article dans divers enseigne que vous connaissez . Il est a...

{Visual Basic, VB6, VB.NET, VB 2005} CLONE/FORK DES FLUX DE LA CONSOLE : PERMETTRE LA REDIRECTION ET L'AFFICHAGE DANS LA CONSOLE
Ce code permet à une processus Console de garder l'affichage dans la Console tout en étant rediriger...

{Visual Basic, VB6, VB.NET, VB 2005} PATCHEUR DE FICHIER
Code permettant d'appliquer un patch a une position définie sur un fichier quelconque (Exe, Dll, txt...

{Python} JEU DU NOMBRE MYSTERE EN PYTHON
un petit jeu en mode console ou l'on doit décrouvrir un nombre grace à 3 indices a resoudre de tête ...

{C# / C#.NET} CLONE/FORK DES FLUX DE LA CONSOLE : PERMETTRE LA REDIRECTION ET L'AFFICHAGE DANS LA CONSOLE
Ce code permet à une processus Console de garder l'affichage dans la Console tout en étant rediriger...

{C# / C#.NET} (CONSOLE) TROUVER LA CLEF D'UN CODE INSEE EN DONNANT SES 13 PREMIERS CHIFFRES
Un code INSEE est composé de 15 caractères. Les 13 premiers sont directement liés à certains caract...

{C / C++ / C++.NET} CARNET D'ADRESSE MODE CONSOLE
Ce programme est un carnet d'adresse en mode console qui écrit dans un .txt les informations suivant...

{C / C++ / C++.NET} SNAKE CONSOLE
Dans le cadre du cours de correction de programme j'ai modifier la source suivante : http://www.c...

{C / C++ / C++.NET} SNAKE CONSOLE
Ce Snake en C++, développé en une ou deux heures avec Dev-C++, est relativement simpliste mais assez...

{C / C++ / C++.NET} BEJEWELED EN C
C'est le fameux jeu Bejeweled implémenté en c sous console. Vous allez trouvez un affichage en te...