Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Tracert

Posted on 1999-01-20
4
Medium Priority
?
518 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

772 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