473,387 Members | 1,348 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to ping smtp server from vba in ms access?

374 256MB
I am sending semi automated emails within my access database using our companies internal smtp server.

One of the issues I had was capturing whether people were connected to the lan to be able to send these emails.

I decided to use a ping to test whether the server was giving a response, if it was then they must be connected and if not then they arent.

The ping code is as follows:

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. 'Ping function. Original Source code taken from www.allapi.com.
  3. 'Modified into a function and tested for VBA compatability by Jeminar 22May04
  4.  
  5.  
  6. Const SOCKET_ERROR = 0
  7. Private Type WSADATA
  8.     wVersion As Integer
  9.     wHighVersion As Integer
  10.     szDescription(0 To 255) As Byte
  11.     szSystemStatus(0 To 128) As Byte
  12.     iMaxSockets As Integer
  13.     iMaxUdpDg As Integer
  14.     lpVendorInfo As Long
  15. End Type
  16. Private Type Hostent
  17.     h_name As Long
  18.     h_aliases As Long
  19.     h_addrtype As Integer
  20.     h_length As Integer
  21.     h_addr_list As Long
  22. End Type
  23. Private Type IP_OPTION_INFORMATION
  24.     Ttl As Byte
  25.     Tos As Byte
  26.     Flags As Byte
  27.     OptionsSize As Long
  28.     OptionsData As String * 128
  29. End Type
  30. Private Type IP_ECHO_REPLY
  31.     Address(0 To 3) As Byte
  32.     status As Long
  33.     RoundTripTime As Long
  34.     DataSize As Integer
  35.     Reserved As Integer
  36.     Data As Long
  37.     Options As IP_OPTION_INFORMATION
  38. End Type
  39. Private Declare Function gethostbyname Lib "wsock32.dll" (ByVal hostname As String) As Long
  40. Private Declare Function WSAStartup Lib "wsock32.dll" (ByVal wVersionRequired&, lpWSADATA As WSADATA) As Long
  41. Private Declare Function WSACleanup Lib "wsock32.dll" () As Long
  42. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
  43. Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
  44. Private Declare Function IcmpCloseHandle Lib "icmp.dll" (ByVal HANDLE As Long) As Boolean
  45. Private Declare Function IcmpSendEcho Lib "ICMP" (ByVal IcmpHandle As Long, ByVal DestAddress As Long, ByVal RequestData As String, _
  46. ByVal RequestSize As Integer, RequestOptns As IP_OPTION_INFORMATION, ReplyBuffer As IP_ECHO_REPLY, ByVal ReplySize As Long, _
  47. ByVal TimeOut As Long) As Boolean
  48.  
  49.  
  50. Public Function Ping(ByVal hostname As String) As Long
  51.     Dim hFile As Long, lpWSADATA As WSADATA
  52.     Dim hHostent As Hostent, AddrList As Long
  53.     Dim Address As Long, rIP As String
  54.     Dim OptInfo As IP_OPTION_INFORMATION
  55.     Dim EchoReply As IP_ECHO_REPLY
  56.     Call WSAStartup(&H101, lpWSADATA)
  57.     If gethostbyname(hostname + String(64 - Len(hostname), 0)) <> SOCKET_ERROR Then
  58.         CopyMemory hHostent.h_name, ByVal gethostbyname(hostname + String(64 - Len(hostname), 0)), Len(hHostent)
  59.         CopyMemory AddrList, ByVal hHostent.h_addr_list, 4
  60.         CopyMemory Address, ByVal AddrList, 4
  61.     End If
  62.     hFile = IcmpCreateFile()
  63.     If hFile = 0 Then
  64.         MsgBox "Unable to Create File Handle"
  65.         Exit Function
  66.     End If
  67.     OptInfo.Ttl = 255
  68.     If IcmpSendEcho(hFile, Address, String(32, "A"), 32, OptInfo, EchoReply, Len(EchoReply) + 8, 2000) Then
  69.         rIP = CStr(EchoReply.Address(0)) + "." + CStr(EchoReply.Address(1)) + "." + _
  70. CStr(EchoReply.Address(2)) + "." + CStr(EchoReply.Address(3))
  71.     Else
  72.     End If
  73.     If EchoReply.status = 0 Then
  74.         Ping = EchoReply.RoundTripTime
  75.     Else
  76.         Ping = -1
  77.     End If
  78.     Call IcmpCloseHandle(hFile)
  79.     Call WSACleanup
  80. End Function
  81.  
  82.  
