Get Computer Name

ADO DOT NET

Centurion
Joined
Dec 20, 2006
Messages
160
Hi
I have this simple .NET VB command which will return the computer name:
Visual Basic:
System.Net.Dns.GetHostName().Trim(".")
I want to know how should I convert it to VB6?
I mean getting the name in VB6?
I found something that won't work:
Visual Basic:
Public Declare Function gethostbyname Lib "wsock32" (ByVal szHost As String) As Long

   Dim localhost As String
   gethostbyname (localhost)
   localhost = localhost.Trim(".")
 
you need to give your string some space ( the equivalent in .net would be using a stringbuilder ) , basically Dim localhost As String * 256
eg:
Code:
[COLOR=#b1b100]Public[/COLOR] [COLOR=#b1b100]Declare[/COLOR] [COLOR=#b1b100]Function[/COLOR] gethostbyname Lib [COLOR=#ff0000]"wsock32"[/COLOR] [COLOR=#66cc66]([/COLOR]ByVal szHost [COLOR=#b1b100]As[/COLOR] [COLOR=#b1b100]String[/COLOR][COLOR=#66cc66])[/COLOR] [COLOR=#b1b100]As[/COLOR] [COLOR=#b1b100]Long[/COLOR]
 
   [COLOR=#b1b100]Dim[/COLOR] localhost [COLOR=#b1b100]As[/COLOR] [COLOR=#b1b100]String [B]* 256[/B][/COLOR]
   gethostbyname [COLOR=#66cc66]([/COLOR]localhost[COLOR=#66cc66])[/COLOR]
   localhost = localhost.[COLOR=#b1b100]Trim[/COLOR][COLOR=#66cc66]([/COLOR][COLOR=#ff0000]"."[/COLOR][COLOR=#66cc66])[/COLOR]
 
Remove parenthesis or use Call

It's been a few years since I've used VB6, but if I recall correctly you will almost certainly have to remove the parenthesis when you call the function, or use the Call keyword. Without Call, the parenthesis will cause the string to be evaluated before it is passed to the function, meaning the result will not be placed in the original variable.

Visual Basic:
'Incorrect:
gethostbyname (localhost)
'Correct:
Call gethostbyname(localhost)
'Correct:
gethostbyname localhost

Good luck :cool:
 
i finally dug out my VB6 disk. i put together a basic example of how this works in VB6.
you must initialize the socket service first using WSAStartup before you can use GetHostName. ( note: gethostbyname returns the IP not the name )
i have comented the code as much as poss.
Code:
Private Const WS_VERSION_REQD = &H101
Private Type WSADATA
    wVersion As Integer
    wHighVersion As Integer
    szDescription As String * 256
    szSystemStatus As String * 128
    iMaxSockets As Integer
    iMaxUdpDg As Integer
    lpVendorInfo As Long
End Type
Private Declare Function WSAStartup Lib "ws2_32.dll" (ByVal wVersionRequired As Integer, ByRef lpWSAData As WSADATA) As Long
Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
Private Declare Function gethostbyname Lib "wsock32" (ByVal szHost As String) As Long
Private Declare Function gethostname Lib "ws2_32.dll" (ByVal name As String, ByVal namelen As Long) As Long
Private Sub Command1_Click()
Dim ws_data As WSADATA
Dim lngStart As Long
'/// must start up the socket service first...
lngStart = WSAStartup(WS_VERSION_REQD, ws_data)
'/// -1 means SocketError
If lngStart <> -1 Then
    Dim localhost As String * 256
    Dim l As Long
    l = gethostname(localhost, 256)
    If l <> -1 Then
        MsgBox localhost
    End If
End If
'/// clean up the socket
WSACleanup
End Sub
hope it helps if you aint already found the answer. :)
 
Back
Top