关于局域网的两个问题??? 请大家赐教!!!!!(20分)

  • 主题发起人 主题发起人 byflyer
  • 开始时间 开始时间
B

byflyer

Unregistered / Unconfirmed
GUEST, unregistred user!
1>如何编程实现获取局域网内所有计算机名???
2>如何编程实现判断局域网内哪台电脑为主机服务器???
 
好像是enum打头的API函数,具体不太清楚
 
====来自离线数据库=====

来自 :Croco 时间 :2000-07-12 17:57:28
根据计算机名得到IP地址的函数:
uses
WinSock;

function GetIPByName(AName: String): 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 := '';
StrPCopy(Buffer, AName);
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;

另外,取得工作组及成员的列表需要
WNetOpenEnum,WNetEnumResource,WNetCloseEnum
等函数来实现,比较麻烦,所以没人回答。
还是用现成的控件吧。偶找到一个netusers.zip,200多行。在下面贴出来吧。




--------------------------------------------------------------------------------

来自 :Croco 时间 :2000-07-12 18:01:47
{

TNetUsers VCL component for Delphi 2,3 & 4.

Author : Vijendra Kumar H.
Software Engineer
Bangalore, INDIA.
E-Mail : vijendrah@hotmail.com

Desc :
i) Retrieves all the network user names from the given domain
If the user don't specify the domain name, it will take from
the default domain(i.e. the domain to which the machine is
connected)
ii) Retrieves all Servers(Domains) on the Network

Note : This component is a freeware and can be used by anyone. If you
use it for a commercial products, please give credit to me.
You may modify the source code to your desire and if you make any
cool modifications, please send me the mods!!! :-)

For getting the default domain name, I had tried a lot and atlast
I have followed searching the machine name in each domain. If anyone
knows a better way of determining the default domain name, please
inform me.

Initial Release : 9th July 1998
Mods for Delphi 4 : 15th Mar 1999
Added GetServerList and Getting DefaultDomainName : 20th Mar 1999.
}

Unit NetUsers;

Interface

Uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

Type
TNetUsers = Class(TComponent)
Private
fServer : String;
Function GetUsers : Boolean;
Protected
Procedure SetServer(Server : String);
Public
List : TStringList;
Constructor Create(Owner:TComponent); Override;
Destructor Destroy; Override;
Function GetServerList : Boolean;
Function GetUserList : Boolean;
Published
Property Server :String Read fServer Write SetServer;
End;

PnetResourceArr = ^TNetResource;

Procedure Register;

Implementation

Procedure TNetUsers.SetServer(Server : String);
Begin
If fServer <> Server Then
fServer := Server;
End;

Constructor TNetUsers.Create(Owner:TComponent);
Begin
Inherited Create(Owner);
If Not (csDesigning in ComponentState) Then
Begin
List := TStringList.Create;
List.Sorted := True;
End;
End;

Destructor TNetUsers.Destroy;
Begin
If Not(csDesigning In ComponentState) Then
List.Destroy;
Inherited Destroy;
End;

Function TNetUsers.GetServerList : Boolean;
Type
{$H+}
PMyRec = ^MyRec;
MyRec = Record
dwScope : Integer;
dwType : Integer;
dwDisplayType : Integer;
dwUsage : Integer;
LocalName : String;
RemoteName : String;
Comment : String;
Provider : String;
End;
{H-}
Var
NetResource : TNetResource;
TempRec : PMyRec;
Buf : Pointer;
Count,
BufSize,
Res : DWORD;
lphEnum : THandle;
p : PNetResourceArr;
i,
j : SmallInt;
NetworkTypeList : TList;
Begin
Result := False;
NetworkTypeList := TList.Create;
List.Clear;
GetMem(Buf, 8192);
Try
Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_CONTAINER, Nil,lphEnum);
If Res <> 0 Then Raise Exception(Res);
Count := $FFFFFFFF;
BufSize := 8192;
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
If Res = ERROR_NO_MORE_ITEMS Then Exit;
If (Res <> 0) Then Raise Exception(Res);
P := PNetResourceArr(Buf);
For I := 0 To Count - 1 Do
Begin
New(TempRec);
TempRec^.dwScope := P^.dwScope;
TempRec^.dwType := P^.dwType ;
TempRec^.dwDisplayType := P^.dwDisplayType ;
TempRec^.dwUsage := P^.dwUsage ;
TempRec^.LocalName := StrPas(P^.lpLocalName);
TempRec^.RemoteName := StrPas(P^.lpRemoteName);
TempRec^.Comment := StrPas(P^.lpComment);
TempRec^.Provider := StrPas(P^.lpProvider);
NetworkTypeList.Add(TempRec);
Inc(P);
End;
Res := WNetCloseEnum(lphEnum);
If Res <> 0 Then Raise Exception(Res);
For J := 0 To NetworkTypeList.Count-1 Do
Begin
TempRec := NetworkTypeList.Items[J];
NetResource := TNetResource(TempRec^);
Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum);
If Res <> 0 Then Raise Exception(Res);
While true Do
Begin
Count := $FFFFFFFF;
BufSize := 8192;
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
If Res = ERROR_NO_MORE_ITEMS Then Break;
If (Res <> 0) Then Raise Exception(Res);
P := PNetResourceArr(Buf);
For I := 0 To Count - 1 Do
Begin
List.Add(P^.lpRemoteName);
Inc(P);
End;
End;
End;
Res := WNetCloseEnum(lphEnum);
If Res <> 0 Then Raise Exception(Res);
Result := True;
Finally
FreeMem(Buf);
NetworkTypeList.Destroy;
End;
End;

