Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Getting a computer name from the IP address(C#)

Getting a computer name from the IP address(C#)

Scheduled Pinned Locked Moved C#
csharpsysadminhelptutorial
6 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    WvdW
    wrote on last edited by
    #1

    Hi, I'm activating software installations on a network but in a very manual way. First pinging the network for active computers, compare actives computers with an installation list then executing a bat on the computer. I thought it should be easy to automate the process and started reading up on the System.Net.NetworkInformation library but the problem is that I don't know how to extract the computer name from the pingReply object. It only returns the IP address and since the DNS lookup table isn't up to date I can't use that. Is there another way to get a computers name from the IP address Thanks WvdW

    L H 2 Replies Last reply
    0
    • W WvdW

      Hi, I'm activating software installations on a network but in a very manual way. First pinging the network for active computers, compare actives computers with an installation list then executing a bat on the computer. I thought it should be easy to automate the process and started reading up on the System.Net.NetworkInformation library but the problem is that I don't know how to extract the computer name from the pingReply object. It only returns the IP address and since the DNS lookup table isn't up to date I can't use that. Is there another way to get a computers name from the IP address Thanks WvdW

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      WvdW wrote:

      then executing a bat on the computer

      [the CodeProject PoliticallyCorrectBot has removed some comments from this post] hope that helps

      1 Reply Last reply
      0
      • W WvdW

        Hi, I'm activating software installations on a network but in a very manual way. First pinging the network for active computers, compare actives computers with an installation list then executing a bat on the computer. I thought it should be easy to automate the process and started reading up on the System.Net.NetworkInformation library but the problem is that I don't know how to extract the computer name from the pingReply object. It only returns the IP address and since the DNS lookup table isn't up to date I can't use that. Is there another way to get a computers name from the IP address Thanks WvdW

        H Offline
        H Offline
        Hessam Jalali
        wrote on last edited by
        #3

        Dns.Resolve method will get you the name however I said before in another post the method is obsolete and Microsoft recommends to use Dns.GetHostEntry method instead of it but that method didn't give me the name of computers on our network except mine maybe there is a trick on using that!. good Luck

        W 1 Reply Last reply
        0
        • H Hessam Jalali

          Dns.Resolve method will get you the name however I said before in another post the method is obsolete and Microsoft recommends to use Dns.GetHostEntry method instead of it but that method didn't give me the name of computers on our network except mine maybe there is a trick on using that!. good Luck

          W Offline
          W Offline
          WvdW
          wrote on last edited by
          #4

          Thanks I guess I'll have rewrite it for myself or open a cmd and get the reply from there. Enjoy the rest of your day WvdW

          H 1 Reply Last reply
          0
          • W WvdW

            Thanks I guess I'll have rewrite it for myself or open a cmd and get the reply from there. Enjoy the rest of your day WvdW

            H Offline
            H Offline
            Hessam Jalali
            wrote on last edited by
            #5

            I just had it in my archive maybe simplify your work but it in VB :( Option Explicit '// define constants Private Const IP_SUCCESS As Long = 0 Private Const SOCKET_ERROR As Long = -1 Private Const MAX_WSADescription As Long = 256 Private Const MAX_WSASYSStatus As Long = 128 Private Const MIN_SOCKETS_REQD As Long = 1 Private Const WS_VERSION_REQD As Long = &H101 'Private Const WS_VERSION_MAJOR As Long = WS_VERSION_REQD / &H100 And &HFF& Private Const WS_VERSION_MINOR As Long = WS_VERSION_REQD And &HFF& Private Const WSADescription_Len As Long = 256 Private Const WSASYS_Status_Len As Long = 128 Private Const AF_INET As Long = 2 '// structures Private Type HOSTENT hName As Long hAliases As Long hAddrType As Integer hLength As Integer hAddrList As Long End Type Private Type WSADATA wVersion As Integer wHighVersion As Integer szDescription(0 To MAX_WSADescription) As Byte szSystemStatus(0 To MAX_WSASYSStatus) As Byte wMaxSockets As Long wMaxUDPDG As Long dwVendorInfo As Long End Type '// api 'kernel32 Private Declare Sub apiCopyMemory Lib "kernel32" Alias "RtlMoveMemory" (xDest As Any, xSource As Any, ByVal nBytes As Long) Private Declare Function apiStrLen Lib "kernel32" Alias "lstrlenA" (lpString As Any) As Long 'wsock32 Private Declare Function apiGetHostByName Lib "wsock32.dll" Alias "gethostbyname" (ByVal hostname As String) As Long Private Declare Function apiWSAStartup Lib "wsock32.dll" Alias "WSAStartup" (ByVal wVersionRequired As Long, lpWSADATA As WSADATA) As Long Private Declare Function apiWSACleanup Lib "wsock32.dll" Alias "WSACleanup" () As Long Private Declare Function apiInetAddr Lib "wsock32.dll" Alias "inet_addr" (ByVal s As String) As Long Private Declare Function apiGetHostByAddr Lib "wsock32.dll" Alias "gethostbyaddr" (haddr As Long, ByVal hnlen As Long, ByVal addrtype As Long) As Long '// private functions Private Function InitializeSocket() As Boolean Dim WSAD As WSADATA 'attempt to initialize the socket InitializeSocket = apiWSAStartup(WS_VERSION_REQD, WSAD) = IP_SUCCESS End Function Private Sub CloseSocket() 'try to close the socket If apiWSACleanup() <> 0 Then MsgBox "Error calling apiWSACleanup.", vbCritical End If End Sub Public Function GetIPFromHostName(ByVal sHostName As String) As String 'converts a host name to an IP address. Dim nBytes As Long Dim ptrHosent As Long Dim hstHost As HOSTENT D

            W 1 Reply Last reply
            0
            • H Hessam Jalali

              I just had it in my archive maybe simplify your work but it in VB :( Option Explicit '// define constants Private Const IP_SUCCESS As Long = 0 Private Const SOCKET_ERROR As Long = -1 Private Const MAX_WSADescription As Long = 256 Private Const MAX_WSASYSStatus As Long = 128 Private Const MIN_SOCKETS_REQD As Long = 1 Private Const WS_VERSION_REQD As Long = &H101 'Private Const WS_VERSION_MAJOR As Long = WS_VERSION_REQD / &H100 And &HFF& Private Const WS_VERSION_MINOR As Long = WS_VERSION_REQD And &HFF& Private Const WSADescription_Len As Long = 256 Private Const WSASYS_Status_Len As Long = 128 Private Const AF_INET As Long = 2 '// structures Private Type HOSTENT hName As Long hAliases As Long hAddrType As Integer hLength As Integer hAddrList As Long End Type Private Type WSADATA wVersion As Integer wHighVersion As Integer szDescription(0 To MAX_WSADescription) As Byte szSystemStatus(0 To MAX_WSASYSStatus) As Byte wMaxSockets As Long wMaxUDPDG As Long dwVendorInfo As Long End Type '// api 'kernel32 Private Declare Sub apiCopyMemory Lib "kernel32" Alias "RtlMoveMemory" (xDest As Any, xSource As Any, ByVal nBytes As Long) Private Declare Function apiStrLen Lib "kernel32" Alias "lstrlenA" (lpString As Any) As Long 'wsock32 Private Declare Function apiGetHostByName Lib "wsock32.dll" Alias "gethostbyname" (ByVal hostname As String) As Long Private Declare Function apiWSAStartup Lib "wsock32.dll" Alias "WSAStartup" (ByVal wVersionRequired As Long, lpWSADATA As WSADATA) As Long Private Declare Function apiWSACleanup Lib "wsock32.dll" Alias "WSACleanup" () As Long Private Declare Function apiInetAddr Lib "wsock32.dll" Alias "inet_addr" (ByVal s As String) As Long Private Declare Function apiGetHostByAddr Lib "wsock32.dll" Alias "gethostbyaddr" (haddr As Long, ByVal hnlen As Long, ByVal addrtype As Long) As Long '// private functions Private Function InitializeSocket() As Boolean Dim WSAD As WSADATA 'attempt to initialize the socket InitializeSocket = apiWSAStartup(WS_VERSION_REQD, WSAD) = IP_SUCCESS End Function Private Sub CloseSocket() 'try to close the socket If apiWSACleanup() <> 0 Then MsgBox "Error calling apiWSACleanup.", vbCritical End If End Sub Public Function GetIPFromHostName(ByVal sHostName As String) As String 'converts a host name to an IP address. Dim nBytes As Long Dim ptrHosent As Long Dim hstHost As HOSTENT D

              W Offline
              W Offline
              WvdW
              wrote on last edited by
              #6

              Thanks Just scanned it but the 'GetHostNameFromIP' looks promising. Luckily vb is not that difficult to convert to C#. Hope you have a great day:) WvdW

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups