Getting remote computer name

Posted on 2003-02-22
Medium Priority
Last Modified: 2010-05-01
I'm trying to send winpopup messages using my Vb program.
I need to work with win9x hence I'm using mailslots. apparently mailslots dont work with IP addresses, and require computer name. The "dns name" does not work. I suspect it is the "computer name" (SMB/NeTBios name??) that is required there. Can anyone help in finding a function to get this *whatever* name from IP address ?


'                   API DECLARATIONS
Private Declare Function CloseHandle Lib "kernel32" (ByVal hHandle As Long) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFileName As Long, ByVal lpBuff As Any, ByVal nNrBytesToWrite As Long, lpNrOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwAccess As Long, ByVal dwShare As Long, ByVal lpSecurityAttrib As Long, ByVal dwCreationDisp As Long, ByVal dwAttributes As Long, ByVal hTemplateFile As Long) As Long
'                   CONSTANTS
Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000
Const GENERIC_EXECUTE = &H20000000
Const GENERIC_ALL = &H10000000
'                   FUNCTIONS AND SUBS
Private Sub cmdSend_Click()
    Call SendMsg(txtFrom.Text, txtTo.Text, txtMsg.Text)
End Sub

Function SendMsg(sFrom As String, sTo As String, sMsg As String) As Long
    Dim lHandle As Long
    Dim lByteWritten As Long
    Dim sMailslot As String
    sMailslot$ = "\\" + sTo$ + "\mailslot\messngr"
    sMsg$ = sFrom$ + Chr$(0) + sTo$ + Chr$(0) + sMsg$ + Chr$(0)
    Call WriteFile&(lHandle&, sMsg$, Len(sMsg$), lByteWritten&, 0)
    Call CloseHandle&(lHandle&)
End Function
Question by:amIT
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 7999769
I think this will help you...

Private Declare Function GetComputerName Lib "kernel32" Alias _
    "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function ComputerName() As String
    ' Returns the name of the local computer.
    Dim BUFFER As String * 512, Length As Long
    Length = Len(BUFFER)
    If GetComputerName(BUFFER, Length) Then
        ' this API returns non-zero if successful,
        ' and modifies the length argument
        ComputerName = Left(BUFFER, Length)
    End If
End Function

Author Comment

ID: 8000366
Thanks, but I dont need local computer name.
I need to the (NetBIOS or whatever name is used by mailslot) computer name from it's IP.
This name, which is not same as it's dns name, is used by windows to identify itself on the network.

Expert Comment

ID: 8000668
Grab it from the environment variables:

Dim strComputerName As String
strComputerName = Environ("COMPUTERNAME")

No API calls required.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 13

Expert Comment

ID: 8006062
amIT looks like you want is to resolve the remote computer host name from an IP address, correct?  If so, you can do it as follows, from www.mvps.org:

call it from your routine as follows:


copy the code below to your module:

Option Explicit
' Copyright ©1996-2003 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce
'               or publish this code on any web site,
'               online service, or distribute as source
'               on any media without express permission.
Private Const WSADescription_Len As Long = 256
Private Const WSASYS_Status_Len As Long = 128
Private Const WS_VERSION_REQD As Long = &H101
Private Const IP_SUCCESS As Long = 0
Private Const SOCKET_ERROR As Long = -1
Private Const AF_INET As Long = 2

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
  imaxudp As Integer
  lpszvenderinfo As Long
End Type

Private Declare Function WSAStartup Lib "wsock32" _
  (ByVal VersionReq As Long, _
   WSADataReturn As WSADATA) As Long
Private Declare Function WSACleanup Lib "wsock32" () As Long

Private Declare Function inet_addr Lib "wsock32" _
  (ByVal s As String) As Long

Private Declare Function gethostbyaddr Lib "wsock32" _
  (haddr As Long, _
   ByVal hnlen As Long, _
   ByVal addrtype As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" _
   Alias "RtlMoveMemory" _
  (xDest As Any, _
   xSource As Any, _
   ByVal nbytes As Long)
Private Declare Function lstrlen Lib "kernel32" _
   Alias "lstrlenA" _
  (lpString As Any) As Long
Public Function SocketsInitialize() As Boolean

   SocketsInitialize = WSAStartup(WS_VERSION_REQD, WSAD) = IP_SUCCESS
End Function

Public Sub SocketsCleanup()
   If WSACleanup() <> 0 Then
       MsgBox "Windows Sockets error occurred in Cleanup.", vbExclamation
   End If
End Sub

Public Function GetHostNameFromIP(ByVal sAddress As String) As String

   Dim ptrHosent As Long
   Dim hAddress As Long
   Dim nbytes As Long
   If SocketsInitialize() Then

     'convert string address to long
      hAddress = inet_addr(sAddress)
      If hAddress <> SOCKET_ERROR Then
        'obtain a pointer to the HOSTENT structure
        'that contains the name and address
        'corresponding to the given network address.
         ptrHosent = gethostbyaddr(hAddress, 4, AF_INET)
         If ptrHosent <> 0 Then
           'convert address and
           'get resolved hostname
            CopyMemory ptrHosent, ByVal ptrHosent, 4
            nbytes = lstrlen(ByVal ptrHosent)
            If nbytes > 0 Then
               sAddress = Space$(nbytes)
               CopyMemory ByVal sAddress, ByVal ptrHosent, nbytes
               GetHostNameFromIP = sAddress
            End If
         Else: MsgBox "Call to gethostbyaddr failed."
         End If 'If ptrHosent
      Else: MsgBox "String passed is an invalid IP."
      End If 'If hAddress
   Else: MsgBox "Sockets failed to initialize."
   End If  'If SocketsInitialize
End Function
'--end block--'

Expert Comment

ID: 8901256
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
LVL 13

Expert Comment

ID: 8904968
Personally, I'm still not sure what amit wants -  I'm just as happy if it's closed as if points are awarded.
LVL 49

Expert Comment

ID: 8964148
amIT, an EE Moderator will handle this for you.
Moderator, my recommended disposition is:

    Save as PAQ -- No Refund.

DanRollins -- EE database cleanup volunteer

Accepted Solution

YensidMod earned 0 total points
ID: 9104029
Question is PAQ'd and no points refunded.

Community Support Moderator @Experts Exchange

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

770 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