Procedure.s GetIpAddr(ComputerName.s)
If ComputerName
If WSAStartup_ ((1<<8|1), wsa.WSADATA) = #NOERROR
*host.HOSTENT = gethostbyname_(ComputerName)
WSACleanup_()
If *host
ProcedureReturn PeekS(inet_ntoa_(PeekL(PeekL(*host\h_addr_list))))
EndIf
EndIf
EndIf
EndProcedure
Procedure.s GetMacAddr(IP.s)
n.l = 6
Dim MacAddr.b(n)
If SendARP_(inet_addr_(IP), 0, @MacAddr(0), @n) = #NO_ERROR
For i = 0 To n - 2
MAC$ + RSet(Hex(MacAddr(i)&255),2,"0") + ":"
Next
ProcedureReturn MAC$ + RSet(Hex(MacAddr(i)&255),2,"0")
EndIf
EndProcedure
Procedure.s GetHostName()
Size.l = 32
Host.s = Space(Size)
GetComputerName_(@Host, @Size)
ProcedureReturn Host
EndProcedure
Debug GetHostName()
Debug GetIpAddr(GetHostName())
Debug GetMacAddr(GetIpAddr(GetHostName()))
Last edited by Flype on Sat May 20, 2006 12:06 pm, edited 1 time in total.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Structure ADDRESS
IpLong.l
IpString.s
MacString.s
MacLength.l
MacArray.b[6]
EndStructure
Procedure.l GetIPAddresses(usrList.ADDRESS(), HostName.s = "")
Protected wsa.WSADATA, *host.HOSTENT
If WSAStartup_(1<<8|1, wsa) = #NOERROR
*host = gethostbyname_(HostName)
WSACleanup_()
If *host
For i = 0 To *host\h_length - 1
If AddElement(usrList())
usrList()\IpLong = PeekL(PeekL(*host\h_addr_list)+(i*SizeOf(Long)))
usrList()\IpString = IPString(usrList()\IpLong)
usrList()\MacLength = SizeOf(ADDRESS\MacArray)
If SendARP_(usrList()\IpLong, 0, @usrList()\MacArray, @usrList()\MacLength) = #NO_ERROR
For j = 0 To usrList()\MacLength - 2
usrList()\MacString + RSet(Hex(usrList()\MacArray[j] & 255), 2, "0") + ":"
Next
usrList()\MacString + RSet(Hex(usrList()\MacArray[j] & 255), 2, "0")
EndIf
EndIf
Next
EndIf
EndIf
ProcedureReturn CountList(usrList())
EndProcedure
NewList list.ADDRESS()
If GetIPAddresses(list())
ForEach list()
Debug "IP: " + list()\IpString
Debug "MAC: " + list()\MacString
Next
EndIf
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Nice code, Flype! If I may make one infinitely tiny suggestion, PB4 has unsigned bytes now, so MacArray.c[6] would be slightly cleaner as & 255 wouldn't be needed.
netmaestro wrote:Nice code, Flype! If I may make one infinitely tiny suggestion, PB4 has unsigned bytes now, so MacArray.c[6] would be slightly cleaner as & 255 wouldn't be needed.
if you do that it becomes no more compatible with unicode compiler option.
so it's better to let .b because .c is not a true unsigned byte as it is 2 bytes length in unicode.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
has anyone tried to implement this for Linux or Mac? I'd be heavily interested...
I am also seeking for a cross-platform-way to find all computers in a network... Like Apples Bonjour.
Thank you, that does make it work - I noticed the CountList depreciation.
However, after all, the code is not suitable because it only finds the MAC address if the NIC is connected
Edit: I should say, not suitable for returning the address of the PC it is run from, since I wanted the code for that purpose alone, which in fact it was not intended for anyway.
Last edited by IdeasVacuum on Fri Nov 19, 2010 2:23 am, edited 1 time in total.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.