Solved

Tracert

Posted on 1999-01-20
4
493 Views
Last Modified: 2012-08-13
In dos there is a command called "tracert" that is like a ping but shows all of the servers that it goes through. How can i do something like this within VB6?
0
Comment
Question by:Hanky368
  • 3
4 Comments
 
LVL 12

Accepted Solution

by:
mark2150 earned 100 total points
ID: 1469686
Use the SHELL command and redirect the output of the DOS version to a file, then display the file with VB6 - this is "quick & dirty" and has the advantage of working *today*.

TRACERT does low level packet stuff. It sends out a series of packets with increasing TTL (Time to Live or hop counts). When a packet is unrouted after it's hop count expires a "unable to connect" is sent back. The software times the delay from sending a packet to getting the "unable to connect"  and divides by two to get the hop delay. The TTL is advanced by one and the test repeats.

All of this requires fairly low level I/O transactions with the IP driver and you're going to have a difficult time getting VB5/6 to do this stuff as I/O details are, by design, hidden.

Ok, so answer is to use existing, known good, DOS programs to do the low level I/O and cr*p and to have VB just stand back and run things and handle the user GUI. Is good compromise as will work easy and fast, but it offends some "purists" that require everything be done "in" VB (as if the SHELL command is not part of VB?).

M

0
 

Author Comment

by:Hanky368
ID: 1469687
ok, so how do i do it?
0
 
LVL 12

Expert Comment

by:mark2150
ID: 1469688
Simple enough. First figure out correct use of command from DOS prompt and then use ExecCmd to give same command with output redirected to a file. SHELL is not appropriate as VB continues to run while SHELL is active. Since you want to wait for results you'll need ExecCmd instead:

'
' These data type declarations are required to support the obscure windows
' function to spawn a child task and wait for it to finish before continuing
'
Private Type STARTUPINFO
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    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 WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, _
    ByVal dwMilliseconds As Long) As Long
'
Private Declare Function CreateProcessA Lib "kernel32" ( _
    ByVal lpApplicationName As Long, _
    ByVal lpCommandLine As String, _
    ByVal lpProcessAttributes As Long, _
    ByVal lpThreadAttributes As Long, _
    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
'
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
'
Const NORMAL_PRIORITY_CLASS = &H20&
Const INFINITE = -1&
'
' I don't really understand the above declares, but they do work so DON'T MESS WITH THEM!
'
Public Sub ExecCmd(cmdline$)
'
' This executes an external windows program and waits for it to complete
'
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
'
' Initialize the STARTUPINFO structure:
'
start.cb = Len(start)
'
' Start the shelled application
'
ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
'
' Wait for the shelled application to finish:
'
holdhere:
    DoEvents
    ret& = WaitForSingleObject(proc.hProcess, 5000)
    If ret& <> 0 Then GoTo holdhere
'
ret& = CloseHandle(proc.hProcess)
End Sub

My local copy of TRACERT gives the following help:

Usage: tracert [-d] [-h maximum_hops] [-j host-list] [-w timeout] target_name

Options:
    -d                 Do not resolve addresses to hostnames.
    -h maximum_hops    Maximum number of hops to search for target.
    -j host-list       Loose source route along host-list.
    -w timeout         Wait timeout milliseconds for each reply.

Given that I'd code:

DIM Cmd AS STRING
TargetURL = "www.ntsc.navy.mil"

Cmd = "Tracert " & TargetURL & ">C:\ROUTE.TXT"
ExecCmd( Cmd )
'
' We only resume execution when ExecCmd is complete
'
OPEN "C:\ROUTE.TXT" for input as #1
Text1.Text = ""
DO WHILE NOT EOF(1)
    LINE INPUT #1, TEMP$
    Text1.Text = Text1.Text & TEMP$ & vbCRLF
LOOP
CLOSE #1

Anyway this may not be perfect, but it should get you real close. I'm assuming that you have a multi-line text box on your form.

M

0
 
LVL 12

Expert Comment

by:mark2150
ID: 1469689
PS You can adjust the Properties of TRACERT.EXE to RUN MINIMIZED and CLOSE ON EXIT. This will make it run in the background.

M

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Vb.net Listview 9 76
Write data from SQL to CSV file - position 1 45
Vb.net 2008 2 49
Add a task in Outlook from access 11 32
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now