제가 쓰는 소스 일부 발체 입니다.
사용목적이 틀려서.. 그러나 소스내부에 해당하는 부문이 있습니다.
<!--CodeS-->
function TUnitCommonUtil.GetNetworkInfo(SERVERIP : Cardinal): boolean;
var
RET_BETSTINTERFACE: DWORD;
dwSize: ULONG;
PIPAddrTable: PMIB_IPADDRTABLE;
AdapterInfo: PIP_ADAPTER_INFO;
i: integer;
retvalue: integer;
PBuf: PCHAR;
PerAdapterInfo: IP_PER_ADAPTER_INFO;
PIpAddr: PIP_ADDR_STRING;
begin
Result := false;
if GetBestInterface(SERVERIP, RET_BETSTINTERFACE) = 0 then
begin
GetMem(PIPAddrTable, sizeof(MIB_IPADDRTABLE));
dwSize := 0;
retvalue := GetIpAddrTable(PIPAddrTable, dwSize, false);
if (retvalue <> ERROR_INSUFFICIENT_BUFFER) then
begin
FreeMem(PIPAddrTable, sizeof(MIB_IPADDRTABLE));
exit;
end
else
begin
try
FreeMem(PIPAddrTable, sizeof(MIB_IPADDRTABLE));
GetMem(PIPAddrTable, dwSize);
if GetIpAddrTable(PIPAddrTable, dwSize, false) = NO_ERROR then
begin
for i := 0 to PIPAddrTable^.dwNumEntries - 1 do
begin
if RET_BETSTINTERFACE = PIPAddrTable^.table[i].dwIndex then
begin
mIP := PIPAddrTable^.table[i].dwAddr;
mMask := PIPAddrTable^.table[i].dwMask;
mGateWay := 0;
GetMem(pBuf, sizeof(IP_ADAPTER_INFO));
dwSize := 0;
retvalue := GetAdaptersInfo(PIP_ADAPTER_INFO(PBuf), dwSize);
if (retvalue <> ERROR_INSUFFICIENT_BUFFER) and (retvalue = NO_ERROR) then
begin
FreeMem(pBuf, sizeof(IP_ADAPTER_INFO));
exit;
end;
try
FreeMem(pBuf, sizeof(IP_ADAPTER_INFO));
GetMem(pBuf, dwSize);
FillChar(pBuf^, dwSize, #0); // clear buffer
retvalue := GetAdaptersInfo(PIP_ADAPTER_INFO(PBuf), dwSize);
if retvalue = NO_ERROR then
AdapterInfo := PIP_ADAPTER_INFO(PBuf);
while (AdapterInfo <> nil) do
begin
if @AdapterInfo^.IpAddressList <> nil then
if (PIPAddrTable^.table[i].dwAddr = str2ipaddr('127.0.0.1')) or (str2ipaddr(strpas(@AdapterInfo^.IpAddressList.IpAddress.S[0])) = PIPAddrTable^.table[i].dwAddr) then
begin
if str2ipaddr(strpas(@AdapterInfo^.IpAddressList.IpAddress.S[0])) <> 0 then
mIP := str2ipaddr(strpas(@AdapterInfo^.IpAddressList.IpAddress.S[0]));
mAdaterName := strpas(AdapterInfo^.Description);
mAdaperIndexName := strpas(AdapterInfo^.AdapterName);
mMacLength := AdapterInfo^.AddressLength;
move(AdapterInfo^.Address[0], mMacAddr[0], AdapterInfo^.AddressLength);
if @AdapterInfo^.GatewayList <> nil then
mGateWay := str2ipaddr(strpas(@AdapterInfo^.GatewayList.IpAddress.S[0]));
mUseDHCP := AdapterInfo^.DhcpEnabled;
result := true;
mDNS1 := 0;
mDNS2 := 0;
if (osvi.dwPlatformId = VER_PLATFORM_WIN32_NT) then
begin
dwSize := SizeOf(PerAdapterInfo);
retvalue := GetPerAdapterInfo(AdapterInfo^.Index, @PerAdapterInfo, dwSize);
if retvalue = 0 then
begin
mUseDNS := PerAdapterInfo.AutoconfigEnabled or PerAdapterInfo.AutoconfigActive;
mDNS1 := 0;
mDNS2 := 0;
PIpAddr := @PerAdapterInfo.DnsServerList;
if PIpAddr <> nil then
begin
mDNS1 := str2ipaddr(strpas(@PIpAddr^.IpAddress.S[0]));
PIpAddr := PIpAddr^.Next;
if PIpAddr <> nil then
mDNS2 := str2ipaddr(strpas(@PIpAddr^.IpAddress.S[0]));
end;
end;
end
else if osvi.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS then
begin
// Win98 DNS 가져와야 합니다.
mUseDNS := 0;
mDNS1 := 0;
mDNS2 := 0;
end;
사용목적이 틀려서.. 그러나 소스내부에 해당하는 부문이 있습니다.
<!--CodeS-->
function TUnitCommonUtil.GetNetworkInfo(SERVERIP : Cardinal): boolean;
var
RET_BETSTINTERFACE: DWORD;
dwSize: ULONG;
PIPAddrTable: PMIB_IPADDRTABLE;
AdapterInfo: PIP_ADAPTER_INFO;
i: integer;
retvalue: integer;
PBuf: PCHAR;
PerAdapterInfo: IP_PER_ADAPTER_INFO;
PIpAddr: PIP_ADDR_STRING;
begin
Result := false;
if GetBestInterface(SERVERIP, RET_BETSTINTERFACE) = 0 then
begin
GetMem(PIPAddrTable, sizeof(MIB_IPADDRTABLE));
dwSize := 0;
retvalue := GetIpAddrTable(PIPAddrTable, dwSize, false);
if (retvalue <> ERROR_INSUFFICIENT_BUFFER) then
begin
FreeMem(PIPAddrTable, sizeof(MIB_IPADDRTABLE));
exit;
end
else
begin
try
FreeMem(PIPAddrTable, sizeof(MIB_IPADDRTABLE));
GetMem(PIPAddrTable, dwSize);
if GetIpAddrTable(PIPAddrTable, dwSize, false) = NO_ERROR then
begin
for i := 0 to PIPAddrTable^.dwNumEntries - 1 do
begin
if RET_BETSTINTERFACE = PIPAddrTable^.table[i].dwIndex then
begin
mIP := PIPAddrTable^.table[i].dwAddr;
mMask := PIPAddrTable^.table[i].dwMask;
mGateWay := 0;
GetMem(pBuf, sizeof(IP_ADAPTER_INFO));
dwSize := 0;
retvalue := GetAdaptersInfo(PIP_ADAPTER_INFO(PBuf), dwSize);
if (retvalue <> ERROR_INSUFFICIENT_BUFFER) and (retvalue = NO_ERROR) then
begin
FreeMem(pBuf, sizeof(IP_ADAPTER_INFO));
exit;
end;
try
FreeMem(pBuf, sizeof(IP_ADAPTER_INFO));
GetMem(pBuf, dwSize);
FillChar(pBuf^, dwSize, #0); // clear buffer
retvalue := GetAdaptersInfo(PIP_ADAPTER_INFO(PBuf), dwSize);
if retvalue = NO_ERROR then
AdapterInfo := PIP_ADAPTER_INFO(PBuf);
while (AdapterInfo <> nil) do
begin
if @AdapterInfo^.IpAddressList <> nil then
if (PIPAddrTable^.table[i].dwAddr = str2ipaddr('127.0.0.1')) or (str2ipaddr(strpas(@AdapterInfo^.IpAddressList.IpAddress.S[0])) = PIPAddrTable^.table[i].dwAddr) then
begin
if str2ipaddr(strpas(@AdapterInfo^.IpAddressList.IpAddress.S[0])) <> 0 then
mIP := str2ipaddr(strpas(@AdapterInfo^.IpAddressList.IpAddress.S[0]));
mAdaterName := strpas(AdapterInfo^.Description);
mAdaperIndexName := strpas(AdapterInfo^.AdapterName);
mMacLength := AdapterInfo^.AddressLength;
move(AdapterInfo^.Address[0], mMacAddr[0], AdapterInfo^.AddressLength);
if @AdapterInfo^.GatewayList <> nil then
mGateWay := str2ipaddr(strpas(@AdapterInfo^.GatewayList.IpAddress.S[0]));
mUseDHCP := AdapterInfo^.DhcpEnabled;
result := true;
mDNS1 := 0;
mDNS2 := 0;
if (osvi.dwPlatformId = VER_PLATFORM_WIN32_NT) then
begin
dwSize := SizeOf(PerAdapterInfo);
retvalue := GetPerAdapterInfo(AdapterInfo^.Index, @PerAdapterInfo, dwSize);
if retvalue = 0 then
begin
mUseDNS := PerAdapterInfo.AutoconfigEnabled or PerAdapterInfo.AutoconfigActive;
mDNS1 := 0;
mDNS2 := 0;
PIpAddr := @PerAdapterInfo.DnsServerList;
if PIpAddr <> nil then
begin
mDNS1 := str2ipaddr(strpas(@PIpAddr^.IpAddress.S[0]));
PIpAddr := PIpAddr^.Next;
if PIpAddr <> nil then
mDNS2 := str2ipaddr(strpas(@PIpAddr^.IpAddress.S[0]));
end;
end;
end
else if osvi.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS then
begin
// Win98 DNS 가져와야 합니다.
mUseDNS := 0;
mDNS1 := 0;
mDNS2 := 0;
end;
end;
AdapterInfo := AdapterInfo^.Next;
end;
finally
freeMem(pBuf, dwSize);
end;
end;
end;
end;
finally
FreeMem(PIPAddrTable, dwSize);
end;
end;
end;
end;
<!--CodeE-->
이소스는 Destination IP에 지칭되는 Adapter와 IP를 가져오는 함수의 소스 일부 입니다.
님은 GetIpAddrTable, GetAdaptersInfo 이 두가지 Function으로 원하는 결과를 얻을수 있을 겁니다.