Envoi de chaînes sur la console

Soyez le premier à donner votre avis sur cette source.

Snippet vu 6 239 fois - Téléchargée 74 fois

Contenu du snippet

Nix avait expliqué comment déclencher un programme DOS (Rubrique Shell : Envoyer une commande DOS). Voici la même chose, mais en beaucoup plus compliqué (et un peu plus puissant...).

Source / Exemple :


VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   975
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   3585
   LinkTopic       =   "Form1"
   ScaleHeight     =   975
   ScaleWidth      =   3585
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton Command1 
      Caption         =   "C'est parti !"
      Height          =   495
      Left            =   1200
      TabIndex        =   0
      Top             =   240
      Width           =   1215
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Declare Function AllocConsole Lib "kernel32" () As Long
Private Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) As Long
Private Declare Function WriteConsole Lib "kernel32" Alias "WriteConsoleA" (ByVal hConsoleOutput As Long, ByVal lpBuffer As String, ByVal nNumberOfCharsToWrite As Long, lpNumberOfCharsWritten As Long, lpReserved As Any) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function FreeConsole Lib "kernel32" () As Long

Private Const STD_OUTPUT_HANDLE = -11&

Private hConsole As Long

Private Sub Command1_Click()
'*** Pour lancer un application appelée Text.exe ou test.bat, par exemple
Dim app_name As String
Dim txt As String
Dim num_written As Long

    app_name = App.Path
    If Right$(app_name, 1) <> "\" Then app_name = app_name & "\"
    app_name = app_name & "test.bat"

    txt = "Alors, on y va ?" & vbCrLf
    WriteConsole hConsole, txt, Len(txt), num_written, vbNullString
    'enlever le commentaire à la suite pour lancer l'application....
    'Shell app_name
End Sub
Private Sub Form_Load()
Dim txt As String
Dim num_written As Long
    If AllocConsole() Then
        hConsole = GetStdHandle(STD_OUTPUT_HANDLE)
        If hConsole = 0 Then MsgBox "Impossible d'allouer STDOUT"

        ' Par sécurité, pour les neuneus....
        txt = "***********************************************" & vbCrLf & _
              "*   Attention, fermer l'appli VB en premier   *" & vbCrLf & _
              "***********************************************" & vbCrLf
        WriteConsole hConsole, txt, Len(txt), num_written, vbNullString

        ' Rend l'appli visible, au premier plan....
        Me.Show
        SetFocus
    Else
        MsgBox "Impossible d'allouer STDOUT"
    End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
    CloseHandle hConsole
    FreeConsole
End Sub

Conclusion :


Créez un fichier format texte, mettez le code dedans puis renommez le fichier en Form1.frm Ouvrez form1.frm avec VB 5 ou VB 6, sous 95 ou 98 (devrait pas avoir de problème en NT)... et cliquez.
Trouvez à quoi ca sert et me prévenir....

A voir également

Ajouter un commentaire

Commentaires

cs_Yesai
Messages postés
21
Date d'inscription
mardi 6 avril 2004
Statut
Membre
Dernière intervention
29 avril 2004
-
Renaud si tu pouvais m'expliquer comment le debut de ton programme fonctionne la partie qui commence par VERSION 5.00, mon visual basic 6.00 n'a pas l'aire de le reconnaitre
cs_Yesai
Messages postés
21
Date d'inscription
mardi 6 avril 2004
Statut
Membre
Dernière intervention
29 avril 2004
-
Renaud si tu pouvais m'expliquer comment le debut de ton programme fonctionne la partie qui commence par VERSION 5.00, mon visual basic 6.00 n'a pas l'aire de le reconnaitre
cs_DidierToulouse
Messages postés
1
Date d'inscription
mardi 23 décembre 2003
Statut
Membre
Dernière intervention
23 décembre 2003
-
Pas facile d'être Newbie...

J'ai fait ce qu'a dit le Monsieur, et quand je lance le build, j'obtiens :

C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(46): 'Version' is a type and cannot be used as an expression.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(47): Name 'Begin' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(47): Method arguments must be enclosed in parentheses.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(47): Name 'VB' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(47): Comma, ')', or a valid expression continuation expected.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(48): Name 'Caption' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(49): 'System.Windows.Forms.Control.clientHeight' is not accessible in this context because it is 'Private'.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(50): Name 'ClientLeft' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(51): Name 'ClientTop' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(52): 'System.Windows.Forms.Control.clientWidth' is not accessible in this context because it is 'Private'.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(53): Name 'LinkTopic' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(54): Name 'ScaleHeight' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(55): Name 'ScaleWidth' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(56): Name 'StartUpPosition' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(57): Name 'Begin' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(57): Method arguments must be enclosed in parentheses.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(57): Name 'VB' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(57): Comma, ')', or a valid expression continuation expected.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(58): Name 'Caption' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(66): 'Attribute' is a type and cannot be used as an expression.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(66): Name 'VB_Name' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(67): 'Attribute' is a type and cannot be used as an expression.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(67): Name 'VB_GlobalNameSpace' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(68): 'Attribute' is a type and cannot be used as an expression.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(68): Name 'VB_Creatable' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(69): 'Attribute' is a type and cannot be used as an expression.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(69): Name 'VB_PredeclaredId' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(70): 'Attribute' is a type and cannot be used as an expression.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(70): Name 'VB_Exposed' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(71): Statement is not valid inside a method.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(73): Keyword is not valid as an identifier.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(74): Keyword is not valid as an identifier.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(75): Keyword is not valid as an identifier.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(76): Keyword is not valid as an identifier.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(77): Keyword is not valid as an identifier.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(79): 'Private' is not valid on a local constant declaration.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(81): 'Private' is not valid on a local variable declaration.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(83): Statement cannot appear within a method body. End of method assumed.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(89): Name 'App' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(90): 'Public Overloads ReadOnly Property Right() As Integer' has no parameters and its return type cannot be indexed.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(90): Type character '$' does not match declared data type 'Integer'.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(94): Name 'WriteConsole' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(94): Name 'hConsole' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(101): Name 'AllocConsole' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(102): Name 'hConsole' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(102): Name 'GetStdHandle' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(102): Name 'STD_OUTPUT_HANDLE' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(103): Name 'hConsole' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(109): Name 'WriteConsole' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(109): Name 'hConsole' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(113): Name 'SetFocus' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(119): Name 'CloseHandle' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(119): Name 'hConsole' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(120): Name 'FreeConsole' is not declared.
C:Documents and SettingsDidier MorandiMes documentsVisual Studio ProjectsWindowsApplication3Form1.vb(123): 'End Sub' must be preceded by a matching 'Sub'.

Est-ce parce que j'utilise VB .Net au lieu de VB ?
Merci.

D.
Cyberdevil
Messages postés
483
Date d'inscription
mardi 10 juillet 2001
Statut
Membre
Dernière intervention
12 juillet 2006
-
excellent ! 10/10

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.