IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB 6 et antérieur Discussion :

[VB6] [Système] Récupérer le contenu d'une fenêtre DOS


Sujet :

VB 6 et antérieur

  1. #1
    Membre à l'essai
    Inscrit en
    Mars 2002
    Messages
    23
    Détails du profil
    Informations forums :
    Inscription : Mars 2002
    Messages : 23
    Points : 13
    Points
    13
    Par défaut [VB6] [Système] Récupérer le contenu d'une fenêtre DOS
    Ahahahaha, vous allez voir comme j'ai de belles questions...

    Je voudrait me faire un DOS évolué... et comme j'ai pas envie de me taper toutes les fonctions du DOS, j'aimerai juste faire un transfert de ma fenêtre DOS vers un TextBox par exemple :



    Et quand je fais Copy truc machin, qu'il aille discrètement demander au Dos de la faire...
    Si je fais Copy /?, par exemple, qu'il récupère la réponse du DOS et qu'il me l'affiche à la suite dans le TextBox...

    Je suis assez clair ???
    Pour résumer, je voudrais faire un Dos en me servant du DOS mais avec ma propre interface...
    Le miel, ça colle dans les poils, mais c'est pas graaaaaaaaaave !!!
    Pensez à mettre [Résolu] dans le sujet de votre message quand votre problème est arrangé...

  2. #2
    Futur Membre du Club
    Inscrit en
    Juin 2002
    Messages
    4
    Détails du profil
    Informations forums :
    Inscription : Juin 2002
    Messages : 4
    Points : 5
    Points
    5
    Par défaut
    très interessant comme projet, je veux bien savoir comment fait on pour intercepter les réponses windows moi aussi
    les codes erreurs ok mais les autres messages ...??

  3. #3
    Rédacteur
    Avatar de DarkVader
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2002
    Messages
    2 130
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2002
    Messages : 2 130
    Points : 3 118
    Points
    3 118
    Par défaut
    Voilà le genre de questions qui fait progresser le schmilblic.

    Continue,
    à défaut de réponse, quand la question est intelligente ...

  4. #4
    Membre averti Avatar de BakaOnigiri
    Inscrit en
    Avril 2002
    Messages
    366
    Détails du profil
    Informations forums :
    Inscription : Avril 2002
    Messages : 366
    Points : 437
    Points
    437
    Par défaut
    je sais comment le faire en delphi, çà t'intéresse ?

  5. #5
    Futur Membre du Club
    Inscrit en
    Juin 2002
    Messages
    4
    Détails du profil
    Informations forums :
    Inscription : Juin 2002
    Messages : 4
    Points : 5
    Points
    5
    Par défaut
    lordMantel : moi ca m'interesse, j'ai commencé à faire un tout petit peu de delphi

  6. #6
    Membre à l'essai
    Inscrit en
    Mars 2002
    Messages
    23
    Détails du profil
    Informations forums :
    Inscription : Mars 2002
    Messages : 23
    Points : 13
    Points
    13
    Par défaut
    Hey... Mais moi aussi ça m'interresse...
    Le miel, ça colle dans les poils, mais c'est pas graaaaaaaaaave !!!
    Pensez à mettre [Résolu] dans le sujet de votre message quand votre problème est arrangé...

  7. #7
    Membre averti Avatar de BakaOnigiri
    Inscrit en
    Avril 2002
    Messages
    366
    Détails du profil
    Informations forums :
    Inscription : Avril 2002
    Messages : 366
    Points : 437
    Points
    437
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    nit Unit1;
     
    //Lancer une application Dos et rediriger sa sortie standard vers l'application appelante
    //Marchioni Valérian - loub1@caramail.com - ICQ#: 30687888
    //20/03/2002
    //Sources : Bryan Ashby sur borland.public.delphi.winapi
    //ATTENTION: Ne gère pas les paramètres passés dans la ligne de commande
    //(pour ca, il faut écrire quelques routines qui traitent la ligne de commande pour en séparer le chemin et les paramètres)
     
     
    interface
     
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
     
    type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        Memo1: TMemo;
        Button2: TButton;
        Label1: TLabel;
        Label2: TLabel;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Déclarations privées }
      public
        { Déclarations publiques }
      end;
     
    var
      Form1: TForm1;
     
    implementation
     
    {$R *.dfm}
     
    function ExecAndGetConsoleOutput (const CommandLine : string;
      var Output : TStringList) : boolean;
    var
      Sa            : TSecurityAttributes;
      StartInfo     : TStartupInfo;
      ProcInfo      : TProcessInformation;
      StdOutFile,
      AppProc,
      AppThread     : LongWord;
      RootDir,
      WorkDir,
      StdOutFn      : string;
    begin
      Result := FileExists(ExtractFilePath (CommandLine) +
        ExtractFileName (CommandLine));
      if Result then
      begin
        StdOutFile := INVALID_HANDLE_VALUE;
        AppProc := INVALID_HANDLE_VALUE;
        AppThread := INVALID_HANDLE_VALUE;
     
        try
          RootDir := ExtractFilePath (ParamStr(0));
          WorkDir := ExtractFilePath (CommandLine);
     
          if not (FileSearch (ExtractFileName (CommandLine), WorkDir) <> '')
    then
            WorkDir := RootDir;
     
          FillChar (Sa, SizeOf(Sa), #0);
          Sa.nLength := SizeOf (Sa);
          Sa.lpSecurityDescriptor := nil;
          Sa.bInheritHandle := TRUE;
     
          StdOutFn := RootDir + '_tmpoutp.tmp';
          StdOutFile := CreateFile (PChar(StdOutFn), GENERIC_READ or
    GENERIC_WRITE,
            FILE_SHARE_READ or FILE_SHARE_WRITE, @Sa, CREATE_ALWAYS,
            FILE_ATTRIBUTE_TEMPORARY or FILE_FLAG_WRITE_THROUGH, 0);
     
          if StdOutFile <> INVALID_HANDLE_VALUE then
          begin
            FillChar (StartInfo, SizeOf(StartInfo), #0);
            with StartInfo do
            begin
              cb := SizeOf (StartInfo);
              dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
              wShowWindow := SW_HIDE;
              hStdInput := GetStdHandle (STD_INPUT_HANDLE);
              hStdError := StdOutFile;
              hStdOutput := stdOutFile;
            end;
     
            if CreateProcess (nil, PChar(CommandLine), nil, nil, TRUE, 0, nil,
              PChar(WorkDir), StartInfo, ProcInfo) then
            begin
              WaitForSingleObject (ProcInfo.hProcess, INFINITE);
              AppProc := ProcInfo.hProcess;
              AppThread := ProcInfo.hThread;
     
              CloseHandle (StdOutFile);
              StdOutFile := INVALID_HANDLE_VALUE;
     
              Output.Clear;
              Output.LoadFromFile (StdOutFn);
            end;
          end;
        finally
          if StdOutFile <> INVALID_HANDLE_VALUE then
            CloseHandle (StdOutFile);
          if AppProc <> INVALID_HANDLE_VALUE then
            CloseHandle (AppProc);
          if AppThread <> INVALID_HANDLE_VALUE then
            CloseHandle (AppThread);
     
          if FileExists (StdOutFn) then
            SysUtils.DeleteFile (StdOutFn);
        end;
      end;
    end;
     
    procedure TForm1.Button1Click(Sender: TObject);
    var sortie:TStringList;
    begin
    sortie:=TStringList.Create;
    Sortie.clear;
    Memo1.Lines.Clear;
    if ExecAndGetConsoleOutput(edit1.Text,Sortie)
      then memo1.Lines.Assign(sortie)
      else memo1.Lines.add('Erreur');
    sortie.free;
    end;
     
    procedure TForm1.Button2Click(Sender: TObject);
    begin
    close
    end;
     
    end.
    ou alors ici : http://phidels.com/php/zip/recupdos.zip qui vient du site : http://www.phidels.com/php/index

  8. #8
    Membre à l'essai
    Inscrit en
    Mars 2002
    Messages
    23
    Détails du profil
    Informations forums :
    Inscription : Mars 2002
    Messages : 23
    Points : 13
    Points
    13
    Par défaut
    Sinon, pas de solution en VB ???
    Le miel, ça colle dans les poils, mais c'est pas graaaaaaaaaave !!!
    Pensez à mettre [Résolu] dans le sujet de votre message quand votre problème est arrangé...

  9. #9
    Rédacteur

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2002
    Messages
    608
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mars 2002
    Messages : 608
    Points : 1 561
    Points
    1 561
    Par défaut
    Ce que nous a donné LordMantel me paraît traduisible en VB. J'essaie et je mets le code si ça marche.

  10. #10
    Membre à l'essai
    Inscrit en
    Mars 2002
    Messages
    23
    Détails du profil
    Informations forums :
    Inscription : Mars 2002
    Messages : 23
    Points : 13
    Points
    13
    Par défaut
    Merci NightFall...
    Le miel, ça colle dans les poils, mais c'est pas graaaaaaaaaave !!!
    Pensez à mettre [Résolu] dans le sujet de votre message quand votre problème est arrangé...

  11. #11
    Candidat au Club
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 2
    Points : 2
    Points
    2
    Par défaut
    Salut nounours666,

    Pourquoi tu ne fais pas tout simplement un truc du style :

    shell( chaine_saisie & ">out.txt" )
    puis tu recupere et affiche ce qu il y a dans le fichier out.txt ?

    Ca me parais assez simple ou peut etre je ne comprend pas la question !!!

    A +

  12. #12
    Membre à l'essai
    Inscrit en
    Mars 2002
    Messages
    23
    Détails du profil
    Informations forums :
    Inscription : Mars 2002
    Messages : 23
    Points : 13
    Points
    13
    Par défaut
    Ouais, j'y ai déjà pensé et je m'en servirai mais le but ultime, c'est de créé une émulation du DOS... Donc c'est encore plus profond...
    Le miel, ça colle dans les poils, mais c'est pas graaaaaaaaaave !!!
    Pensez à mettre [Résolu] dans le sujet de votre message quand votre problème est arrangé...

  13. #13
    Rédacteur

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2002
    Messages
    608
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mars 2002
    Messages : 608
    Points : 1 561
    Points
    1 561
    Par défaut
    Bon me revoila avec le code. Il faut mettre un textbox, un richtextbox (pour utiliser sa méthode LoadFile qui permet de charger un fichier), et un bouton.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, _
    ByVal lpCommandLine As String, lpProcessAttributes As Any, lpThreadAttributes As Any, _
    ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Long, _
    ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
     
    Private Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) As Long
    Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
     
    Private Const NORMAL_PRIORITY_CLASS = &H20
    Private Const STARTF_USESHOWWINDOW = &H1
    Private Const STARTF_USESTDHANDLES = &H100
    Private Const STD_INPUT_HANDLE = -10&
    Private Const INFINITE = &HFFFF
    Private Const INVALID_HANDLE_VALUE = -1
    Private Const GENERIC_READ = &H80000000
    Private Const GENERIC_WRITE = &H40000000
    Private Const FILE_SHARE_READ = &H1
    Private Const FILE_SHARE_WRITE = &H2
    Private Const CREATE_ALWAYS = 2
    Private Const FILE_ATTRIBUTE_TEMPORARY = &H100
    Private Const FILE_FLAG_WRITE_THROUGH = &H80000000
     
    Private Fichier As String
     
    Private Sub ExecuteCommande(commandLine As String)
     
    Dim startinfo As STARTUPINFO
    Dim procinfo As PROCESS_INFORMATION
    Dim stdOutFile As Long
     
    stdOutFile = CreateFile(Fichier, GENERIC_READ + GENERIC_WRITE, FILE_SHARE_READ + FILE_SHARE_WRITE, ByVal 0&, _
    CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY + FILE_FLAG_WRITE_THROUGH, 0)
     
    If stdOutFile <> INVALID_HANDLE_VALUE Then
        With startinfo
            .cb = Len(startinfo)
            .dwFlags = STARTF_USESHOWWINDOW + STARTF_USESTDHANDLES
            .hStdInput = GetStdHandle(STD_INPUT_HANDLE)
            .hStdError = stdOutFile
            .hStdOutput = stdOutFile
        End With
        If CreateProcess(vbNullString, "command.com /c" & commandLine, ByVal 0&, ByVal 0&, 1, NORMAL_PRIORITY_CLASS, 0&, vbNullString, startinfo, procinfo) Then
            WaitForSingleObject procinfo.hProcess, INFINITE
            CloseHandle stdOutFile
            CloseHandle procinfo.dwProcessId
            CloseHandle procinfo.dwThreadId
            If Dir(Fichier) <> "" Then
                txtResult.LoadFile Fichier, rtfText
                Kill Fichier
            End If
        Else
            CloseHandle stdOutFile
        End If
    End If
     
    End Sub
     
    Private Sub Command1_Click()
    ExecuteCommande Text1.Text
    End Sub
     
    Private Sub Form_Load()
    Fichier = "c:\result.txt"
    End Sub
    Je n'ai pas mis les déclarations des types STARTUPINFO, PROCESS_INFORMATION, et SECURITY_ATTRIBUTES pour une question de place. Vous les trouverez sur Internet, le forum, la visionneuse API, etc...
    Donc il faut taper la commande dans le textbox, par exemple "copy /?", cliquer sur le bouton et la sortie apparaît. Par contre gros problème si la commande attend à son tour que l'on tape une touche, par exemple si l'on utilise "/p", parce que le prog vb attend que l'exécution de la commande soit terminée, qui du coup ne l'est jamais. Mais on peut spécifier un nombre de millisecondes à WaitForSingleObject au lieu d'attendre indéfiniment comme ici.

  14. #14
    Membre à l'essai
    Inscrit en
    Mars 2002
    Messages
    23
    Détails du profil
    Informations forums :
    Inscription : Mars 2002
    Messages : 23
    Points : 13
    Points
    13
    Par défaut
    Merci NightFall, je regarde et on en reparle demain...
    Le miel, ça colle dans les poils, mais c'est pas graaaaaaaaaave !!!
    Pensez à mettre [Résolu] dans le sujet de votre message quand votre problème est arrangé...

  15. #15
    Rédacteur

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2002
    Messages
    608
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mars 2002
    Messages : 608
    Points : 1 561
    Points
    1 561
    Par défaut
    Citation Envoyé par Rico
    Pourquoi tu ne fais pas tout simplement un truc du style :
    shell( chaine_saisie & ">out.txt" )
    puis tu recupere et affiche ce qu il y a dans le fichier out.txt ?
    Ben c'est à peu près ce que fait le code de LordMantel (contrairement aux apparences ) sauf que l'exécution est lancée avec CreateProcess, et qu'il spécifie la redirection à CreateProcess, après avoir crée lui-même le fichier, au lieu de la mettre dans la ligne de commande.
    A part ça un shell avec une lecture du fichier juste après ne va pas parce que shell rend tout de suite la main sans attendre que la commande soit terminée.

    J'ai vu une variante où au lieu de rediriger vers un fichier on redirige vers un "Pipe" que l'on créé avec l'API CreatePipe.

  16. #16
    Nouveau Candidat au Club
    Inscrit en
    Octobre 2004
    Messages
    1
    Détails du profil
    Informations forums :
    Inscription : Octobre 2004
    Messages : 1
    Points : 1
    Points
    1
    Par défaut
    Salut,

    Désolé de remonter ce vieux sujet mais j'ai mis le code au-dessus et ça ne marche pas.
    Enfin à moitié, le fichier est bien créé, la commande se lance bien mais le fichier ne se remplit pas avec les infos de la commande.
    Et je n'arrive pas à trouver le problème.

    Et je voulais aussi savoir si le "command.com" est nécessaire ?
    "If CreateProcess(vbNullString, "command.com /c" & commandLine,..."
    parce que je suis sous XP et quand je lance la commande avec command.com devant, il me met un message d'erreur.

    Merci

  17. #17
    Membre habitué Avatar de Crisanar
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    162
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : Belgique

    Informations forums :
    Inscription : Novembre 2004
    Messages : 162
    Points : 137
    Points
    137
    Par défaut
    Je ne sais pas si c'est ca mais l'invite de commandes sous XP c'est cmd et non command.
    Et de plus pour ne pas passer par un fichier, il suffirait p-e d'utiliser les pipes.
    Je n'ai pas encore utilisé mais il ya justement un tuto d'Iczelion utilisant les pipes pour récupérer l'affichage d'une applic console. C'est orienté MASM mais les APIs sont les mm en ASM qu'en VB

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Récupérer le contenu d'une boite de dialogue Windows en VB6
    Par DWade dans le forum VB 6 et antérieur
    Réponses: 4
    Dernier message: 30/06/2006, 22h01
  2. [VB6]Comment centrer le contenu d'une cellule
    Par zackrel dans le forum VB 6 et antérieur
    Réponses: 15
    Dernier message: 19/05/2006, 20h02
  3. [VB6] Bloquer la modification dans une combo sans être en lecture seule..
    Par malhivertman1 dans le forum VB 6 et antérieur
    Réponses: 9
    Dernier message: 10/05/2006, 16h55
  4. Réponses: 2
    Dernier message: 05/10/2004, 22h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo