Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "Сети";
Текущий архив: 2004.01.13;
Скачать: [xml.tar.bz2];

Вниз

Как получить локальный IP адрес   Найти похожие ветки 

 
Сергей Н.   (2003-10-03 09:42) [0]

Доброе всем время суток. Как узнать IP адреса на компе с двумя сетевыми карточками. Заранее спасибо всем кто ответит.


 
Hazard   (2003-10-04 01:49) [1]

Сначала узнаешь первой функцией имя своего компьютера
потом IP по полученному имени
Свирепствуй!

function GetComputerNetName: string;
var
buffer: array[0..255] of char;
size: dword;
begin
size := 256;
if GetComputerName(buffer, size) then
Result := buffer
else
Result := ""
end;

function GetIPAddress(Name:String): string;
var
WSAData : TWSAData;
p : PHostEnt;
begin
WSAStartup(WINSOCK_VERSION, WSAData);
p := GetHostByName(PChar(Name));
Result := inet_ntoa(PInAddr(p.h_addr_list^)^);
WSACleanup;
end;


 
Сергей Н.   (2003-10-06 12:55) [2]

2Hazard так я узнаю только один IP адрес, а как узнать все IP адреса на компе.


 
Verg   (2003-10-06 13:26) [3]

unit IPHelper;

interface
uses Windows;
const
MAX_INTERFACE_NAME_LEN = 256;
MAX_TRANSPORT_NAME_LEN = 40;
MAX_MEDIA_NAME = 16;
MAX_PORT_NAME = 16;
MAX_DEVICE_NAME = 128;
MAX_PHONE_NUMBER_LEN = 128;
MAX_DEVICETYPE_NAME = 16;
MAXLEN_IFDESCR = 256;
MAXLEN_PHYSADDR = 8;

type
TIPAddr = ULONG; // An IP address.
TIPMask = ULONG; // An IP subnet mask.
TIP_STATUS = ULONG; // Status code returned from IP APIs.

TMIB_IFROW = record
wszName : array[0..MAX_INTERFACE_NAME_LEN-1] of widechar;
dwIndex,
dwType,
dwMtu,
dwSpeed,
dwPhysAddrLen : DWORD;
bPhysAddr : array[0..MAXLEN_PHYSADDR-1] of BYTE;
dwAdminStatus,
dwOperStatus,
dwLastChange,
dwInOctets,
dwInUcastPkts,
dwInNUcastPkts,
dwInDiscards,
dwInErrors,
dwInUnknownProtos,
dwOutOctets,
dwOutUcastPkts,
dwOutNUcastPkts,
dwOutDiscards,
dwOutErrors,
dwOutQLen,
dwDescrLen : DWORD;
bDescr : array[0..MAXLEN_IFDESCR-1] of BYTE;
end;

PMIB_IFROW = ^TMIB_IFROW;

TMIB_IFTABLE = record
dwNumEntries : DWORD;
table : array[0..0] of TMIB_IFROW;
end;
PMIB_IFTABLE = ^TMIB_IFTABLE;

TMIB_IPADDRROW = record
dwAddr,
dwIndex,
dwMask,
dwBCastAddr,
dwReasmSize : DWORD;
unused1,
unused2 : WORD;
end;
PMIB_IPADDRROW = ^TMIB_IPADDRROW;

TMIB_IPADDRTABLE = record
dwNumEntries : DWORD;
table : array[0..0] of TMIB_IPADDRROW;
end;
PMIB_IPADDRTABLE= ^TMIB_IPADDRTABLE;

//////////////////////////////////////////////////////////////////////////////
// //
// Gets the MIB-II IfTable //
// //
{Parameters
pIfTable
[out] Pointer to a buffer that receives the interface table as a MIB_IFTABLE structure.
pdwSize
[in, out] On input, specifies the size of the buffer pointed to by the pIfTable parameter.
On output, if the buffer is not large enough to hold the returned interface table,
the function sets this parameter equal to the required buffer size.

bOrder
[in] Specifies whether the returned interface table should be sorted in ascending order by
interface index. If this parameter is TRUE, the table is sorted.

Return Values
If the function succeeds, the return value is NO_ERROR.

If the function fails, the return value is one of the following error codes.

Return Code Description
ERROR_INSUFFICIENT_BUFFER The buffer pointed to by the pIfTable parameter is not large
enough.
The required size is returned in the DWORD variable pointed
to by the pdwSize parameter.
ERROR_INVALID_PARAMETER The pdwSize parameter is NULL, or GetIfTable is unable to
write to the memory pointed to by the pdwSize parameter.
ERROR_NOT_SUPPORTED This function is not supported on the operating system
in use on the local system.
Other Use FormatMessage to obtain the message string for the returned error.
}
//////////////////////////////////////////////////////////////////////////////
function GetIfTable(var pIfTable : TMIB_IFTABLE; var pdwSize : ULONG; bOrder : BOOL): DWORD; stdcall;

