PDA

View Full Version : Ping in Visual Basic 5! HELP!


Uzi[WNCT]
24-02-2003, 18:08
nn ci riesco..... niente da fare..... grrrr... :mad:

plz, se qcno mi sa dire come si fa a fare un programma che restituisca il valore in ms del lag da un host, me lo dica.... nn so + che fare :p

Ho fatto un casino di prove, ma niente da fare..... ufff... :(

Mi servirebbe proprio na funzioncina del tipo:

ret = Ping(host)


e che in "ret" ci sia il valore in millisecondi.

HELP! :muro:

cionci
24-02-2003, 18:49
Preso da API-Guide :

Const SOCKET_ERROR = 0
Private Type WSAdata
wVersion As Integer
wHighVersion As Integer
szDescription(0 To 255) As Byte
szSystemStatus(0 To 128) As Byte
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As Long
End Type
Private Type Hostent
h_name As Long
h_aliases As Long
h_addrtype As Integer
h_length As Integer
h_addr_list As Long
End Type
Private Type IP_OPTION_INFORMATION
TTL As Byte
Tos As Byte
Flags As Byte
OptionsSize As Long
OptionsData As String * 128
End Type
Private Type IP_ECHO_REPLY
Address(0 To 3) As Byte
Status As Long
RoundTripTime As Long
DataSize As Integer
Reserved As Integer
data As Long
Options As IP_OPTION_INFORMATION
End Type
Private Declare Function GetHostByName Lib "wsock32.dll" Alias "gethostbyname" (ByVal HostName As String) As Long
Private Declare Function WSAStartup Lib "wsock32.dll" (ByVal wVersionRequired&, lpWSAdata As WSAdata) As Long
Private Declare Function WSACleanup Lib "wsock32.dll" () As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
Private Declare Function IcmpCloseHandle Lib "icmp.dll" (ByVal HANDLE As Long) As Boolean
Private Declare Function IcmpSendEcho Lib "ICMP" (ByVal IcmpHandle As Long, ByVal DestAddress As Long, ByVal RequestData As String, ByVal RequestSize As Integer, RequestOptns As IP_OPTION_INFORMATION, ReplyBuffer As IP_ECHO_REPLY, ByVal ReplySize As Long, ByVal TimeOut As Long) As Boolean
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Const HostName = "www.allapi.net"
Dim hFile As Long, lpWSAdata As WSAdata
Dim hHostent As Hostent, AddrList As Long
Dim Address As Long, rIP As String
Dim OptInfo As IP_OPTION_INFORMATION
Dim EchoReply As IP_ECHO_REPLY
Call WSAStartup(&H101, lpWSAdata)
If GetHostByName(HostName + String(64 - Len(HostName), 0)) <> SOCKET_ERROR Then
CopyMemory hHostent.h_name, ByVal GetHostByName(HostName + String(64 - Len(HostName), 0)), Len(hHostent)
CopyMemory AddrList, ByVal hHostent.h_addr_list, 4
CopyMemory Address, ByVal AddrList, 4
End If
hFile = IcmpCreateFile()
If hFile = 0 Then
MsgBox "Unable to Create File Handle"
Exit Sub
End If
OptInfo.TTL = 255
If IcmpSendEcho(hFile, Address, String(32, "A"), 32, OptInfo, EchoReply, Len(EchoReply) + 8, 2000) Then
rIP = CStr(EchoReply.Address(0)) + "." + CStr(EchoReply.Address(1)) + "." + CStr(EchoReply.Address(2)) + "." + CStr(EchoReply.Address(3))
Else
MsgBox "Timeout"
End If
If EchoReply.Status = 0 Then
MsgBox "Reply from " + HostName + " (" + rIP + ") recieved after " + Trim$(CStr(EchoReply.RoundTripTime)) + "ms"
Else
MsgBox "Failure ..."
End If
Call IcmpCloseHandle(hFile)
Call WSACleanup
End Sub

Uzi[WNCT]
24-02-2003, 18:54
GRAZIE! :D
Il problema lo avevo anche risolto. Ho trovato un listato che fa quello che mi serve. Perņ questo codice che hai postato č NETTAMENTE meglio.... adesso vedo cosa fare. :)

Adesso diciamo che ho una domanda "niubba".

Supponiamo che ho il mio programma "Pinger.exe".

Vorrei poterlo usare in questo modo (da "console"):

C:\> pinger 123.123.12.12
C:\> 65


Nel senso: passo un parametro dal prompt e sempre nel prompt ricevo l'output (che adesso ricevo con un MsgBox).

