Getting a computer name from the IP address(C#)
-
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
-
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
-
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
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
-
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
-
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
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
-
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