Results 1 to 7 of 7

Thread: Running Ping Or Resolving Domains

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Posts
    3

    Running Ping Or Resolving Domains

    Hi,

    I need to right a module to resolve a domain name into a IP address.

    In the program, I have modified a script to allow the program to ping a specific IP address, but it would be nice if I could allow the user to give a domain instead.

    Also if I were to use the shell command to run the ping, how do I store the output into a string variable for displaying in a text box? I might want to incorporate a trace route feature and using the shell command would be the easiest method. In the past I have tried code like
    VB Code:
    1. TR = shell("tracert [url]www.google.com[/url]")
    but when I display the TR variable I just get a number.

    All Ideas much apreciated
    Thanx,
    Grey Fox

  2. #2
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424
    VB Code:
    1. 'Redirects output from console program to textbox.
    2. 'Requires two textboxes and one command button.
    3. 'Set MultiLine property of Text2 to true.
    4. '
    5. 'Original bcx version of this program was made by
    6. ' dl <dl@tks.cjb.net>
    7. 'VB port was made by Jernej Simoncic <jernej@isg.si>
    8. 'Visit Jernejs site at [url]http://www2.arnes.si/~sopjsimo/[/url]
    9. '
    10. 'Note: don't run plain DOS programs with this example
    11. 'under Windows 95,98 and ME, as the program freezes when
    12. 'execution of program is finnished.
    13.  
    14. Option Explicit
    15. Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, ByVal nSize As Long) As Long
    16. Private Declare Sub GetStartupInfo Lib "kernel32" Alias "GetStartupInfoA" (lpStartupInfo As STARTUPINFO)
    17. Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As Any, lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
    18. Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
    19. Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long
    20. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    21. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    22.  
    23. Private Type SECURITY_ATTRIBUTES
    24.   nLength As Long
    25.   lpSecurityDescriptor As Long
    26.   bInheritHandle As Long
    27. End Type
    28.  
    29. Private Type PROCESS_INFORMATION
    30.   hProcess As Long
    31.   hThread As Long
    32.   dwProcessId As Long
    33.   dwThreadId As Long
    34. End Type
    35.  
    36. Private Type STARTUPINFO
    37.   cb As Long
    38.   lpReserved As Long
    39.   lpDesktop As Long
    40.   lpTitle As Long
    41.   dwX As Long
    42.   dwY As Long
    43.   dwXSize As Long
    44.   dwYSize As Long
    45.   dwXCountChars As Long
    46.   dwYCountChars As Long
    47.   dwFillAttribute As Long
    48.   dwFlags As Long
    49.   wShowWindow As Integer
    50.   cbReserved2 As Integer
    51.   lpReserved2 As Byte
    52.   hStdInput As Long
    53.   hStdOutput As Long
    54.   hStdError As Long
    55. End Type
    56.  
    57. Private Type OVERLAPPED
    58.     ternal As Long
    59.     ternalHigh As Long
    60.     offset As Long
    61.     OffsetHigh As Long
    62.     hEvent As Long
    63. End Type
    64.  
    65. Private Const STARTF_USESHOWWINDOW = &H1
    66. Private Const STARTF_USESTDHANDLES = &H100
    67. Private Const SW_HIDE = 0
    68. Private Const EM_SETSEL = &HB1
    69. Private Const EM_REPLACESEL = &HC2
    70.  
    71. Private Sub Command1_Click()
    72.   Command1.Enabled = False
    73.   Redirect Text1.Text, Text2
    74.   Command1.Enabled = True
    75. End Sub
    76. Private Sub Form_Load()
    77.     Text1.Text = "ping"
    78. End Sub
    79. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    80.   If Command1.Enabled = False Then Cancel = True
    81. End Sub
    82.  
    83. Sub Redirect(cmdLine As String, objTarget As Object)
    84.   Dim i%, t$
    85.   Dim pa As SECURITY_ATTRIBUTES
    86.   Dim pra As SECURITY_ATTRIBUTES
    87.   Dim tra As SECURITY_ATTRIBUTES
    88.   Dim pi As PROCESS_INFORMATION
    89.   Dim sui As STARTUPINFO
    90.   Dim hRead As Long
    91.   Dim hWrite As Long
    92.   Dim bRead As Long
    93.   Dim lpBuffer(1024) As Byte
    94.   pa.nLength = Len(pa)
    95.   pa.lpSecurityDescriptor = 0
    96.   pa.bInheritHandle = True
    97.  
    98.   pra.nLength = Len(pra)
    99.   tra.nLength = Len(tra)
    100.  
    101.   If CreatePipe(hRead, hWrite, pa, 0) <> 0 Then
    102.     sui.cb = Len(sui)
    103.     GetStartupInfo sui
    104.     sui.hStdOutput = hWrite
    105.     sui.hStdError = hWrite
    106.     sui.dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
    107.     sui.wShowWindow = SW_HIDE
    108.     If CreateProcess(vbNullString, cmdLine, pra, tra, True, 0, Null, vbNullString, sui, pi) <> 0 Then
    109.       SetWindowText objTarget.hwnd, ""
    110.       Do
    111.         Erase lpBuffer()
    112.         If ReadFile(hRead, lpBuffer(0), 1023, bRead, ByVal 0&) Then
    113.           SendMessage objTarget.hwnd, EM_SETSEL, -1, 0
    114.           SendMessage objTarget.hwnd, EM_REPLACESEL, False, lpBuffer(0)
    115.           DoEvents
    116.         Else
    117.           CloseHandle pi.hThread
    118.           CloseHandle pi.hProcess
    119.           Exit Do
    120.         End If
    121.         CloseHandle hWrite
    122.       Loop
    123.       CloseHandle hRead
    124.     End If
    125.   End If
    126. End Sub

    Hope this will help u

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Posts
    3

    Talking

    Thanx very much!
    Thanx,
    Grey Fox

  4. #4
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424
    don't forget to add * resolved * to ur post subject. Cheers

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    what's wrong with 'traceroute google.com > file.txt' and then opening it? much easier


  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Posts
    3
    Hi,

    Using that code I can run commands perfectly, however when I try to ping or tracert a domain through it, using the domain name, I get a this host cannot be found error. It's strange, it works with the IP.

    Any ideas?
    Thanx,
    Grey Fox

  7. #7
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253
    DNS resolution, perhaps?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width