mrdumont
Messages postés
16
Date d'inscription
jeudi 20 juillet 2006
Statut
Membre
Dernière intervention
6 février 2008
13 janv. 2008 à 18:50
Oui avec la source original c'est toujours le meme probleme : le programme ne repond plus...
Voila la form du programme :
'Private MyDOS As DOSClass
Private WithEvents MyDOS As DOSClass
Private Sub CmdQuitter_Click()
Unload Me
End Sub
Private Sub Form_Load()
Set MyDOS = New DOSClass
End Sub
Private Sub Form_Unload(Cancel As Integer)
MyDOS.ClosedCommand
Set MyDOS = Nothing
End Sub
Private Sub MyDOS_ReceiveOutputs(CommandOutputs As String)
TextDos = TextDos & CommandOutputs
End Sub
Private Sub Command1_Click()
'Faire un dir de c: sur les environnement de type XP 2000
TextDos = ""
MyDOS.CommandLine = "nc 192.168.1.174 80"
MyDOS.ExecuteCommand
End Sub
Private Sub Command2_Click()
'Lancer la commande NET
TextDos = ""
MyDOS.CommandLine = "net.exe"
MyDOS.ExecuteCommand
End Sub
Private Sub Command3_Click()
'Lancer un ping
TextDos = ""
MyDOS.CommandLine = "Ping.exe 127.0.0.1"
MyDOS.ExecuteCommand
End Sub
Private Sub Command4_Click()
'Lancer un programme X
CommonDialog1.DialogTitle = "Choisir un programme de type 'dos'"
CommonDialog1.Filter = " Programmes |*.exe"
CommonDialog1.ShowOpen
If CommonDialog1.FileName <> "" Then
TextDos = ""
MyDOS.CommandLine = CommonDialog1.FileName
MyDOS.ExecuteCommand
End If
End Sub
(le fichier nc.exe se trouve dans le meme dossier que le programme )
Maintenant voila le Module du programme :
Option Explicit
'API CreatePipe permet de créer un "pipe" anonime,
'on récupére un handle pour lire et un pour ecrire.
Private Declare Function CreatePipe Lib "kernel32" _
(phReadPipe As Long, _
phWritePipe As Long, _
lpPipeAttributes As Any, _
ByVal nSize As Long) As Long
'Utiliser pour lire le "pipe" rempli par le process qui
'sera créé par l'API CretaProcessA
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
'Structure utilisée par l'API CreateProcessA
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
'Structure utilisée par l'API CreateProcessA
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
'Structure utilisée par l'API CreateProcessA
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
'Cette API lance un commande et renvoie les infos sur le process
'dans la structure PRECESS_INFORMATION
Private Declare Function CreateProcessA Lib "kernel32" _
(ByVal lpApplicationName As Long, _
ByVal lpCommandLine As String, _
lpProcessAttributes As SECURITY_ATTRIBUTES, _
lpThreadAttributes As SECURITY_ATTRIBUTES, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As Long, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long
'Cette API Permet de terminer prématurément un process KILLLL :)
Private Declare Function TerminateProcess Lib "kernel32" _
(ByVal hProcess As Long, _
ByVal uExitCode As Long) As Long
'Fermeture d'un handle
Private Declare Function CloseHandle Lib "kernel32" (ByVal hHandle As Long) As Long
'Constantes utilisée pour les API
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const STARTF_USESTDHANDLES = &H100&
Private Const STARTF_USESHOWWINDOW = &H1
Private mCommand As String 'Variable privée contenant la ligne de commande
Private mOutputs As String 'Variable privée pour la lecture du texte renvoié
Private ProcI As PROCESS_INFORMATION 'Process utilisé
Private HLecturePipe As Long 'Handle de lecture du "pipe"
Private HEcriturePipe As Long 'Handle d'écriture du "pipe"
'Evénement de reception de donnée de l'objet
Public Event ReceiveOutputs(CommandOutputs As String)
'------------------------------------------------------------
' Propriété publique qui permet de passer ou de lire la ligne
' de commande passer au module
'------------------------------------------------------------
Public Property Let CommandLine(DOSCommand As String)
mCommand = DOSCommand
End Property
Public Property Get CommandLine() As String
CommandLine = mCommand
End Property
'------------------------------------------------------------
' Propriété publique qui permet de lire la totalité des données
' reçues après l'exécution
'------------------------------------------------------------
Public Property Get Outputs()
Outputs = mOutputs
End Property
'------------------------------------------------------------
' Fonction publique qui lance l'éxécution de la ligne de commande
'------------------------------------------------------------
Public Function ExecuteCommand() As String
'Variable contenant le résultat des fonction API
Dim Result As Long
'Variable Structure utilisée par l'API CreateProcessA
Dim Start As STARTUPINFO
'Variable Structure utilisée par l'API CreateProcessA
Dim Sa As SECURITY_ATTRIBUTES
'Variable contenant le nombre d'octet lus dans le "pipe"
Dim LngOctetRec As Long
'Variable buffer de lecture du "pipe"
Dim strBuff As String * 256
'Ca c'est pour les couillons qui oublis de donner
'la commande avant de lancer l'exécution....
If Len(mCommand) = 0 Then
MsgBox "La commande à lancer n'a pas été renseignée!!!", vbCritical
Exit Function
End If
'Renseignement de la structure SECURITY_ATTRIBUTES
Sa.nLength = Len(Sa)
Sa.bInheritHandle = 1&
Sa.lpSecurityDescriptor = 0&
'Création du "Pipe" et Test du résultat
If CreatePipe(HLecturePipe, HEcriturePipe, Sa, 0) = 0 Then
'Si une erreur
MsgBox "Erreur de création du Pipe. Erreurr: " & Err.LastDllError, vbCritical
Exit Function
End If
'Renseignement de la structure STARTUPINFO
Start.cb = Len(Start)
Start.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
Start.hStdOutput = HEcriturePipe
Start.hStdError = HEcriturePipe
'Création du process = Exécution de la commande
If CreateProcessA(0&, mCommand, Sa, Sa, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, Start, ProcI) <> 1 Then
'Si une erreur, fermeture des Handles
Result = CloseHandle(HLecturePipe)
Result = CloseHandle(HEcriturePipe)
MsgBox "Fichier ou commande non trouvé.", vbCritical
Exit Function
End If
'Fermeture du "pipe" de sortie
Result = CloseHandle(HEcriturePipe)
mOutputs = ""
'Lecture du "pipe" en lecture pour récupérer les infos !
Do
Result = ReadFile(HLecturePipe, strBuff, 256, LngOctetRec, 0&)
mOutputs = mOutputs & Left(strBuff, LngOctetRec)
'Envoie les données au programme via l'événement
RaiseEvent ReceiveOutputs(Left(strBuff, LngOctetRec))
DoEvents
Loop While Result <> 0
'Fermeture de tous les Handles
Result = CloseHandle(ProcI.hProcess)
Result = CloseHandle(ProcI.hThread)
Result = CloseHandle(HLecturePipe)
'Renvoie du résultat
ExecuteCommand = mOutputs
End Function
Public Sub ClosedCommand()
'Variable contenant le résultat des fonction API
Dim Result As Long
'Force la fermeture du process en cours
TerminateProcess ProcI.hProcess, 0
'Fermeture de tous les Handles
Result = CloseHandle(ProcI.hProcess)
Result = CloseHandle(ProcI.hThread)
Result = CloseHandle(HLecturePipe)
End Sub
Le Server.bat qui attend une connecter est :
nc -l -p 80