{
Parameters
pIpAddrTable [out] Pointer to a buffer that receives the interface–to–IP address
mapping table as a MIB_IPADDRTABLE structure.
pdwSize [in, out] On input, specifies the size of the buffer pointed to by the
pIpAddrTable parameter.
On output, if the buffer is not large enough to hold the
returned mapping table, the function sets this parameter
equal to the required buffer size.

bOrder [in] Specifies whether the returned mapping table should be sorted
in ascending order by IP address.
If this parameter is TRUE, the table is sorted.
Return Values
If the function succeeds, the return value is NO_ERROR.

If the function fails, the return value is one of the following error codes.

Return Code Description
ERROR_INSUFFICIENT_BUFFER The buffer pointed to by the pIpAddrTable parameter is not
large enough. The required size is returned in the DWORD
variable pointed to by the pdwSize parameter.
ERROR_INVALID_PARAMETER The pdwSize parameter is NULL, or GetIpAddrTable is unable
to write to the memory pointed to by the pdwSize parameter.
ERROR_NOT_SUPPORTED This function is not supported on the operating system in
use on the local system.
Other Use FormatMessage to obtain the message string for the returned error.
}
function GetIpAddrTable(var TMIB_IPADDRTABLE; var pdwSize : ULONG; bOrder : BOOL): DWORD; stdcall;

function SendARP(DestIP:DWORD;SrcIP:DWORD; var pMacAddr; var PhyAddrLen:DWORD):DWORD;stdcall;
{
Parameters
DestIP
[in] Destination IP address. The ARP request attempts to obtain the physical address that corresponds to this IP address.
SrcIP
[in] IP address of the sender. This parameter is optional. The caller may specify zero for the parameter.
pMacAddr
[out] Pointer to an array of ULONG variables. The first six bytes of this array receive the physical address that corresponds to the IP address specified by DestIP.
PhyAddrLen
[in, out] On input, specifies the maximum buffer size the user has set aside at pMacAddr to receive the MAC address, in bytes. On output, specifies the number of bytes written to pMacAddr.
Return Values
If the function succeeds, the return value is NO_ERROR.

If the function fails, use FormatMessage to obtain the message string for the returned error.

Remarks
For information about the IPAddr data type, see Windows Simple Data Types. To convert an IP address between dotted decimal notation and IPAddr format, use the inet_addr and inet_ntoa functions.
}

implementation

function GetIfTable; external "IPHLPAPI.DLL" name "GetIfTable";
function GetIpAddrTable; external "IPHLPAPI.DLL" name "GetIpAddrTable";
function SendARP; external "IPHLPAPI.DLL" name "SendARP";

end.


 
RedKat   (2003-11-12 13:53) [4]

Ух ! круто ! ;-)
Но оказывается можно проще :

type
TIPAddrList =array [0..100] of PInAddr;
PIPAddrList =^TIPAddrList;
var
WSData :TWSAData;
HostName :array [0..127] of char;
HostInfo :PHostEnt;
Addr :TIPAddrList;//TInAddr;
begin
try
if WSAStartup($0202, WSData) <> 0 then
RaiseLastOSError;
if gethostname(HostName, SizeOf(HostName)) = SOCKET_ERROR then
raiseLastOSError;
HostInfo :=GetHostByName(HostName);
if HostInfo = nil then
raiseLastOSError;
// Addr :=PInAddr(HostInfo^.h_addr_list^)^;
Addr :=PIPAddrList(HostInfo^.h_addr_list)^;
i :=0;
while Addr[i] <> nil do
begin
writeln(i, " :", inet_ntoa(Addr[i]^));
inc(i);
end;
readln;
finally
WSACleanup;
end;
end;
//



Страницы: 1 вся ветка

Форум: "Сети";
Текущий архив: 2004.01.13;
Скачать: [xml.tar.bz2];

Наверх





Память: 0.47 MB
Время: 0.007 c
3-37518
Lonely
2003-12-16 17:32
2004.01.13
информация о структуре таблицы


14-37912
MPS
2003-12-20 03:50
2004.01.13
...по поводу сертификации RetraTech


1-37685
TATIANA
2003-12-26 11:27
2004.01.13
Maskedit - Edit c маской


6-37833
Passlight
2003-11-10 18:00
2004.01.13
Обработка ошибок в TIdHTTP


9-37487
NailMan
2003-06-21 12:25
2004.01.13
Конструкция ресурсного мэнеджера в 3Д-игре





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский