请问如何动态得到网上邻居中的计算机名(我用WnetOpenEnum和WnetEnumResource只得到了工作组名)(50分)

  • 主题发起人 主题发起人 追命
  • 开始时间 开始时间

追命

Unregistered / Unconfirmed
GUEST, unregistred user!
我使用这两个API后,利用Tnetresource类的lpremotename得到工作组名,其他几个属性如
lplocalname为空,lpProvider的值为“Microsoft Network”,lpComment为空,不知如何
得到计算机名,请大虾指点
 
用TNetUsers或TNTUSERLIST控件很简单的。
http://vcl.vclxx.com/DELPHI/D32FREE/NETUSERS.ZIP
http://vcl.vclxx.com/DELPHI/D32FREE/NTUSERLIST.ZIP
 
下面是我做试验的一段代码,看一下吧.

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls ,winsock;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
function GetIP(Name:String) : string;
procedure GetDomainList();
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
GetDomainList();
end;

////////////////////////////////////////
function TForm1.GetIP(Name:String):String;
var
WSAData: TWSAData;
HostEnt: PHostEnt;
sIP: string;
begin
WSAStartup(2, WSAData);
HostEnt := gethostbyname(PChar(Name));
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;
result:= sIP;
end;
///////////////////////////////////////

procedure TForm1.GetDomainList();
var
i : Integer;
ErrCode : Integer;
NetRes : Array[0..1023] of TNetResource;
EnumHandle : Thandle;
EnumEntries : Dword;
BufferSize : Dword;
ComputerIP,ComputerName:String;
begin
//try
With NetRes[0] do begin
dwScope :=RESOURCE_GLOBALNET;
dwType :=RESOURCETYPE_ANY;
dwDisplayType :=RESOURCEDISPLAYTYPE_DOMAIN;
dwUsage :=RESOURCEUSAGE_CONNECTABLE;
lpLocalName :=NIL;
lpRemoteName :=NIL;
lpComment :=NIL;
lpProvider :=NIL;
end;
{ get net root }
ErrCode:=WNetOpenEnum(
RESOURCE_GLOBALNET,
RESOURCETYPE_ANY,
RESOURCEUSAGE_CONTAINER,
@NetRes[0],
EnumHandle
);

If ErrCode=NO_ERROR then begin
EnumEntries:=1;
BufferSize:=SizeOf(NetRes);
ErrCode:=WNetEnumResource(EnumHandle,EnumEntries,@NetRes[0],BufferSize);
WNetCloseEnum(EnumHandle);
ErrCode:=WNetOpenEnum(
RESOURCE_GLOBALNET,
RESOURCETYPE_ANY,
RESOURCEUSAGE_CONNECTABLE,
@NetRes[0],
EnumHandle
);
EnumEntries:=1024;
BufferSize:=SizeOf(NetRes);
ErrCode:=WNetEnumResource(
EnumHandle,
EnumEntries,
@NetRes[0],
BufferSize
);

for i:=0 to 1023 do
if NetRes.lpRemoteName='' then Exit
else begin
ComputerName:=Copy(NetRes.lpRemoteName,3,Length(NetRes.lpRemoteName)-2);
ComputerIP:=GetIP(ComputerName);
ShowMessage('计算机名' + NetRes.lpRemoteName + chr(13)
+ '计算机说明:' + NetRes.lpComment + chr(13)
+ 'IP地址:' + ComputerIP);
end;
end;
end;

end.
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部