This worked perfeclty fine when I was working from home, the ping was not being returned so ofcourse the smtp server was unavailable.

I also tested the ping against different websites and I was returned pings with as expected latency values.

I have now come in to work and my code, even though connected to the network is returning 0 for the ping of the smtp.

This is causing my email sending script to believe that there is not a connection to the smtp server:

Expand|Select|Wrap|Line Numbers
  1. SMTPName = ELookup("[SettingValue]", "tblDBSettings", "[SettingName]='SMTPServer'")
  2.  
  3. 'Check to see if we are connected to the smtp server if not then exit this function
  4. If Ping(" & SMTPName & ") = -1 Then
  5. MsgBox "You are not connected to the network. For further help contact database admin.", vbInformation, "Error in Email"
  6. Exit Function
  7. Else
  8. SMTPUser = ELookup("[SettingValue]", "tblDBSettings", "[SettingName]='SMTPUser'")
  9. SMTPPass = ELookup("[SettingValue]", "tblDBSettings", "[SettingName]='SMTPPass'")
  10. End If
However as the above code shows I explicity state -1 value has to be returned. But for some reason 0 also causes the msgbox to pop up :|

I have tried pinging various websites using the code in the 1st part of this post and they all return -1, whereas they did not when I was at home. Any ideas why this might be happening?

=== Edit

After a bit more testing it seems the value returning 0 jsut means there is 0 latency due to the nserver being within my network:
Expand|Select|Wrap|Line Numbers
  1. ?GetHostNameFromIP("137.xxx.xx.101")
  2. NUTS1ETA
  3. ?GetHostNameFromIP("137.xxx.xx.83")
  4. w7bc-p1595.xxx.xxxxx.net
  5. ?ping("w7bc-p1595.xxx.xxxxx.net")
  6. output: 0
  7. ?ping("NUTS1ETA")
  8. output: 0
  9. ?ping("http://www.yahoo.com")
  10. output: -1 
  11. ?ping("yahoo.com")
  12. output: -1 
NUTS1ETA is the SMTP server where the full address is: NUTS1ETA.xxx.xxxxx.net
w7bc-p1595 is my computer. Both pings and lookups via IP cause a value of 0 to be returned. Weirdly though I do not know why I can not ping normal websites such as www.yahoo.com etc.. they all return a -1 yet when I am at home they return a value such as 40, 74, 88 etc etc which I assumed were milliseconds for the ping reply.
Mar 7 '11 #1

✓ answered by munkee

I forgot to update this thread with the working code. Note there are a quite a few functions in this.

The most basic is PingHost which I adapted to be more userfriendly than the Ping function. Just supply a host name and it will return the result.

To work out what the result translates in to use the constants at the top of the script. For example a ping of 11010 result would be Ip_status_base + 10 = ip request timed out

