Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi

I have this simple .NET VB command which will return the computer name:

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:

Public Declare Function gethostbyname Lib "wsock32" (ByVal szHost As String) As Long

  Dim localhost As String
  gethostbyname (localhost)
  localhost = localhost.Trim(".")

  • Leaders
Posted

you need to give your string some space ( the equivalent in .net would be using a stringbuilder ) , basically Dim localhost As String * 256

eg:

[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]

Posted

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.

 

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

 

Good luck :cool:

Never trouble another for what you can do for yourself.
  • Leaders
Posted

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.

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. :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...