请问,有哪些方法得到机器的IP地址???谢谢!!(50分)

  • 主题发起人 主题发起人 cxling03
  • 开始时间 开始时间
C

cxling03

Unregistered / Unconfirmed
GUEST, unregistred user!
请问,有哪些方法得到机器的IP地址??谢谢!!
 
function FindAllComputers: DWORD;//寻找局域网的机器名
Var
EnumNetWorkHandle,EnumWorkGroupHandle, EnumComputerHandle: THandle;
EnumError: DWORD;
Network: TNetResource;
NetWorkEntries,WorkGroupEntries, ComputerEntries: DWORD;
EnumNetWorkBuffer,EnumWorkGroupBuffer, EnumComputerBuffer: array[1..MaxEntries] of TNetResource;
EnumBufferLength: DWORD;
I, J, K: DWORD;
begin
Computers.Clear;
FillChar(Network, SizeOf(Network), 0);
with Network do
begin
dwScope := RESOURCE_GLOBALNET;
dwType := RESOURCETYPE_ANY;
dwUsage := RESOURCEUSAGE_CONTAINER;
end;

EnumError := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, @Network, EnumNetWorkHandle);

if EnumError = NO_ERROR then
begin
NetWorkEntries := MaxEntries;
EnumBufferLength := SizeOf(EnumNetWorkBuffer);
EnumError := WNetEnumResource(EnumNetWorkHandle, NetWorkEntries, @EnumNetWorkBuffer, EnumBufferLength);
if EnumError = NO_ERROR then
begin
for I := 1 to NetWorkEntries do
begin
EnumError := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, @EnumNetWorkBuffer, EnumWorkGroupHandle);
if EnumError = NO_ERROR then
begin
WorkGroupEntries := MaxEntries;
EnumBufferLength := SizeOf(EnumWorkGroupBuffer);
EnumError := WNetEnumResource(EnumWorkGroupHandle, WorkGroupEntries, @EnumWorkGroupBuffer, EnumBufferLength);
if EnumError = NO_ERROR then
begin
For J := 1 To WorkGroupEntries do
begin
if SystemVersion = 5 then //Apply For WIN 2K,XP
begin
EnumError := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0,@EnumWorkGroupBuffer[J],EnumComputerHandle);
if EnumError = NO_ERROR then
begin
ComputerEntries := MaxEntries;
EnumBufferLength := SizeOf(EnumComputerBuffer);
EnumError := WNetEnumResource(EnumComputerHandle, ComputerEntries, @EnumComputerBuffer, EnumBufferLength);
for K := 1 to ComputerEntries do
Computers.Add(Copy(EnumComputerBuffer[K].lpRemoteName, 3, Length(EnumComputerBuffer[K].lpRemoteName) - 2));
end;
end
else if SystemVersion = 4 then //Apply For WIN 98,95 ME
begin
Computers.Add(Copy(EnumWorkGroupBuffer[J].lpRemoteName, 3, Length(EnumWorkGroupBuffer[J].lpRemoteName) - 2));
end;
end;
end;
WNetCloseEnum(EnumComputerHandle);
end;
end;
end;
WNetCloseEnum(EnumWorkGroupHandle);
end;

if EnumError = ERROR_NO_MORE_ITEMS then
EnumError := NO_ERROR;
Result := EnumError;
end;

function ChangeNameToIP(ComputerName: String): String;//机器名转化成IP
var
WsData : TWSAData;
HostName : array[0..255] of char;
HostEnt : PHostEnt;
addr : PChar;
IP : string;
begin
try
WSAStartup($0101, WsData);
try
GetHostName(hostName, sizeof(hostName));
StrPCopy(hostName, ComputerName);
hostEnt := GetHostByName(hostName);
if Assigned(hostEnt) then
if Assigned(hostEnt^.h_addr_list) then
begin
addr := hostEnt^.h_addr_list^;
if Assigned (addr) then
begin
IP := Format('%d.%d.%d.%d', [byte(addr [0]),
byte(addr[1]), byte (addr[2]), byte (addr[3])]);
Result := IP;
end
else
Result := '';
end
else
Result := ''
else
begin
Result := '';
end;
finally
WSACleanup;
end;
except

end;
end;
 
Function LocalIP : STRING;
Type
TaPInAddr = Array [0..10] Of PInAddr;
PaPInAddr = ^ TaPInAddr;
Var
phe : PHostEnt;
pptr : PaPInAddr;
Buffer : Array [0..63] Of CHAR;
i : INTEGER;
GInitData : TWSADATA;
Begin
WSAStartup ($101, GInitData);
RESULT := '';
GetHostName (Buffer, SizeOf (Buffer));
phe := GetHostByName (Buffer);
If phe = Nil Then
EXIT;
pptr := PaPInAddr (phe^.h_addr_list);
i := 0;
While pptr^ <> Nil Do
Begin
RESULT := StrPas (inet_ntoa (pptr^^));
INC (i)
End;
WSACleanup
End;
 
到dfm找一下,贴了浪费版面
 
后退
顶部