A result of 0 is a succesful ping and you know the connection is free and returning data.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4.  
  5. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  6. ' Copyright ©1996-2009 VBnet, Randy Birch, All Rights Reserved.
  7. ' Some pages may also contain other copyrights by the author.
  8. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  9. ' Distribution: You can freely use this code in your own
  10. '               applications, but you may not reproduce
  11. '               or publish this code on any web site,
  12. '               online service, or distribute as source
  13. '               on any media without express permission.
  14. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  15. Private Const IP_SUCCESS As Long = 0
  16. Private Const IP_STATUS_BASE As Long = 11000
  17. Private Const IP_BUF_TOO_SMALL As Long = (IP_STATUS_BASE + 1)
  18. Private Const IP_DEST_NET_UNREACHABLE As Long = (IP_STATUS_BASE + 2)
  19. Private Const IP_DEST_HOST_UNREACHABLE As Long = (IP_STATUS_BASE + 3)
  20. Private Const IP_DEST_PROT_UNREACHABLE As Long = (IP_STATUS_BASE + 4)
  21. Private Const IP_DEST_PORT_UNREACHABLE As Long = (IP_STATUS_BASE + 5)
  22. Private Const IP_NO_RESOURCES As Long = (IP_STATUS_BASE + 6)
  23. Private Const IP_BAD_OPTION As Long = (IP_STATUS_BASE + 7)
  24. Private Const IP_HW_ERROR As Long = (IP_STATUS_BASE + 8)
  25. Private Const IP_PACKET_TOO_BIG As Long = (IP_STATUS_BASE + 9)
  26. Private Const IP_REQ_TIMED_OUT As Long = (IP_STATUS_BASE + 10)
  27. Private Const IP_BAD_REQ As Long = (IP_STATUS_BASE + 11)
  28. Private Const IP_BAD_ROUTE As Long = (IP_STATUS_BASE + 12)
  29. Private Const IP_TTL_EXPIRED_TRANSIT As Long = (IP_STATUS_BASE + 13)
  30. Private Const IP_TTL_EXPIRED_REASSEM As Long = (IP_STATUS_BASE + 14)
  31. Private Const IP_PARAM_PROBLEM As Long = (IP_STATUS_BASE + 15)
  32. Private Const IP_SOURCE_QUENCH As Long = (IP_STATUS_BASE + 16)
  33. Private Const IP_OPTION_TOO_BIG As Long = (IP_STATUS_BASE + 17)
  34. Private Const IP_BAD_DESTINATION As Long = (IP_STATUS_BASE + 18)
  35. Private Const IP_ADDR_DELETED As Long = (IP_STATUS_BASE + 19)
  36. Private Const IP_SPEC_MTU_CHANGE As Long = (IP_STATUS_BASE + 20)
  37. Private Const IP_MTU_CHANGE As Long = (IP_STATUS_BASE + 21)
  38. Private Const IP_UNLOAD As Long = (IP_STATUS_BASE + 22)
  39. Private Const IP_ADDR_ADDED As Long = (IP_STATUS_BASE + 23)
  40. Private Const IP_GENERAL_FAILURE As Long = (IP_STATUS_BASE + 50)
  41. Private Const MAX_IP_STATUS As Long = (IP_STATUS_BASE + 50)
  42. Private Const IP_PENDING As Long = (IP_STATUS_BASE + 255)
  43. Private Const PING_TIMEOUT As Long = 500
  44. Private Const WS_VERSION_REQD As Long = &H101
  45. Private Const MIN_SOCKETS_REQD As Long = 1
  46. Private Const SOCKET_ERROR As Long = -1
  47. Private Const INADDR_NONE As Long = &HFFFFFFFF
  48. Private Const MAX_WSADescription As Long = 256
  49. Private Const MAX_WSASYSStatus As Long = 128
  50.  
  51. Private Type WSADATA
  52.    wVersion As Integer
  53.    wHighVersion As Integer
  54.    szDescription(0 To MAX_WSADescription) As Byte
  55.    szSystemStatus(0 To MAX_WSASYSStatus) As Byte
  56.    wMaxSockets As Long
  57.    wMaxUDPDG As Long
  58.    dwVendorInfo As Long
  59. End Type
  60.  
  61. Private Type ICMP_OPTIONS
  62.    Ttl             As Byte
  63.    Tos             As Byte
  64.    Flags           As Byte
  65.    OptionsSize     As Byte
  66.    OptionsData     As Long
  67. End Type
  68.  
  69. Private Type ICMP_ECHO_REPLY
  70.    Address         As Long
  71.    status          As Long
  72.    RoundTripTime   As Long
  73.    DataSize        As Long
  74.   'Reserved        As Integer
  75.    DataPointer     As Long
  76.    Options         As ICMP_OPTIONS
  77.    Data            As String * 250
  78. End Type
  79.  
  80. Private Declare Function gethostbyname Lib "wsock32" _
  81.   (ByVal hostname As String) As Long
  82.  
  83. Private Declare Sub CopyMemory Lib "kernel32" _
  84.    Alias "RtlMoveMemory" _
  85.   (xDest As Any, _
  86.    xSource As Any, _
  87.    ByVal nbytes As Long)
  88.  
  89. Private Declare Function lstrlenA Lib "kernel32" _
  90.   (lpString As Any) As Long
  91.  
  92. Private Declare Function WSAStartup Lib "wsock32" _
  93.    (ByVal wVersionRequired As Long, _
  94.     lpWSADATA As WSADATA) As Long
  95.  
  96. Private Declare Function WSACleanup Lib "wsock32" () As Long
  97.  
  98. Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
  99.  
  100. Private Declare Function IcmpCloseHandle Lib "icmp.dll" _
  101.    (ByVal IcmpHandle As Long) As Long
  102.  
  103. Private Declare Function IcmpSendEcho Lib "icmp.dll" _
  104.    (ByVal IcmpHandle As Long, _
  105.     ByVal DestinationAddress As Long, _
  106.     ByVal RequestData As String, _
  107.     ByVal RequestSize As Long, _
  108.     ByVal RequestOptions As Long, _
  109.     ReplyBuffer As ICMP_ECHO_REPLY, _
  110.     ByVal ReplySize As Long, _
  111.     ByVal TimeOut As Long) As Long
  112.  
  113. Private Declare Function inet_addr Lib "wsock32" _
  114.    (ByVal s As String) As Long
  115.  
  116. Private Declare Function inet_ntoa Lib "wsock32.dll" _
  117.   (ByVal addr As Long) As Long
  118.  
  119. Private Declare Function lstrcpyA Lib "kernel32" _
  120.   (ByVal RetVal As String, _
  121.    ByVal Ptr As Long) As Long
  122.  
  123.  
  124. Public Function pinghost(host As String) As String
  125.  
  126.    Dim ECHO As ICMP_ECHO_REPLY
  127.    Dim pos As Long
  128.    Dim success As Long
  129.    Dim sIPAddress As String
  130.  
  131.    If SocketsInitialize() Then
  132.      'convert the host name into an IP address
  133.       sIPAddress = GetIPFromHostName(host)
  134.      'ping the ip passing the address, text
  135.      'to use, and the ECHO structure
  136.       success = ping(sIPAddress, "Some text to send", ECHO)
  137.      'display the results
  138.     pinghost = success
  139.     SocketsCleanup
  140.    Else
  141.        Exit Function
  142.    End If
  143.  
  144. End Function
  145.  
  146.  
  147. Private Function ping(sAddress As String, _
  148.                       sDataToSend As String, _
  149.                       ECHO As ICMP_ECHO_REPLY) As Long
  150.   'If Ping fails .Status will be the error code
  151.    Dim hPort As Long
  152.    Dim dwAddress As Long
  153.  
  154.   'convert the address into a long representation
  155.    dwAddress = inet_addr(sAddress)
  156.  
  157.   'if dwAddress is valid
  158.    If dwAddress <> INADDR_NONE Then
  159.  
  160.      'open a port
  161.       hPort = IcmpCreateFile()
  162.  
  163.      'and if successful,
  164.       If hPort Then
  165.  
  166.         'ping it.
  167.          Call IcmpSendEcho(hPort, _
  168.                            dwAddress, _
  169.                            sDataToSend, _
  170.                            Len(sDataToSend), _
  171.                            0, _
  172.                            ECHO, _
  173.                            Len(ECHO), _
  174.                            PING_TIMEOUT)
  175.  
  176.         'return the status as ping success
  177.          ping = ECHO.status
  178.  
  179.         'close the port handle
  180.          Call IcmpCloseHandle(hPort)
  181.  
  182.       End If  'If hPort
  183.  
  184.    Else
  185.  
  186.         'the address format was probably invalid
  187.          ping = INADDR_NONE
  188.  
  189.    End If
  190.  
  191. End Function
  192.  
  193.  
  194.  
  195.  
  196.  
  197. Private Function GetIPFromHostName(ByVal sHostName As String) As String
  198.  
  199.   'converts a host name to an IP address
  200.    Dim ptrHosent As Long  'address of HOSENT structure
  201.    Dim ptrName As Long    'address of name pointer
  202.    Dim ptrAddress As Long 'address of address pointer
  203.    Dim ptrIPAddress As Long
  204.    Dim ptrIPAddress2 As Long
  205.  
  206.    ptrHosent = gethostbyname(sHostName & vbNullChar)
  207.  
  208.    If ptrHosent <> 0 Then
  209.  
  210.       ptrName = ptrHosent
  211.  
  212.       ptrAddress = ptrHosent + 12
  213.  
  214.      'get the IP address
  215.       CopyMemory ptrName, ByVal ptrName, 4
  216.       CopyMemory ptrAddress, ByVal ptrAddress, 4
  217.       CopyMemory ptrIPAddress, ByVal ptrAddress, 4
  218.       CopyMemory ptrIPAddress2, ByVal ptrIPAddress, 4
  219.  
  220.       GetIPFromHostName = GetInetStrFromPtr(ptrIPAddress2)
  221.  
  222.    End If
  223.  
  224. End Function
  225.  
  226.  
  227. Private Function GetStrFromPtrA(ByVal lpszA As Long) As String
  228.  
  229.    GetStrFromPtrA = String$(lstrlenA(ByVal lpszA), 0)
  230.    Call lstrcpyA(ByVal GetStrFromPtrA, ByVal lpszA)
  231.  
  232. End Function
  233.  
  234.  
  235. Private Function GetInetStrFromPtr(Address As Long) As String
  236.  
  237.    GetInetStrFromPtr = GetStrFromPtrA(inet_ntoa(Address))
  238.  
  239. End Function
  240.  
  241.  
  242. Private Sub SocketsCleanup()
  243.  
  244.    If WSACleanup() <> 0 Then
  245.        MsgBox "Windows Sockets error occurred in Cleanup.", vbExclamation
  246.    End If
  247.  
  248. End Sub
  249.  
  250.  
  251. Private Function SocketsInitialize() As Boolean
  252.  
  253.    Dim WSAD As WSADATA
  254.  
  255.    SocketsInitialize = WSAStartup(WS_VERSION_REQD, WSAD) = IP_SUCCESS
  256.  
  257. End Function
  258.  
  259.  