Function TNetUsers.GetUsers : Boolean;
Var
NetResource : TNetResource;
Buf : Pointer;
Count,
BufSize,
Res : DWord;
Ind : Integer;
lphEnum : THandle;
Temp : PNetResourceArr;
Begin
Result := False;
List.Clear;
GetMem(Buf, 8192);
Try
FillChar(NetResource, SizeOf(NetResource), 0);
NetResource.lpRemoteName := @fServer[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 <> 0 Then Exit;
While True Do
Begin
Count := $FFFFFFFF;
BufSize := 8192;
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
If Res = ERROR_NO_MORE_ITEMS Then Exit;
If (Res <> 0) then Exit;
Temp := PNetResourceArr(Buf);
For Ind := 0 to Count - 1 do
Begin
List.Add(Temp^.lpRemoteName + 2); { Add all the network usernames to List StringList }
Inc(Temp);
End;
End;
Res := WNetCloseEnum(lphEnum);
If Res <> 0 Then Raise Exception(Res);
Result := True;
Finally
FreeMem(Buf);
End;
End;

Function TNetUsers.GetUserList : Boolean;
Var
ServerList : TStringList;
TempInt,
Ind : Integer;
{$IFDEF VER100}
MaxLen : Integer;
{$ELSE}
MaxLen : Cardinal;
{$ENDIF}
Buf : PChar;
MachineName : String;
Begin
Result := False;
If fServer = '' Then
Begin
If GetServerList Then
Begin
ServerList := List;
GetMem(Buf,255);
MaxLen := 255;
GetComputerName(Buf, MaxLen);
MachineName := StrPas(Buf);
Freemem(Buf,255);
For Ind := 0 to ServerList.Count-1 Do
Begin
fServer := ServerList.Strings[Ind];
If GetUsers Then
If List.Find(MachineName, TempInt) Then
Begin
Result := True;
Exit;
End;
End;
End;
End
Else
Result := GetUsers;
End;

Procedure Register;
Begin
RegisterComponents('HVK Utility', [TNetUsers]);
End;

End.




--------------------------------------------------------------------------------

来自 :Croco 时间 :2000-07-12 18:17:26
试一下:
procedure TForm1.Button1Click(Sender: TObject);
begin
// 工作组
NetUsers1.GetServerList;
ListBox1.Clear;
ListBox1.Items.AddStrings(NetUsers1.List);

// 第一组成员
NetUsers1.Server := ListBox1.Items[0];
NetUsers1.GetUserList;
ListBox2.Clear;
ListBox2.Items.AddStrings(NetUsers1.List);
end;




--------------------------------------------------------------------------------

来自 :xiao.lit 时间 :2000-07-12 18:51:36
再这里先谢谢 Croco 大侠。

已经可以找到工作组和各组的成员名字,
但是怎样才能获得某一个成员所对应的 IP 地址呢?



 
一个特别点的方法:
知道对方的计算机名后,如果能确定对方的操作系统是WINDOWS的话,在程序里执行
NBTSTAT命令,把执行结果转到一个文本文件里去,然后再读取这个文件中特定的位置(这个
位置要自己去试几次才知道了),就可以知道的了:)
 
接受答案了.
 
后退
顶部