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 : 11647
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} LIBGRAPHC - DESSIN D'UI EN MODE CONSOLE
Voila, je poste ma librairie de fonction de dessin d'interface utilisateur en mode console parce que...

{C / C++ / C++.NET} ALGO RÉSOLUTION DE SUDOKU.
Bonjour, ben voici mon algo de résolution d'un sudoku que j'ai utiliser pour mon programme SudoBr...

{Python} CHANGEMENT DE CASSE
J'ai fait un petit programme en mode console pour un ami, qui renvoie dans un fichier texte toutes l...

{C / C++ / C++.NET} MORPION EN CONSOLE
Voici un programme de morpion en mode console. La plupart des programmes du même jeu que j'ai vu pr...

{PHP} WEBSH : WEB SHELL POUR ADMINISTRER UN SERVEUR LINUX SANS CLIENT SSH
Bonjour à tous, Oui je sais, certains vont hurler en entendant parler d'exécution de commandes pa...

{C / C++ / C++.NET} JEU D'ECHEC EN MODE CONSOLE
Cette application implémente le jeu d'echec en C et mode console. Il existe seulement le mode multij...

{C / C++ / C++.NET} MARIO SOKOBAN EN "C" CONSOLE
Voici un mario programmer en C console je tien a preciser que c'est pas moi qui la fait mes un amis ...

{PHP} PETIT CLIENT + AJAX + PHP
Permet d'executer des commandes via la commande shell_exec(), Les requettes qui sont envoyer via la ...

{Visual Basic, VB6, VB.NET, VB 2005} SEVENZIP CONSOLE
N'ayant pas trouvé de source exploitable avec le Framework 3.5 concernant l'utilisation de 7-zip, je...

{JAVA / J2EE} CONVERTIR ENTRE LES BASES 10,2,8 ET 16
ce code permet de convertir des valeurs en base donnée a une autre base aussi de chercher le code as...