6 8148
Rabbit
12,516 Expert Mod 8TB
Ping(" & SMTPName & ")

should be

Ping("" & SMTPName & "")

I don't know why you are concatenating empty strings though.
Mar 7 '11 #2
NeoPa
32,556 Expert Mod 16PB
I couldn't find any valid link that the source came from. This is not really a VBA or Access issue. It's the code itself you're using. I would suggest going back to where you got it and seeing if there is any documentation on usage and interpretation of results. I believe you may be misinterpreting what you're getting, but I can't proceed further without access to the original pages.
Mar 7 '11 #3
munkee
374 256MB
As neopa suggested it is the code. I have been doing a bit more research around all of this and the problem was that the original code was adapted from some vbnet script. Atfer digging around I found some very similar code which needed to also be adapted (my first ever try) and discovered that the -1 and the 0 are not actually a ping as in ms returned values but they are codes.

There are around 15 different codes that can be returned where -1 is a socket error which means a connection can not even be made to the host/ip you are trying to ping.

0 is a succesful connection and everything else above 0 references another error code I think the highest is around 25.

Now moving on to my network when I run a ping command through the comamnd prompt I can not return a result. I think this is purely down to my network blocking outgoing pings or something along those lines. Luckily this is no issue for pinging servers currently within my domain area where the smtp server is

