//获得局域网工作组
// 获得组内计算机的名称
//获得计算机的IP地址
//IP--计算机名
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Psock, NMHttp,WinSock;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Edit1: TEdit;
Edit2: TEdit;
Label2: TLabel;
Label3: TLabel;
Button5: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
Function GetServerList( var Computers : TStringList ) : DWORD;
Function GetUsers( GroupName : string; var List : TStringList ) : Boolean;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
type
TNetResourceArray = ^TNetResource;//网络类型的数组
const
MaxEntries = 250;
{$R *.DFM}
//获得局域网工作组
procedure TForm1.Button1Click(Sender: TObject);
var
list:Tstringlist;
begin
list:=Tstringlist.Create;
GetServerList(LIST);
showmessage(list.CommaText);
list.free;
end;
function TForm1.GetServerList(var Computers: TStringList): DWORD;
var
EnumWorkGroupHandle, EnumComputerHandle: THandle;
EnumError: DWORD;
Network: TNetResource;
WorkGroupEntries, ComputerEntries: DWORD;
EnumWorkGroupBuffer, EnumComputerBuffer: array[1..MaxEntries] of TNetResource;
EnumBufferLength: DWORD;
I, J: 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, 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 I := 1 to WorkGroupEntries do
begin
EnumError := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, @EnumWorkGroupBuffer
, EnumComputerHandle);
if EnumError = NO_ERROR then
begin
ComputerEntries := MaxEntries;
EnumBufferLength := SizeOf(EnumComputerBuffer);
EnumError := WNetEnumResource(EnumComputerHandle, ComputerEntries, @EnumComputerBuffer, EnumBufferLength);
if EnumError = NO_ERROR then
for J := 1 to ComputerEntries do
Computers.Add(Copy(EnumComputerBuffer[J].lpRemoteName, 1, Length(EnumComputerBuffer[J].lpRemoteName)));
WNetCloseEnum(EnumComputerHandle);
end;
end;
end;
WNetCloseEnum(EnumWorkGroupHandle);
end;
if EnumError = ERROR_NO_MORE_ITEMS then
EnumError := NO_ERROR;
Result := EnumError;
end;
function TForm1.GetUsers(GroupName: string;
var List: TStringList): Boolean;
var
NetResource : TNetResource;
Buf : Pointer;
Count,BufSize,Res : DWord;
Ind : Integer;
lphEnum : THandle;
Temp : TNetResourceArray;
Begin
buf:=nil;
Result := False;
List.Clear;
FillChar(NetResource, SizeOf(NetResource), 0);//初始化网络层次信息
NetResource.lpRemoteName := @GroupName[1];//指定工作组名称
NetResource.dwDisplayType := RESOURCEDISPLAYTYPE_SERVER;//类型为服务器(工作组)
NetResource.dwUsage := RESOURCEUSAGE_CONTAINER;
NetResource.dwScope := RESOURCETYPE_DISK;//列举文件资源信息
//获取指定工作组的网络资源句柄
Res := WNetOpenEnum( RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum);
If Res <> NO_ERROR Then Exit; //执行失败
While True Do//列举指定工作组的网络资源
Begin
Count := $FFFFFFFF;//不限资源数目
BufSize := 8192;//缓冲区大小设置为8K
GetMem(Buf, BufSize);//申请内存,用于获取工作组信息
//获取计算机名称
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
If Res = ERROR_NO_MORE_ITEMS Then break;//资源列举完毕
If (Res <> NO_ERROR) then Exit;//执行失败
Temp := TNetResourceArray(Buf);
For Ind := 0 to Count - 1 do//列举工作组的计算机名称
Begin
//获取工作组的计算机名称,+2表示删除"//",如//wangfajun=>wangfajun
List.Add(Temp^.lpRemoteName + 2);
Inc(Temp);
End;
End;
Res := WNetCloseEnum(lphEnum);//关闭一次列举
If Res <> NO_ERROR Then exit;//执行失败
Result := True;
FreeMem(Buf);
end;
// 获得组内计算机的名称
procedure TForm1.Button2Click(Sender: TObject);
var
listGroup,listComputer:Tstringlist;
GroupName:string; //组名称
i:integer;
begin
GroupName:='WorkGroup';
listgroup:=Tstringlist.Create;
listcomputer:=Tstringlist.Create;
GetServerList(LISTgroup);
for i:=0 to listgroup.Count-1 do
begin
GetUsers(listgroup,LISTComputer);
showmessage(listcomputer.CommaText);
end;
listGroup.free;
listComputer.free;
end;
//获得计算机的IP地址
procedure TForm1.Button3Click(Sender: TObject);
var
wsadata:TWSAdata;
hostenthostent;
scomputername,sip:string;
begin
scomputername:=edit1.Text;
wsastartup(2,wsadata);
hostent:=gethostbyname(pchar(scomputername));
if hostent <> nil then
begin
with hostent^ do
sip:=format('%d.%d.%d.%d',[byte(h_addr^[0]),byte(h_addr^[1]),byte(h_addr^[2]),
byte(h_addr^[3])]);
end;
wsacleanup;
edit2.Text :=sip;
end;
//IP--计算机名
procedure TForm1.Button5Click(Sender: TObject);
var
WSAData:TWSAData;
pHostEnt;
sIP:string;
InetAddr:dword;
begin
WSAStartup(2,WSAData);
sip:=edit2.Text ;
inetaddr:=inet_addr(pchar(sip));
try
try
p:=gethostbyaddr(@inetaddr,length(sip),pf_inet);
memo1.LineS.Add('ip地址:'+sip);
memo1.LineS.add('计算机名:'+p^.h_name);
memo1.Lines.add('别名:'+string(P^.h_aliases));
memo1.lines.add('地址类型:'+chr(p^.h_addrtype+64));
memo1.lines.add('地址长度:'+inttostr(p^.h_length)+' 字节');
memo1.lines.add('----------------');
edit1.SetFocus ;
edit1.Text:='192.168.26.';
finally
WSACleanup;
//注意要释放资源,无论有否异常
end;
except
memo1.lines.add('无法得到该IP地址对应的计算机名!');
memo1.lines.add('----------------');
edit2.Text:='202.195.240.5';
end;
end;
end.