En este truco he dejado otra función interesante, GetCurrentIP, que nos informa de la IP de la máquina local actual.
delphi
uses Windows, WinSock...; type TMIB_IPADDRROW = packed record dwAddr: DWORD; dwIndex: DWORD; dwMask: DWORD; dwBCastAddr: DWORD; dwReasmSize: DWORD; unused1: SmallInt; wType: SmallInt; end; TMIB_IPADDRTABLE = record dwNumEntries: DWORD; table: array[0..0] of TMIB_IPADDRROW; end; PMIB_IPADDRTABLE = ^TMIB_IPADDRTABLE; function GetIpAddrTable(IpAddrTable: PMIB_IPADDRTABLE; pdwSize: PULONG; Order: BOOL): DWORD; stdcall; external 'iphlpapi.dll' name 'GetIpAddrTable'; implementation function GetCurrentIP: DWORD; var Wsa: WSADATA; Name: array[0..255] of char; hostinfo: PHOSTENT; begin Result:= 0; FillChar(Wsa, SizeOf(WSAData), 0); if WSAStartup(MAKEWORD(2, 2), Wsa) = 0 then begin if gethostname(Name, SizeOf(Name)) = 0 then begin hostinfo:= gethostbyname(Name); if hostinfo <> nil then Result:= PDWORD(hostinfo^.h_addr_list^)^; WSACleanup; end; end; end; function GetBrodcastAddress: String; var pIPAddrTable: PMIB_IPADDRTABLE; dwSize: DWORD; i: integer; BroadCastInAddr: IN_ADDR; begin BroadCastInAddr.S_addr:= 0; dwSize:= 0; GetIpAddrTable(nil, @dwSize, true); GetMem(pIPAddrTable, dwSize); if pIPAddrTable<>nil then begin if GetIpAddrTable(pIPAddrTable, @dwSize, true) = NO_ERROR then for i:=0 to pIPAddrTable^.dwNumEntries-1 do begin if GetCurrentIP = pIPAddrTable^.table[i].dwAddr then begin BroadCastInAddr.S_addr:= pIPAddrTable^.table[i].dwAddr or not pIPAddrTable^.table[i].dwMask; break; end; end; FreeMem(pIPAddrTable); end; Result:= inet_ntoa(BroadCastInAddr); end;
Espero que el truco sea de utilidad y le deis un uso correcto sin maldades.
Saludos.