Solved

Get fully qualified domain name

Posted on 2000-04-03
15
561 Views
Last Modified: 2008-03-03
How do I get the fully qualified domain name of a Windows9x/NT machine?  I have gotten some code from the Microsoft Knowledge Base, but, it only gives the host name of the PC, not the IP domain as well.

Any help would be most appreciated.

Thank you for your time.

--

Sincerely,

Don
0
Comment
Question by:dcordner
  • 8
  • 5
  • 2
15 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2681554
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2681675
Place the following code in the module:

Option Explicit

Public Const MAX_WSADescription = 256
Public Const MAX_WSASYSStatus = 128
Public Const ERROR_SUCCESS       As Long = 0
Public Const WS_VERSION_REQD     As Long = &H101
Public Const WS_VERSION_MAJOR    As Long = WS_VERSION_REQD \ &H100 And &HFF&
Public Const WS_VERSION_MINOR    As Long = WS_VERSION_REQD And &HFF&
Public Const MIN_SOCKETS_REQD    As Long = 1
Public Const SOCKET_ERROR        As Long = -1

Public Type HOSTENT
   hName      As Long
   hAliases   As Long
   hAddrType  As Integer
   hLen       As Integer
   hAddrList  As Long
End Type

Public Type WSADATA
   wVersion      As Integer
   wHighVersion  As Integer
   szDescription(0 To MAX_WSADescription)   As Byte
   szSystemStatus(0 To MAX_WSASYSStatus)    As Byte
   wMaxSockets   As Integer
   wMaxUDPDG     As Integer
   dwVendorInfo  As Long
End Type

Public Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long

Public Declare Function WSAStartup Lib "WSOCK32.DLL" _
   (ByVal wVersionRequired As Long, lpWSADATA As WSADATA) As Long
   
Public Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long

Public Declare Function gethostname Lib "WSOCK32.DLL" _
   (ByVal szHost As String, ByVal dwHostLen As Long) As Long
   
Public Declare Function gethostbyname Lib "WSOCK32.DLL" _
   (ByVal szHost As String) As Long
   
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
   (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)

Public Function GetIPAddress() As String

   Dim sHostName    As String * 256
   Dim lpHost    As Long
   Dim HOST      As HOSTENT
   Dim dwIPAddr  As Long
   Dim tmpIPAddr() As Byte
   Dim i         As Integer
   Dim sIPAddr  As String
   
   If Not SocketsInitialize() Then
      GetIPAddress = ""
      Exit Function
   End If

   If gethostname(sHostName, 256) = SOCKET_ERROR Then
      GetIPAddress = ""
      MsgBox "Windows Sockets error " & Str$(WSAGetLastError()) & _
              " has occurred. Unable to successfully get Host Name."
      SocketsCleanup
      Exit Function
   End If

   sHostName = Trim$(sHostName)
   lpHost = gethostbyname(sHostName)
   
   If lpHost = 0 Then
      GetIPAddress = ""
      MsgBox "Windows Sockets are not responding. " & _
              "Unable to successfully get Host Name."
      SocketsCleanup
      Exit Function
   End If
   
  'to extract the returned IP address, we have to copy
  'the HOST structure and its members
   CopyMemory HOST, lpHost, Len(HOST)
   CopyMemory dwIPAddr, HOST.hAddrList, 4
   
  'create an array to hold the result
   ReDim tmpIPAddr(1 To HOST.hLen)
   CopyMemory tmpIPAddr(1), dwIPAddr, HOST.hLen
   
  'and with the array, build the actual address,
  'appending a period between members
   For i = 1 To HOST.hLen
      sIPAddr = sIPAddr & tmpIPAddr(i) & "."
   Next
 
  'the routine adds a period to the end of the
  'string, so remove it here
   GetIPAddress = Mid$(sIPAddr, 1, Len(sIPAddr) - 1)
   
   SocketsCleanup
   
End Function

Public Function GetIPHostName() As String

    Dim sHostName As String * 256
   
    If Not SocketsInitialize() Then
        GetIPHostName = ""
        Exit Function
    End If
   
    If gethostname(sHostName, 256) = SOCKET_ERROR Then
        GetIPHostName = ""
        MsgBox "Windows Sockets error " & Str$(WSAGetLastError()) & _
                " has occurred.  Unable to successfully get Host Name."
        SocketsCleanup
        Exit Function
    End If
   
    GetIPHostName = Left$(sHostName, InStr(sHostName, Chr(0)) - 1)
    SocketsCleanup