Qcno sa come fare? Nn dovrebbe essere difficile mi pare... tempo fa mi pareva di averlo fatto, ma nn trovo + niente..... :(

Uzi[WNCT]
24-02-2003, 20:26
Ho risolto l'input da console...... mi manca l'output del risultato..... :muro: :muro:

Uzi[WNCT]
25-02-2003, 17:10
.... pare che l'output nn sia facilmente fattibile :eek: ma che roba...

Un casino per fare un programma che faccia il ping e scriva SOLO il numero come risultato.... :( :muro:

cionci
25-02-2003, 18:10
Sai il VB non č espressamente pensato per queste cose...
Comunque potresti provare a lavorare con le API:

Private Const FOREGROUND_BLUE = &H1
Private Const FOREGROUND_GREEN = &H2
Private Const FOREGROUND_RED = &H4
Private Const BACKGROUND_BLUE = &H10
Private Const BACKGROUND_GREEN = &H20
Private Const BACKGROUND_RED = &H40
Private Const BACKGROUND_INTENSITY = &H80&
Private Const BACKGROUND_SEARCH = &H20&
Private Const FOREGROUND_INTENSITY = &H8&
Private Const FOREGROUND_SEARCH = (&H10&)
Private Const ENABLE_LINE_INPUT = &H2&
Private Const ENABLE_ECHO_INPUT = &H4&
Private Const ENABLE_MOUSE_INPUT = &H10&
Private Const ENABLE_PROCESSED_INPUT = &H1&
Private Const ENABLE_WINDOW_INPUT = &H8&
Private Const ENABLE_PROCESSED_OUTPUT = &H1&
Private Const ENABLE_WRAP_AT_EOL_OUTPUT = &H2&
Private Const STD_OUTPUT_HANDLE = -11&
Private Const STD_INPUT_HANDLE = -10&
Private Const STD_ERROR_HANDLE = -12&
Private Const INVALID_HANDLE_VALUE = -1&
Private Declare Function AllocConsole Lib "kernel32" () As Long
Private Declare Function FreeConsole Lib "kernel32" () As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) 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, lpBuffer As Any, ByVal nNumberOfCharsToWrite As Long, lpNumberOfCharsWritten As Long, lpReserved As Any) As Long
Private Declare Function ReadConsole Lib "kernel32" Alias "ReadConsoleA" (ByVal hConsoleInput As Long, ByVal lpBuffer As String, ByVal nNumberOfCharsToRead As Long, lpNumberOfCharsRead As Long, lpReserved As Any) As Long
Private Declare Function SetConsoleTextAttribute Lib "kernel32" (ByVal hConsoleOutput As Long, ByVal wAttributes As Long) As Long
Private Declare Function SetConsoleTitle Lib "kernel32" Alias "SetConsoleTitleA" (ByVal lpConsoleTitle As String) As Long
Private hConsoleOut As Long, hConsoleIn As Long, hConsoleErr As Long
Private Sub Form_Load()
'KPD-Team 2001
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
'Create console
If AllocConsole() Then
hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE)
If hConsoleOut = INVALID_HANDLE_VALUE Then MsgBox "Unable to get STDOUT"
hConsoleIn = GetStdHandle(STD_INPUT_HANDLE)
If hConsoleOut = INVALID_HANDLE_VALUE Then MsgBox "Unable to get STDIN"
Else
MsgBox "Couldn't allocate console"
End If
'Set the caption of the console window
SetConsoleTitle "The KPD-Team 2001"
'Set the background color of the text in the console to bright YELLOW text
'on a BLUE background
SetConsoleTextAttribute hConsoleOut, FOREGROUND_RED Or FOREGROUND_GREEN Or FOREGROUND_INTENSITY Or BACKGROUND_BLUE
'Write something in the console
ConsoleWriteLine "Hello World!"
ConsoleWrite "Please enter your name: "
'Ask for user input and show it in the caption
Me.Caption = "Your name: " + ConsoleReadLine()
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Delete console
CloseHandle hConsoleOut
CloseHandle hConsoleIn
FreeConsole
End Sub
Sub ConsoleWriteLine(sInput As String)
ConsoleWrite sInput + vbCrLf
End Sub
Sub ConsoleWrite(sInput As String)
Dim cWritten As Long
WriteConsole hConsoleOut, ByVal sInput, Len(sInput), cWritten, ByVal 0&
End Sub
Function ConsoleReadLine() As String
Dim ZeroPos As Long
'Create a buffer
ConsoleReadLine = String(10, 0)
'Read the input
ReadConsole hConsoleIn, ConsoleReadLine, Len(ConsoleReadLine), vbNull, vbNull
'Strip off trailing vbCrLf and Chr$(0)'s
ZeroPos = InStr(ConsoleReadLine, Chr$(0))
If ZeroPos > 0 Then ConsoleReadLine = Left$(ConsoleReadLine, ZeroPos - 3)
End Function

Uzi[WNCT]
25-02-2003, 18:17
Uhmmmm...... grazie ancora!

Adesso farņ una prova, ma mi sa che sto codice apre un'altra shell.. e nn scrive su quella di partenza..... boh... cmq lo provo! :)

ciauz

cionci
25-02-2003, 18:19
Allora prova senza AllocConsole...ma solamente con GetStdHandle(STD_OUTPUT_HANDLE)...

Uzi[WNCT]
25-02-2003, 18:20
difatti.... :( apre un'altra shell..... :cry:

Uzi[WNCT]
25-02-2003, 18:28
sembra nn funzionare..... mi sa che nn ha idea di dove scrivere..... :rolleyes: