Reputation: 159
Ok guys, I've dug and dug, including on this site, and can't find any solution to this.
Assume several computers are connected to a network and all are in the same workgroup. I can easily get a list of all their network names, like //MYCOMPUTER-PC. I'm trying to get all of the local IP address of these machines (not just the current machine) i.e. 192.168.0.1 without having to physically connect to every possible IP and test a connection.
So far everything I've tried has failed. Either I can only get the IP address of the machine I'm running on or, I get the ip address for Searchguide (198.105.244.65) which is where my ISP defaults when an IP address can't be found.
Here's the code I'm using that fails. If I leave the \ in the network name, I get an unknown result. If I remove the \ I get the Searchguide IP:
function GetIPAddress(NetworkName: String): String;
var
Error: DWORD;
HostEntry: PHostEnt;
Data: WSAData;
Address: In_Addr;
begin
Error:=WSAStartup(MakeWord(1, 1), Data);
if Error = 0 then
begin
HostEntry:=gethostbyname(PansiChar(NetworkName));
Error:=GetLastError();
if Error = 0 then
begin
Address:=PInAddr(HostEntry^.h_addr_list^)^;
Result:=inet_ntoa(Address);
end
else
begin
Result:='Unknown';
end;
end
else
begin
Result:='Error';
end;
WSACleanup();
end;
So, assuming I know nothing about the router these computers are connected to, is it even possible to get a list of local IP's???
Thanks
edit
Here's an image of it's output
Upvotes: 1
Views: 3356
Reputation: 54832
You're using XE5, string is unicode, you are not passing host name to gethostbyname
but some garbage. Replace
HostEntry:=gethostbyname(PansiChar(NetworkName));
with
HostEntry:=gethostbyname(PansiChar(AnsiString(NetworkName)));
Additionally test the return of gethostbyname
for success, not the last error. Use documentation of the api for proper usage. Also use WSAGetLastError
to retrieve winsock error code (as documented), not GetLastError
.
Upvotes: 2