I will post up my adapted code when I am at work tomorrow which also can output other information regarding the ping such as the latency, ip address and traceroute.
Mar 7 '11 #4
NeoPa
32,556 Expert Mod 16PB
That sounds like a good result. Let us know if further help is sought after posting the code, or whether (as I think you're saying) the issue is now successfully resolved.
Mar 7 '11 #5
munkee
374 256MB
I forgot to update this thread with the working code. Note there are a quite a few functions in this.

The most basic is PingHost which I adapted to be more userfriendly than the Ping function. Just supply a host name and it will return the result.

To work out what the result translates in to use the constants at the top of the script. For example a ping of 11010 result would be Ip_status_base + 10 = ip request timed out

A result of 0 is a succesful ping and you know the connection is free and returning data.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4.  
  5. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  6. ' Copyright ©1996-2009 VBnet, Randy Birch, All Rights Reserved.
  7. ' Some pages may also contain other copyrights by the author.
  8. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  9. ' Distribution: You can freely use this code in your own
  10. '               applications, but you may not reproduce
  11. '               or publish this code on any web site,
  12. '               online service, or distribute as source
  13. '               on any media without express permission.
  14. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  15. Private Const IP_SUCCESS As Long = 0
  16. Private Const IP_STATUS_BASE As Long = 11000
  17. Private Const IP_BUF_TOO_SMALL As Long = (IP_STATUS_BASE + 1)
  18. Private Const IP_DEST_NET_UNREACHABLE As Long = (IP_STATUS_BASE + 2)
  19. Private Const IP_DEST_HOST_UNREACHABLE As Long = (IP_STATUS_BASE + 3)
  20. Private Const IP_DEST_PROT_UNREACHABLE As Long = (IP_STATUS_BASE + 4)
  21. Private Const IP_DEST_PORT_UNREACHABLE As Long = (IP_STATUS_BASE + 5)
  22. Private Const IP_NO_RESOURCES As Long = (IP_STATUS_BASE + 6)
  23. Private Const IP_BAD_OPTION As Long = (IP_STATUS_BASE + 7)
  24. Private Const IP_HW_ERROR As Long = (IP_STATUS_BASE + 8)
  25. Private Const IP_PACKET_TOO_BIG As Long = (IP_STATUS_BASE + 9)
  26. Private Const IP_REQ_TIMED_OUT As Long = (IP_STATUS_BASE + 10)
  27. Private Const IP_BAD_REQ As Long = (IP_STATUS_BASE + 11)
  28. Private Const IP_BAD_ROUTE As Long = (IP_STATUS_BASE + 12)
  29. Private Const IP_TTL_EXPIRED_TRANSIT As Long = (IP_STATUS_BASE + 13)
  30. Private Const IP_TTL_EXPIRED_REASSEM As Long = (IP_STATUS_BASE + 14)
  31. Private Const IP_PARAM_PROBLEM As Long = (IP_STATUS_BASE + 15)
  32. Private Const IP_SOURCE_QUENCH As Long = (IP_STATUS_BASE + 16)
  33. Private Const IP_OPTION_TOO_BIG As Long = (IP_STATUS_BASE + 17)
  34. Private Const IP_BAD_DESTINATION As Long = (IP_STATUS_BASE + 18)
  35. Private Const IP_ADDR_DELETED As Long = (IP_STATUS_BASE + 19)
  36. Private Const IP_SPEC_MTU_CHANGE As Long = (IP_STATUS_BASE + 20)
  37. Private Const IP_MTU_CHANGE As Long = (IP_STATUS_BASE + 21)
  38. Private Const IP_UNLOAD As Long = (IP_STATUS_BASE + 22)
  39. Private Const IP_ADDR_ADDED As Long = (IP_STATUS_BASE + 23)
  40. Private Const IP_GENERAL_FAILURE As Long = (IP_STATUS_BASE + 50)
  41. Private Const MAX_IP_STATUS As Long = (IP_STATUS_BASE + 50)
  42. Private Const IP_PENDING As Long = (IP_STATUS_BASE + 255)
  43. Private Const PING_TIMEOUT As Long = 500
  44. Private Const WS_VERSION_REQD As Long = &H101
  45. Private Const MIN_SOCKETS_REQD As Long = 1
  46. Private Const SOCKET_ERROR As Long = -1
  47. Private Const INADDR_NONE As Long = &HFFFFFFFF
  48. Private Const MAX_WSADescription As Long = 256
  49. Private Const MAX_WSASYSStatus As Long = 128
  50.  
  51. Private Type WSADATA
  52.    wVersion As Integer
  53.    wHighVersion As Integer
  54.    szDescription(0 To MAX_WSADescription) As Byte
  55.    szSystemStatus(0 To MAX_WSASYSStatus) As Byte
  56.    wMaxSockets As Long
  57.    wMaxUDPDG As Long
  58.    dwVendorInfo As Long
  59. End Type
  60.  
  61. Private Type ICMP_OPTIONS
  62.    Ttl             As Byte
  63.    Tos             As Byte
  64.    Flags           As Byte
  65.    OptionsSize     As Byte
  66.    OptionsData     As Long
  67. End Type
  68.  
  69. Private Type ICMP_ECHO_REPLY
  70.    Address         As Long
  71.    status          As Long
  72.    RoundTripTime   As Long
  73.    DataSize        As Long
  74.   'Reserved        As Integer
  75.    DataPointer     As Long
  76.    Options         As ICMP_OPTIONS
  77.    Data            As String * 250
  78. End Type
  79.  
  80. Private Declare Function gethostbyname Lib "wsock32" _
  81.   (ByVal hostname As String) As Long
  82.  
  83. Private Declare Sub CopyMemory Lib "kernel32" _
  84.    Alias "RtlMoveMemory" _
  85.   (xDest As Any, _
  86.    xSource As Any, _
  87.    ByVal nbytes As Long)
  88.  
  89. Private Declare Function lstrlenA Lib "kernel32" _
  90.   (lpString As Any) As Long
  91.  
  92. Private Declare Function WSAStartup Lib "wsock32" _
  93.    (ByVal wVersionRequired As Long, _
  94.     lpWSADATA As WSADATA) As Long
  95.  
  96. Private Declare Function WSACleanup Lib "wsock32" () As Long
  97.  
  98. Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
  99.  
  100. Private Declare Function IcmpCloseHandle Lib "icmp.dll" _
  101.    (ByVal IcmpHandle As Long) As Long
  102.  
  103. Private Declare Function IcmpSendEcho Lib "icmp.dll" _
  104.    (ByVal IcmpHandle As Long, _
  105.     ByVal DestinationAddress As Long, _
  106.     ByVal RequestData As String, _
  107.     ByVal RequestSize As Long, _
  108.     ByVal RequestOptions As Long, _
  109.     ReplyBuffer As ICMP_ECHO_REPLY, _
  110.     ByVal ReplySize As Long, _
  111.     ByVal TimeOut As Long) As Long
  112.  
  113. Private Declare Function inet_addr Lib "wsock32" _
  114.    (ByVal s As String) As Long
  115.  
  116. Private Declare Function inet_ntoa Lib "wsock32.dll" _
  117.   (ByVal addr As Long) As Long
  118.  
  119. Private Declare Function lstrcpyA Lib "kernel32" _
  120.   (ByVal RetVal As String, _
  121.    ByVal Ptr As Long) As Long
  122.  
  123.  
  124. Public Function pinghost(host As String) As String
  125.  
  126.    Dim ECHO As ICMP_ECHO_REPLY
  127.    Dim pos As Long
  128.    Dim success As Long
  129.    Dim sIPAddress As String
  130.  
  131.    If SocketsInitialize() Then
  132.      'convert the host name into an IP address
  133.       sIPAddress = GetIPFromHostName(host)
  134.      'ping the ip passing the address, text
  135.      'to use, and the ECHO structure
  136.       success = ping(sIPAddress, "Some text to send", ECHO)
  137.      'display the results
  138.     pinghost = success
  139.     SocketsCleanup
  140.    Else
  141.        Exit Function
  142.    End If
  143.  
  144. End Function
  145.  
  146.  
  147. Private Function ping(sAddress As String, _
  148.                       sDataToSend As String, _
  149.                       ECHO As ICMP_ECHO_REPLY) As Long
  150.   'If Ping fails .Status will be the error code
  151.    Dim hPort As Long
  152.    Dim dwAddress As Long
  153.  
  154.   'convert the address into a long representation
  155.    dwAddress = inet_addr(sAddress)
  156.  
  157.   'if dwAddress is valid
  158.    If dwAddress <> INADDR_NONE Then
  159.  
  160.      'open a port
  161.       hPort = IcmpCreateFile()
  162.  
  163.      'and if successful,
  164.       If hPort Then
  165.  
  166.         'ping it.
  167.          Call IcmpSendEcho(hPort, _
  168.                            dwAddress, _
  169.                            sDataToSend, _
  170.                            Len(sDataToSend), _
  171.                            0, _
  172.                            ECHO, _
  173.                            Len(ECHO), _
  174.                            PING_TIMEOUT)
  175.  
  176.         'return the status as ping success
  177.          ping = ECHO.status
  178.  
  179.         'close the port handle
  180.          Call IcmpCloseHandle(hPort)
  181.  
  182.       End If  'If hPort
  183.  
  184.    Else
  185.  
  186.         'the address format was probably invalid
  187.          ping = INADDR_NONE
  188.  
  189.    End If
  190.  
  191. End Function
  192.  
  193.  
  194.  
  195.  
  196.  
  197. Private Function GetIPFromHostName(ByVal sHostName As String) As String
  198.  
  199.   'converts a host name to an IP address
  200.    Dim ptrHosent As Long  'address of HOSENT structure
  201.    Dim ptrName As Long    'address of name pointer
  202.    Dim ptrAddress As Long 'address of address pointer
  203.    Dim ptrIPAddress As Long
  204.    Dim ptrIPAddress2 As Long
  205.  
  206.    ptrHosent = gethostbyname(sHostName & vbNullChar)
  207.  
  208.    If ptrHosent <> 0 Then
  209.  
  210.       ptrName = ptrHosent
  211.  
  212.       ptrAddress = ptrHosent + 12
  213.  
  214.      'get the IP address
  215.       CopyMemory ptrName, ByVal ptrName, 4
  216.       CopyMemory ptrAddress, ByVal ptrAddress, 4
  217.       CopyMemory ptrIPAddress, ByVal ptrAddress, 4
  218.       CopyMemory ptrIPAddress2, ByVal ptrIPAddress, 4
  219.  
  220.       GetIPFromHostName = GetInetStrFromPtr(ptrIPAddress2)
  221.  
  222.    End If
  223.  
  224. End Function
  225.  
  226.  
  227. Private Function GetStrFromPtrA(ByVal lpszA As Long) As String
  228.  
  229.    GetStrFromPtrA = String$(lstrlenA(ByVal lpszA), 0)
  230.    Call lstrcpyA(ByVal GetStrFromPtrA, ByVal lpszA)
  231.  
  232. End Function
  233.  
  234.  
  235. Private Function GetInetStrFromPtr(Address As Long) As String
  236.  
  237.    GetInetStrFromPtr = GetStrFromPtrA(inet_ntoa(Address))
  238.  
  239. End Function
  240.  
  241.  
  242. Private Sub SocketsCleanup()
  243.  
  244.    If WSACleanup() <> 0 Then
  245.        MsgBox "Windows Sockets error occurred in Cleanup.", vbExclamation
  246.    End If
  247.  
  248. End Sub
  249.  
  250.  
  251. Private Function SocketsInitialize() As Boolean
  252.  
  253.    Dim WSAD As WSADATA
  254.  
  255.    SocketsInitialize = WSAStartup(WS_VERSION_REQD, WSAD) = IP_SUCCESS
  256.  
  257. End Function
  258.  
  259.  
Apr 18 '11 #6
NeoPa
32,556 Expert Mod 16PB
I appreciate your setting my post as the best answer Munkee, but in this instance I doubt it will prove very helpful to others on a similar quest (I'm glad it helped you of course :-)). For that reason I'm going to reset the Best Answer. I suggest you go ahead and set your most recent post (#6) as the Best Answer instead. That has more useful information in it and is more likely to prove useful to searchers after similar material.
Apr 18 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Andy Turner | last post by:
Hi, I'm trying to setup PHP so it will use a remote SMTP server. I'm going to sound like a newbie but how? I know you need to alter php.ini, change SMTP from localhost to the SMTP server and the...
21
by: Nancy | last post by:
Hi, Guys, Is there any other way to use python or mod_python writing a web page? I mean, not use "form.py/email", no SMTP server. <form action="form.py/email" method="POST"> ... Thanks a lot. ...
6
by: M P | last post by:
Need help! Whats wrong with my code? My plan is this, I have my IIS5.0 on Server A and then I will use SMTP service of Server B. I already configured IIS to use Server B as its SMTP Server but I...
10
by: ahager via DotNetMonster.com | last post by:
I am receiving this messaage in a console application I wrote, I am authenticating, and my network admin swears their is nothing wrong with the smtp server. Any ideas? public void Mail_update()...
34
by: antonyliu2002 | last post by:
I've set up the virtual smtp server on my IIS 5.1 like so: 1. Assign IP address to "All Unassigned", and listen to port 25. 2. Access Connection granted to "127.0.0.1". 3. Relay only allow...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.