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 : 8564
Catégorie(s) : API, Système
Langages dispo pour ce code :
- VB6
- C# 1.x, C# 2.x
- VB6



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 :).

Snippets en rapport avec : Console, Stdio



Codes sources en rapport avec : Console, Stdio

{Visual Basic, VB6, VB.NET, VB 2005} IP2COUNTRY, UN EXEMPLE D'IMPORT DE CSV VERS MYSQL, EN CONSOLE
Ce programme permet d'importer les données d'un fichier CSV (fichier texte, dont les champs sont sép...

{Visual Basic, VB6, VB.NET, VB 2005} LA CONSOLE WINDOWS, EN VB6
Ce petit projet montre l'utilisation de la console en VB6. Il gère les accents et les couleurs et e...

{C / C++ / C++.NET} THE HANGED
Le fameux jeu du Pendu écrit en C++. Un mode versus Ordinateur est disponible en plus du mode Joue...

{C / C++ / C++.NET} OBJET 3D EN CONSOLE
Ce programme permet de faire tourner un objet 3D, dessiné en ascii dans la console. Il est possib...

{Visual Basic, VB6, VB.NET, VB 2005} PIANO CONSOLE
Piano en mode Console c'est sympa !! Enfaite pour ce qui est des details j'ai utiliser la fonction ...

{Graphisme} FACADE NINTENDO GAME BOY
Pour les besoins d'un projet personnel, j'ai redessiné sous Illustrator cette facade de Game Boy (la...

{C# / C#.NET} CONSOLE PARAMÈTRABLE V1
Voici une classe qui peut rendre bien des services. C'est une console que discrète que se fondra da...

{Visual Basic, VB6, VB.NET, VB 2005} REDIRECTION D'UNE APPLICATION CONSOLE VERS UN TEXTBOX
Ce programme permet de rediriger une application console vers une fenetre graphique, le text est aff...

{C# / C#.NET} GENERATEUR DE LABYRINTHES ALEATOIRE
c'est ma premiere source en C#, j'avais fait un hello world ce matin, et quelqu'un sur le chat n'arr...

{Delphi} FTP CONSOLE
c'est un FTP console a base des composants OverByte - ICS v5 le but : c'est d'avoir un FTP en com...