End Function

Public Function HiByte(ByVal wParam As Integer) As Integer

   Dim tmp As Integer
   tmp = wParam \ &H100
   HiByte = tmp And &HFF&
 
End Function

Public Function LoByte(ByVal wParam As Integer) As Integer

    LoByte = wParam And &HFF&

End Function

Public Sub SocketsCleanup()

    If WSACleanup() <> ERROR_SUCCESS Then
        MsgBox "Socket error occurred in Cleanup."
    End If
   
End Sub

Public Function SocketsInitialize() As Boolean

   Dim WSAD As WSADATA
   Dim sLoByte As String
   Dim sHiByte As String
   
   If WSAStartup(WS_VERSION_REQD, WSAD) <> ERROR_SUCCESS Then
      MsgBox "The 32-bit Windows Socket is not responding."
      SocketsInitialize = False
      Exit Function
   End If
   
   If WSAD.wMaxSockets < MIN_SOCKETS_REQD Then
        MsgBox "This application requires a minimum of " & _
                CStr(MIN_SOCKETS_REQD) & " supported sockets."
       
        SocketsInitialize = False
        Exit Function
    End If
   
   If LoByte(WSAD.wVersion) < WS_VERSION_MAJOR Or _
     (LoByte(WSAD.wVersion) = WS_VERSION_MAJOR And _
      HiByte(WSAD.wVersion) < WS_VERSION_MINOR) Then
     
      sHiByte = CStr(HiByte(WSAD.wVersion))
      sLoByte = CStr(LoByte(WSAD.wVersion))
     
      MsgBox "Sockets version " & sLoByte & "." & sHiByte & _
             " is not supported by 32-bit Windows Sockets."
     
      SocketsInitialize = False
      Exit Function
     
   End If
   
  'must be OK, so lets do it
   SocketsInitialize = True
End Function





Place the following code in the form. Put two textboxes and a command button on the form.

Option Explicit

Private Sub command1_Click()
 Text1 = GetIPHostName()
 Text2 = GetIPAddress()
End Sub
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2681704
The easiest way is to place the Winsock Control on your Form

Private Sub Form_Load()
Caption = Winsock1.LocalIP
End Sub
0
 
LVL 1

Author Comment

by:dcordner
ID: 2681707
Sorry.  This is the same as what I already have.  It returns the hostname of the following:

dcordner

The whole host name is actually:

dcordner.glad.temple.edu


The latter is what I need.
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2681725
0
 
LVL 9

Accepted Solution

by:
Ruchi earned 300 total points
ID: 2681751
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2681786
i'm not at home so i couldnt try out the link i sent you, any luck with it ??  
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:dcordner
ID: 2681839
AzraSound:

The code only produced the host anme and not the fully qualified domain name.

Thanks though.
0
 
LVL 1

Author Comment

by:dcordner
ID: 2681881
Hi Ruchi:

The link below was the closest:

http://www.planetsourcecode.com/vb/scripts/ShowCode.asp

It did return the full qualified domain name.  It actually looks up the IP entry in the DNS table of the DNS server.  

I was hoping for some code that would display it from the local machine.  This way, if the DNS server is unavailable, I could still get it.

This poses an interesting predicament for me, however.  If the local settings for the IP domain are different from the name server, I must decide which to use.

If no one else comes up with any code to get the information from the local machine, the points are yours.

If someone does, I'll post this question again with the destinction of getting the info from the name server so you can get the points.

--

Don
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2681885
Ignore my latest comment.
go to
http://support.microsoft.com/support/kb/articles/Q160/2/15.asp

This is to return the complete domain name.... I think so.
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2681908
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2681947
Hi Dcordner,
I did not read your comment as I posted my last two comments. I am not sure if the link (the KB article) I gave you generates the fully qualified domain name or not. Just let me know. Thanks very much.
0
 
LVL 1

Author Comment

by:dcordner
ID: 2685509
Adjusted points from 200 to 300
0
 
LVL 1

Author Comment

by:dcordner
ID: 2685510
Hi Ruchi:

The link below was the closest and actually ends up being the better solution than what I initially wanted.

You got the points!

Thanks for all your efforts.  Have an extra 100 points for the ton of work you did!
                      http://www.planetsourcecode.com/vb/scripts/ShowCode.asp
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2706571
Dcordner: You're most welcome! I'm glad that I could be of any help to you!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 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

15 Experts available now in Live!

Get 1:1 Help Now