| WindowProc |
|
|
| Hostnamen & -Adressen via Internet einlesen |
|
| Private Const MAX_QUERY_COUNT As Long = 4 |
| Private objQuery(1 To MAX_QUERY_COUNT) As clsQuery |
| Private lngNextIndex As Long |
| Private blnCancel As Boolean |
|
| Private Sub Form_Load() |
| Dim i As Long |
|
| For i = 1 To MAX_QUERY_COUNT |
| Set objQuery(i) = New clsQuery |
| Next i |
|
| Listview_AddItem "localhost" |
| Listview_AddItem "127.0.0.1" |
| Listview_AddItem "google.com" |
| Listview_AddItem "google.de" |
| Listview_AddItem "www.google.com" |
| Listview_AddItem "www.google.de" |
| Listview_AddItem "groups.google.com" |
| Listview_AddItem "groups.google.de" |
| Listview_AddItem "66.102.11.104" |
| Set lvwMain.SelectedItem = Nothing |
| End Sub |
|
| Private Sub Form_Unload(Cancel As Integer) |
| Dim i As Long |
| For i = 1 To MAX_QUERY_COUNT |
| Set objQuery(i) = Nothing |
| Next i |
| Set frmMain = Nothing |
| End Sub |
|
| Private Sub Form_SetCaption(strCaption As String) |
| If strCaption = "" Then |
| frmMain.Caption = "GetHostAsyn" |
| Else |
| frmMain.Caption = "GetHostAsyn (" & strCaption & ")" |
| End If |
| End Sub |
|
| 'Listview |
| Private Sub lvwMain_DblClick() |
| Call cmdCopy_Click |
| End Sub |
|
| Private Sub lvwMain_ItemClick(ByVal Item As MSComctlLib.ListItem) |
| cmdCopy.Enabled = Item.ForeColor = RGB(0, 128, 0) |
| End Sub |
|
| Private Sub lvwMain_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single) |
| Select Case Button |
| Case vbLeftButton, vbRightButton |
| If lvwMain.HitTest(x, y) Is Nothing Then |
| If Not lvwMain.SelectedItem Is Nothing Then |
| lvwMain.SelectedItem.Selected = False |
| Set lvwMain.SelectedItem = Nothing |
| End If |
| End If |
| End Select |
| End Sub |
|
| Private Function Listview_AddItem(strHost As String) As Boolean |
| Dim itmX As ListItem |
| Dim sKey As String |
| On Error Resume Next |
| sKey = "HOST_" & strHost |
| |
| Set itmX = lvwMain.ListItems.Add(, sKey, strHost) |
| |
| If Err.Number = 0 Then |
| itmX.SubItems(colHostname) = "-" |
| itmX.SubItems(colAliases) = "-" |
| itmX.SubItems(colAddrType) = "-" |
| itmX.SubItems(colAddrLength) = "-" |
| itmX.SubItems(colAddrList) = "-" |
| |
| itmX.Selected = False |
| |
| Listview_AddItem = True |
| Else |
| MsgBox "Fehler " & CStr(Err.Number) & ": " & Err.Description, _ |
| vbOKOnly Or vbExclamation |
| |
| Err.Clear |
| |
| Listview_AddItem = False |
| End If |
| End Function |
|
| 'Buttons |
| Private Sub cmdQuery_Click() |
| Dim itmX As ListItem |
| For Each itmX In lvwMain.ListItems |
| itmX.Selected = False |
| itmX.ForeColor = vbBlack |
| itmX.ListSubItems(colHostname).ForeColor = vbBlack |
| itmX.SubItems(colHostname) = "-" |
| itmX.ListSubItems(colAliases).ForeColor = vbBlack |
| itmX.SubItems(colAliases) = "-" |
| itmX.ListSubItems(colAddrType).ForeColor = vbBlack |
| itmX.SubItems(colAddrType) = "-" |
| itmX.ListSubItems(colAddrLength).ForeColor = vbBlack |
| itmX.SubItems(colAddrLength) = "-" |
| itmX.ListSubItems(colAddrList).ForeColor = vbBlack |
| itmX.SubItems(colAddrList) = "-" |
| Next itmX |
| |
| cmdQuery.Enabled = False |
| cmdCancel.Enabled = True |
| cmdCopy.Enabled = False |
| |
| blnCancel = False |
| lngNextIndex = 1 |
| |
| tmrQuery.Enabled = True |
| End Sub |
|
| Private Sub cmdCancel_Click() |
| blnCancel = True |
| ' |
| cmdCancel.Enabled = False |
| End Sub |
|
| Private Sub cmdCopy_Click() |
| Dim strClipboard As String |
| |
| If Not lvwMain.SelectedItem Is Nothing Then |
| With lvwMain.SelectedItem |
| strClipboard = "HOSTENT.h_name = " & .SubItems(colHostname) & vbCrLf & _ |
| "HOSTENT.h_aliases = " & .SubItems(colAliases) & vbCrLf & _ |
| "HOSTENT.h_addrtype = " & .SubItems(colAddrType) & vbCrLf & _ |
| "HOSTENT.h_length = " & .SubItems(colAddrLength) & vbCrLf & _ |
| "HOSTENT.h_addr_list = " & .SubItems(colAddrList) & vbCrLf |
| |
| Clipboard.SetText strClipboard, vbCFText |
| End With |
| End If |
| End Sub |
|
| 'Timer |
| Private Sub tmrQuery_Timer() |
| Dim lngFreeIndex As Long |
| Dim lngInUse As Long |
| Dim i As Long |
| |
| For i = 1 To MAX_QUERY_COUNT |
| If objQuery(i).InUse = True Then |
| lngInUse = lngInUse + 1 |
| Else |
| If lngFreeIndex < 1 Then |
| lngFreeIndex = i |
| End If |
| End If |
| Next i |
| |
| If ((lngNextIndex <= lvwMain.ListItems.Count) And (blnCancel = False)) Then |
| If lngFreeIndex > 0 Then |
| With lvwMain.ListItems(lngNextIndex) |
| .ForeColor = vbBlue |
|
| .ListSubItems(colHostname).ForeColor = vbBlue |
| .SubItems(colHostname) = "-" |
|
| .ListSubItems(colAliases).ForeColor = vbBlue |
| .SubItems(colAliases) = "-" |
|
| .ListSubItems(colAddrType).ForeColor = vbBlue |
| .SubItems(colAddrType) = "-" |
|
| .ListSubItems(colAddrLength).ForeColor = vbBlue |
| .SubItems(colAddrLength) = "-" |
|
| .ListSubItems(colAddrList).ForeColor = vbBlue |
| .SubItems(colAddrList) = "-" |
|
| .EnsureVisible |
|
| objQuery(lngFreeIndex).GetHostInfo .Text |
| End With |
|
| lngNextIndex = lngNextIndex + 1 |
| lngInUse = lngInUse + 1 |
| End If |
|
| Form_SetCaption CStr(lngInUse) & " von " & CStr(MAX_QUERY_COUNT) & " Abfragen aktiv" |
| Else |
| If lngInUse < 1 Then |
| tmrQuery.Enabled = False |
|
| cmdQuery.Enabled = True |
| cmdCancel.Enabled = False |
|
| Form_SetCaption IIf(blnCancel, "Abgebrochen", "Fertig") |
| Else |
| Form_SetCaption IIf(blnCancel, "Abbrechen", "Beende") & "; " & _ |
| CStr(lngInUse) & " von " & CStr(MAX_QUERY_COUNT) & " Abfragen aktiv" |
| End If |
| End If |
| End Sub |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Das steht im Modul: |
|
| Public colHost As New Collection ' collection of clsHost classes |
| Public lngResolveMessage As Long ' WindowProc resolve message |
| Public blnWinsockInit As Boolean ' is winsock already initialized ? |
|
| 'WindowProc |
| Public Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, _ |
| ByVal wParam As Long, ByVal lParam As Long) As Long |
| Dim objHost As clsHost |
|
| Debug.Print "hWnd = " & CStr(hWnd) & String$(12 - Len(CStr(hWnd)), " ") & _ |
| " (0x" & Right$(String$(8, "0") & UCase$(Hex$(hWnd)), 8) & ")" |
| Debug.Print "uMsg = " & CStr(uMsg) & String$(12 - Len(CStr(uMsg)), " ") & _ |
| " (0x" & Right$(String$(8, "0") & UCase$(Hex$(uMsg)), 8) & ")" |
| Debug.Print "wParam = " & CStr(wParam) & String$(12 - Len(CStr(wParam)), " ") & _ |
| " (0x" & Right$(String$(8, "0") & UCase$(Hex$(wParam)), 8) & ")" |
| Debug.Print "lParam = " & CStr(lParam) & String$(12 - Len(CStr(lParam)), " ") & _ |
| " (0x" & Right$(String$(8, "0") & UCase$(Hex$(lParam)), 8) & ")" |
| Debug.Print "----------------------------------------------------------------------------" |
|
| On Error Resume Next |
|
| Set objHost = colHost("HWND_" & CStr(hWnd)) |
|
| If Not objHost Is Nothing Then |
| WindowProc = objHost.IncommingMessage(hWnd, uMsg, wParam, lParam) |
| Else |
| WindowProc = 0 |
| End If |
| End Function |
|
|
| Das steht im Klassenmodul clsHost |
|
| 'Memory Management Functions |
| Private Declare Function GlobalAlloc Lib "kernel32" _ |
| (ByVal wFlags As Long, ByVal dwBytes As Long) As Long |
| Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long |
| Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long |
| Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long |
|
| 'Message and Message Queue Functions |
| Private Declare Function RegisterWindowMessage Lib "user32" _ |
| Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long |
|
| 'Runtime Library Routines |
| Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _ |
| (Destination As Any, ByVal Source As Long, ByVal Length As Long) |
|
| 'String Manipulation Functions |
| Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" _ |
| (ByVal lpString1 As String, ByVal lpString2 As Long) As Long |
| Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" _ |
| (ByVal lpString As Any) As Long |
|
| 'Socket Functions |
| Private Declare Function inet_addr Lib "ws2_32" (ByVal cp As String) As Long |
| Private Declare Function inet_ntoa Lib "ws2_32" (ByVal inn As Long) As Long |
| Private Declare Function WSAAsyncGetHostByAddr Lib "ws2_32" _ |
| (ByVal hWnd As Long, ByVal wMsg As Long, ByRef lngAddr As Long, _ |
| ByVal lngLenght As Long, ByVal lngType As Long, buf As Any, _ |
| ByVal lngBufLen As Long) As Long |
| Private Declare Function WSAAsyncGetHostByName Lib "ws2_32" (ByVal hWnd As Long, _ |
| ByVal wMsg As Long, ByVal strHostname As String, buf As Any, ByVal buflen As Long) As Long |
| Private Declare Function WSACleanup Lib "ws2_32" () As Long |
| Private Declare Function WSAStartup Lib "ws2_32" (ByVal wVersionRequested As Long, _ |
| lpWsaData As WSADATA) As Long |
|
| 'Window Functions |
| Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" _ |
| (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, _ |
| ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, _ |
| ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, _ |
| ByVal hInstance As Long, lpParam As Any) As Long |
| Private Declare Function DestroyWindow Lib "user32" (ByVal hWnd As Long) As Long |
| Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _ |
| (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long |
|
| 'Window Procedure Functions |
| Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _ |
| (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal uMsg As Long, _ |
| ByVal wParam As Long, ByVal lParam As Long) As Long |
|
| 'Events |
| Public Event HostResolved(ByVal Hostname As String, ByRef Aliases() As String, _ |
| ByVal AddrType As String, ByVal AddrLength As Long, ByRef AddrList() As String) |
| Public Event Error(ByVal Number As Integer, Description As String, ByVal Source As String) |
|
| 'Const |
| Private Const CLASS_NAME As String = "clsHost" ' name of this class |
| Private Const WSABASEERR As Long = 10000 ' Winsock Base Error |
|
| 'Windows Sockets definitions of regular Microsoft C error constants |
| Private Const WSAEINTR As Long = (WSABASEERR + 4) |
| Private Const WSAEBADF As Long = (WSABASEERR + 9) ' |
| Private Const WSAEACCES As Long = (WSABASEERR + 13) ' |
| Private Const WSAEFAULT As Long = (WSABASEERR + 14) ' |
| Private Const WSAEINVAL As Long = (WSABASEERR + 22) ' |
| Private Const WSAEMFILE As Long = (WSABASEERR + 24) ' |
|
| 'Windows Sockets definitions of regular Berkeley error constants |
| Private Const WSAEWOULDBLOCK As Long = (WSABASEERR + 35) |
| Private Const WSAEINPROGRESS As Long = (WSABASEERR + 36) ' |
| Private Const WSAEALREADY As Long = (WSABASEERR + 37) ' |
| Private Const WSAENOTSOCK As Long = (WSABASEERR + 38) ' |
| Private Const WSAEDESTADDRREQ As Long = (WSABASEERR + 39) ' |
| Private Const WSAEMSGSIZE As Long = (WSABASEERR + 40) ' |
| Private Const WSAEPROTOTYPE As Long = (WSABASEERR + 41) ' |
| Private Const WSAENOPROTOOPT As Long = (WSABASEERR + 42) ' |
| Private Const WSAEPROTONOSUPPORT As Long = (WSABASEERR + 43) ' |
| Private Const WSAESOCKTNOSUPPORT As Long = (WSABASEERR + 44) ' |
| Private Const WSAEOPNOTSUPP As Long = (WSABASEERR + 45) ' |
| Private Const WSAEPFNOSUPPORT As Long = (WSABASEERR + 46) ' |
| Private Const WSAEAFNOSUPPORT As Long = (WSABASEERR + 47) ' |
| Private Const WSAEADDRINUSE As Long = (WSABASEERR + 48) ' |
| Private Const WSAEADDRNOTAVAIL As Long = (WSABASEERR + 49) ' |
| Private Const WSAENETDOWN As Long = (WSABASEERR + 50) ' |
| Private Const WSAENETUNREACH As Long = (WSABASEERR + 51) ' |
| Private Const WSAENETRESET As Long = (WSABASEERR + 52) ' |
| Private Const WSAECONNABORTED As Long = (WSABASEERR + 53) ' |
| Private Const WSAECONNRESET As Long = (WSABASEERR + 54) ' |
| Private Const WSAENOBUFS As Long = (WSABASEERR + 55) ' |
| Private Const WSAEISCONN As Long = (WSABASEERR + 56) ' |
| Private Const WSAENOTCONN As Long = (WSABASEERR + 57) ' |
| Private Const WSAESHUTDOWN As Long = (WSABASEERR + 58) ' |
| Private Const WSAETOOMANYREFS As Long = (WSABASEERR + 59) ' |
| Private Const WSAETIMEDOUT As Long = (WSABASEERR + 60) ' |
| Private Const WSAECONNREFUSED As Long = (WSABASEERR + 61) ' |
| Private Const WSAELOOP As Long = (WSABASEERR + 62) ' |
| Private Const WSAENAMETOOLONG As Long = (WSABASEERR + 63) ' |
| Private Const WSAEHOSTDOWN As Long = (WSABASEERR + 64) ' |
| Private Const WSAEHOSTUNREACH As Long = (WSABASEERR + 65) ' |
| Private Const WSAENOTEMPTY As Long = (WSABASEERR + 66) ' |
| Private Const WSAEPROCLIM As Long = (WSABASEERR + 67) ' |
| Private Const WSAEUSERS As Long = (WSABASEERR + 68) ' |
| Private Const WSAEDQUOT As Long = (WSABASEERR + 69) ' |
| Private Const WSAESTALE As Long = (WSABASEERR + 70) ' |
| Private Const WSAEREMOTE As Long = (WSABASEERR + 71) ' |
|
| 'Extended Windows Sockets error constant definitions |
| Private Const WSASYSNOTREADY As Long = (WSABASEERR + 91) |
| Private Const WSAVERNOTSUPPORTED As Long = (WSABASEERR + 92) ' |
| Private Const WSANOTINITIALISED As Long = (WSABASEERR + 93) ' |
| Private Const WSAEDISCON As Long = (WSABASEERR + 101) ' |
| Private Const WSAENOMORE As Long = (WSABASEERR + 102) ' |
| Private Const WSAECANCELLED As Long = (WSABASEERR + 103) ' |
| Private Const WSAEINVALIDPROCTABLE As Long = (WSABASEERR + 104) ' |
| Private Const WSAEINVALIDPROVIDER As Long = (WSABASEERR + 105) ' |
| Private Const WSAEPROVIDERFAILEDINIT As Long = (WSABASEERR + 106) ' |
| Private Const WSASYSCALLFAILURE As Long = (WSABASEERR + 107) ' |
| Private Const WSASERVICE_NOT_FOUND As Long = (WSABASEERR + 108) ' |
| Private Const WSATYPE_NOT_FOUND As Long = (WSABASEERR + 109) ' |
| Private Const WSA_E_NO_MORE As Long = (WSABASEERR + 110) ' |
| Private Const WSA_E_CANCELLED As Long = (WSABASEERR + 111) ' |
| Private Const WSAEREFUSED As Long = (WSABASEERR + 112) ' |
|
| Private Const WSAHOST_NOT_FOUND As Long = (WSABASEERR + 1001) ' Authoritative Answer: Host not found |
| Private Const WSATRY_AGAIN As Long = (WSABASEERR + 1002) ' Non-Authoritative: Host not found, or SERVERFAIL |
| Private Const WSANO_RECOVERY As Long = (WSABASEERR + 1003) ' Non recoverable errors, FORMERR, REFUSED, NOTIMP |
| Private Const WSANO_DATA As Long = (WSABASEERR + 1004) ' Valid name, no data record of requested type |
|
| Private Const AF_UNSPEC As Long = 0 ' unspecified |
| Private Const AF_UNIX As Long = 1 ' local to host (pipes, portals) |
| Private Const AF_INET As Long = 2 ' internetwork: UDP, TCP, etc. |
| Private Const AF_IMPLINK As Long = 3 ' arpanet imp addresses |
| Private Const AF_PUP As Long = 4 ' pup protocols: e.g. BSP |
| Private Const AF_CHAOS As Long = 5 ' mit CHAOS protocols |
| Private Const AF_NS As Long = 6 ' XEROX NS protocols |
| Private Const AF_IPX As Long = AF_NS ' IPX protocols: IPX, SPX, etc. |
| Private Const AF_ISO As Long = 7 ' ISO protocols |
| Private Const AF_OSI As Long = AF_ISO ' OSI is ISO |
| Private Const AF_ECMA As Long = 8 ' european computer manufacturers |
| Private Const AF_DATAKIT As Long = 9 ' datakit protocols |
| Private Const AF_CCITT As Long = 10 ' CCITT protocols, X.25 etc |
| Private Const AF_SNA As Long = 11 ' IBM SNA |
| Private Const AF_DECnet As Long = 12 ' DECnet |
| Private Const AF_DLI As Long = 13 ' Direct data link interface |
| Private Const AF_LAT As Long = 14 ' LAT |
| Private Const AF_HYLINK As Long = 15 ' NSC Hyperchannel |
| Private Const AF_APPLETALK As Long = 16 ' AppleTalk |
| Private Const AF_NETBIOS As Long = 17 ' NetBios-style addresses |
| Private Const AF_VOICEVIEW As Long = 18 ' VoiceView |
| Private Const AF_FIREFOX As Long = 19 ' Protocols from Firefox |
| Private Const AF_UNKNOWN1 As Long = 20 ' Somebody is using this! |
| Private Const AF_BAN As Long = 21 ' Banyan |
| Private Const AF_ATM As Long = 22 ' Native ATM Services |
| Private Const AF_INET6 As Long = 23 ' Internetwork Version 6 |
| Private Const AF_CLUSTER As Long = 24 ' Microsoft Wolfpack |
| Private Const AF_12844 As Long = 25 ' IEEE 1284.4 WG AF |
|
| Private Const WS_VERSION_MINOR As Long = &H1& ' |
| Private Const WS_VERSION_MAJOR As Long = &H100& ' |
| Private Const WS_VERSION_REQD As Long = WS_VERSION_MINOR Or WS_VERSION_MAJOR |
|
| Private Const WSADESCRIPTION_LEN As Long = 256 ' WSADATA.szDescription length |
| Private Const WSASYS_STATUS_LEN As Long = 128 ' WSADATA.szSystemStatus length |
| Private Const MAXGETHOSTSTRUCT As Long = 1024 ' Define constant based on rfc883, used by gethostbyxxxx() calls |
| Private Const INADDR_NONE As Long = &HFFFFFFFF ' |
| Private Const GMEM_FIXED As Long = &H0 ' Allocates fixed memory. The return value is a pointer |
| Private Const GWL_WNDPROC As Long = &HFFFFFFFC ' Sets a new address for the window procedure |
|
| 'Types |
| Private Type WSADATA |
| wVersion As Integer |
| wHighVersion As Integer |
| szDescription(0 To WSADESCRIPTION_LEN) As Byte |
| szSystemStatus(0 To WSASYS_STATUS_LEN) 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 |
|
| 'Variables |
| Private lngWindowHandle As Long ' socket window handle |
| Private lngOldWindowProc As Long ' old WindowProc |
| Private lngRequestID As Long ' WSAAsyncGetHostBy Addr/Name requets id |
| Private lngMemoryHandle As Long ' handle of the allocated memory block object |
| Private lngMemoryPointer As Long ' address of the memory block |
|
| 'Class |
| Private Sub Class_Initialize() |
| If modWinsock.blnWinsockInit = False Then |
| Dim lpWsaData As WSADATA |
| Dim lngReturn As Long |
| ' |
| lngReturn = WSAStartup(WS_VERSION_REQD, lpWsaData) |
| If lngReturn = 0 Then |
| If modWinsock.lngResolveMessage = 0 Then |
| modWinsock.lngResolveMessage = RegisterWindowMessage("WSA.ResolveMessage") |
|
| Debug.Print "WSADATA.wVersion = " & Format(HiByte(lpWsaData.wVersion), "####0") & _ |
| "." & Format(LoByte(lpWsaData.wVersion), "####0") |
| Debug.Print "WSADATA.wHighVersion = " & Format(HiByte(lpWsaData.wHighVersion), "####0") & _ |
| "." & Format(LoByte(lpWsaData.wHighVersion), "####0") |
| Debug.Print "WSADATA.szDescription = " & StripNull(StrConv(lpWsaData.szDescription(), vbUnicode)) |
| Debug.Print "WSADATA.szSystemStatus = " & StripNull(StrConv(lpWsaData.szSystemStatus(), vbUnicode)) |
| Debug.Print "WSADATA.iMaxSockets = " & Format(lpWsaData.iMaxSockets, "###,###,###,##0") |
| Debug.Print "WSADATA.iMaxUdpDg = " & Format(lpWsaData.iMaxUdpDg And &HFF&, "###,###,###,##0") |
| Debug.Print "WSADATA.lpVendorInfo = " & Format(lpWsaData.lpVendorInfo, "###,###,###,##0") |
| Debug.Print "----------------------------------------------------------------------------" |
|
| Debug.Print "modWinsock.lngResolveMessage = " & CStr(modWinsock.lngResolveMessage) |
| Debug.Print "----------------------------------------------------------------------------" |
|
| End If |
| modWinsock.blnWinsockInit = True |
| Else |
| Err.Raise 2001, CLASS_NAME, "Die Funktion WSAStartup() ist fehlgeschlagen." |
| Exit Sub |
| End If |
| End If |
|
| If modWinsock.blnWinsockInit = True Then |
| lngWindowHandle = CreateWindowEx(0&, "STATIC", "SOCKET_WINDOW", _ |
| 0&, 0&, 0&, 0&, 0&, 0&, 0&, App.hInstance, ByVal 0&) |
|
| Debug.Print "lngWindowHandle = " & CStr(lngWindowHandle) |
|
| If lngWindowHandle = 0 Then |
| Err.Raise 2002, CLASS_NAME, "Die Funktion CreateWindowEx() ist fehlgeschlagen." |
| Exit Sub |
| End If |
|
| lngOldWindowProc = SetWindowLong(lngWindowHandle, GWL_WNDPROC, AddressOf modWinsock.WindowProc) |
|
| Debug.Print "lngOldWindowProc = " & CStr(lngOldWindowProc) |
| Debug.Print "----------------------------------------------------------------------------" |
|
| If lngOldWindowProc = 0 Then |
| Err.Raise 2003, CLASS_NAME, "Die Funktion SetWindowLong() ist fehlgeschlagen." |
| Exit Sub |
| End If |
| End If |
|
| modWinsock.colHost.Add Me, "HWND_" & CStr(lngWindowHandle) |
| End Sub |
|
| Private Sub Class_Terminate() |
| Dim lngReturn As Long |
| Dim i As Long |
|
| If lngWindowHandle <> 0 Then |
| If lngOldWindowProc <> 0 Then |
| lngReturn = SetWindowLong(lngWindowHandle, GWL_WNDPROC, lngOldWindowProc) |
| lngOldWindowProc = 0 |
| End If |
|
| lngReturn = DestroyWindow(lngWindowHandle) |
| lngWindowHandle = 0 |
| End If |
|
| For i = 1 To modWinsock.colHost.Count |
| If modWinsock.colHost(i) Is Me Then |
| modWinsock.colHost.Remove i |
| Exit For |
| End If |
| Next i |
|
| If ((modWinsock.blnWinsockInit = True) And (modWinsock.colHost.Count = 0)) Then |
| lngReturn = WSACleanup() |
|
| modWinsock.blnWinsockInit = False |
| End If |
| End Sub |
|
| 'GetHostInfo |
| Public Sub GetHostInfo(HostnameOrIP As String) |
| Dim lngAddr As Long |
|
| lngMemoryHandle = GlobalAlloc(GMEM_FIXED, MAXGETHOSTSTRUCT) |
| If lngMemoryHandle > 0 Then |
| lngMemoryPointer = GlobalLock(lngMemoryHandle) |
| If lngMemoryPointer <> 0 Then 'Unlock the memory block |
| GlobalUnlock (lngMemoryHandle) |
| Else 'Memory allocation error |
| Call GlobalFree(lngMemoryHandle) |
| Err.Raise 2005, CLASS_NAME, "Die Funktion GlobalLock() ist fehlgeschlagen." |
| Exit Sub |
| End If |
| Else 'Memory allocation error |
| Err.Raise 2004, CLASS_NAME, "Die Funktion GlobalAlloc() ist fehlgeschlagen." |
| Exit Sub |
| End If |
|
| lngAddr = inet_addr(HostnameOrIP) |
|
| If lngAddr = INADDR_NONE Then |
| lngRequestID = WSAAsyncGetHostByName(lngWindowHandle, modWinsock.lngResolveMessage, _ |
| HostnameOrIP, ByVal lngMemoryPointer, MAXGETHOSTSTRUCT) |
| Else |
| lngRequestID = WSAAsyncGetHostByAddr(lngWindowHandle, modWinsock.lngResolveMessage, _ |
| lngAddr, 4&, AF_INET, ByVal lngMemoryPointer, MAXGETHOSTSTRUCT) |
| End If |
|
| Debug.Print "lngRequestID = " & CStr(lngRequestID) |
| Debug.Print "----------------------------------------------------------------------------" |
|
| End Sub |
|
| 'Incoming Message |
| Public Function IncommingMessage(ByVal hWnd As Long, ByVal uMsg As Long, _ |
| ByVal wParam As Long, ByVal lParam As Long) As Long |
| Dim udtHostent As HOSTENT ' HOSTENT struct |
| Dim strHostname As String ' resolved hostname |
| Dim strAddrType As String ' address type |
| Dim lngAddrLength As Long ' address length |
| Dim strAliases() As String ' list of alternate hostnames (if any) |
| Dim lngAliasesCount As Long ' count of alternate hostnames |
| Dim lngAliasesPtr As Long ' pointer to an alternate hostname |
| Dim strAddrList() As String ' list of ip addresses for the host |
| Dim lngAddrListCount As Long ' count of ip addresses for the host |
| Dim lngAddrListPtr As Long ' pointer to an array of pointers with ip addresses for the host |
| Dim strAddr As String ' a address |
| Dim lngAddr As Long ' a address |
| Dim lngAddrPtr As Long ' a address pointer |
| Dim lngErrorCode As Long ' winsock error code |
|
| Select Case uMsg ' which message did we get |
| Case modWinsock.lngResolveMessage ' host resolved message ? |
| lngErrorCode = HiWord(lParam) ' get the error code |
|
| If lngErrorCode = 0 Then ' no error :) |
| lngMemoryPointer = GlobalLock(lngMemoryHandle) ' lock the memory an get a pointer to the first byte |
|
| Call CopyMemory(udtHostent, lngMemoryPointer, _ |
| LenB(udtHostent)) ' fill the hostent struct with the memory |
|
| strHostname = StrFromPtr(udtHostent.h_name) ' get the hostname from string pointer |
| strAddrType = GetAddressType(udtHostent.h_addrtype) ' get the address type |
| lngAddrLength = udtHostent.h_length ' get the address length |
|
| Do |
| Call CopyMemory(lngAliasesPtr, udtHostent.h_aliases, 4) ' get the pointer to the next alias name |
| If lngAliasesPtr <> 0 Then ' did we get a address ? |
| lngAliasesCount = lngAliasesCount + 1 ' increase alias count |
| ReDim Preserve strAliases(1 To lngAliasesCount) ' redim aliases array |
| strAliases(lngAliasesCount) = StrFromPtr(lngAliasesPtr) ' get the alias from string pointer and add it to the array |
| udtHostent.h_aliases = udtHostent.h_aliases + 4 ' address for the next pointer |
| End If |
| Loop While (lngAliasesPtr <> 0) ' loop while we get a pointer to an alias hostname |
|
| If lngAliasesCount < 1 Then ' no alias hostnames ? |
| ReDim strAliases(0 To 0) ' empty alias list (ubound = 0) |
| End If |
|
| If udtHostent.h_addrtype = AF_INET Then ' if address type is AF_INET |
| Do |
| Call CopyMemory(lngAddrListPtr, udtHostent.h_addr_list, 4) ' get the pointer to the next ip address |
| If lngAddrListPtr <> 0 Then ' did we get a address ? |
| Call CopyMemory(lngAddr, lngAddrListPtr, udtHostent.h_length) ' get the long ip addr from pointer |
|
| lngAddrPtr = inet_ntoa(lngAddr) ' get the pointer to the string ip address |
| strAddr = StrFromPtr(lngAddrPtr) ' get the ip address from string pointer |
|
| lngAddrListCount = lngAddrListCount + 1 ' increase the address count |
|