Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getHostName

Status
Not open for further replies.

mevasquez

Programmer
Joined
Aug 26, 2003
Messages
75
Location
US
I downloaded the sample code in thread222-760425; however, when I try to use Trim$(hostname), nothing happens, the variable still contain vbNullChar characters. How do I remove vbNullChar characters in a string. Here is sample code.
Code:
Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal hostname$, _
  ByVal HostLen As Long) As Long
Private Const SOCKET_ERROR = -1

Dim hostname As String * 256

 If gethostname(hostname, 256) = SOCKET_ERROR Then
         'output error 
          Exit Function
 Else
          hostname = Trim$(hostname)
 End If
Because as you can see hostname was declare as a 256 character string. How do I remove the excess characters. The Trim$(hostname) did not remove the excess characters.
I tried,
Code:
   hostname = replace(hostname, vbNullChar, "")
but this just remove the vbNullChar and left a lot of spaces.

TIA

Mike
 
What about doing the TRIM$(hostname) after you have done the replace () command.

The trim may not be working since there was a vbnullcar at the end and could not trim it.

Just a thought?????
 
I remember this happening to me once before and I fixed it but I don't remember how I did it.

Mike
 
Tried this?

[tt]hostname = Left$(hostname, InStr(hostname, vbNullChar) - 1)[/tt]
 
You've Dimmed hostname as String * 256. It will always be 256 characters long, padded with spaces - that's what the Dim statement does. Change the Dim statement or use another variable

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
What was I thinking?

I seldom used fixed-length Strings for these sorts of API calls and seem to have fallen into a rut. Of course my suggestion won't help eliminate the spaces!

Yes, use a variable-length String variable and pre-pad it to 256 using Spaces$(256) or String$(256, 0) before the call. Follow up with Left$() as I mentioned before.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top