这个问题很难吗?我都问第三次了!!!!(50分)

  • 主题发起人 主题发起人 xie_huan
  • 开始时间 开始时间
X

xie_huan

Unregistered / Unconfirmed
GUEST, unregistred user!
在WIN98中,运行 net view 命令便可得到本地局域网上的所有计算机的名称,那在程序
中如何实现?还有可不可以连IP也一起得到?
我在参考书上看过好象有这么一个函数MOMAND()的,可惜没有具体的用法,
[:(] [:(] [:(]
 
嫌分少??
全部家当都给了[:(!][:(!]
 
查找所有工作组内计算机
var

Computer : Array[1..500] of String[25];

ComputerCount : Integer;





procedure FindAllComputers(Workgroup : String);

Var

EnumHandle : THandle;

WorkgroupRS : TNetResource;

Buf : Array[1..500] of TNetResource;

BufSize : Integer;

Entries : Integer;

Result : Integer;



begin

ComputerCount := 0;



Workgroup := Workgroup + #0;



FillChar(WorkgroupRS, SizeOf(WorkgroupRS) , 0);

With WorkgroupRS do begin

dwScope := 2;

dwType := 3;

dwDisplayType := 1;

dwUsage := 2;

lpRemoteName := @Workgroup[1];

end;



WNetOpenEnum( RESOURCE_GLOBALNET,

RESOURCETYPE_ANY,

0,

@WorkgroupRS,

EnumHandle );



Repeat

Entries := 1;

BufSize := SizeOf(Buf);



Result :=

WNetEnumResource( EnumHandle,

Entries,

@Buf,

BufSize );

If (Result = NO_ERROR) and (Entries = 1) then begin

Inc( ComputerCount );

Computer[ ComputerCount ] := StrPas(Buf[1].lpRemoteName);

end;

Until (Entries <> 1) or (Result <> NO_ERROR);



WNetCloseEnum( EnumHandle );

end; { Find All Computers }

 
取得Win95/98的网络邻居中的工作组及计算机名

procedure GetDomainList(TV:TTreeView);
var
a : Integer;
ErrCode : Integer;
NetRes : Array[0..1023] of TNetResource;
EnumHandle : THandle;
EnumEntries : DWord;
BufferSize : DWord;
s : string;
itm : TTreeNode;
begin
{ Start here }
try
With NetRes[0] do begin
dwScope :=RESOURCE_GLOBALNET;
dwType :=RESOURCETYPE_ANY;
dwDisplayType :=RESOURCEDISPLAYTYPE_DOMAIN;
dwUsage :=RESOURCEUSAGE_CONTAINER;
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_CONTAINER,
@NetRes[0],
EnumHandle
);
EnumEntries:=1024;
BufferSize:=SizeOf(NetRes);
ErrCode:=WNetEnumResource(
EnumHandle,
EnumEntries,
@NetRes[0],
BufferSize
);
IF ErrCode=No_Error then with TV do try
a:=0;
Items.BeginUpDate;
Items.Clear;
Itm:=Items.Add(TV.Selected,string(NetRes[0].lpProvider));
Itm.ImageIndex:=0;
Itm.SelectedIndex:=0;
{ get domains }
下面的一个单元定义了一个组件. TNetworkBrowser, 可以枚举hierachical树上所有的网络资源. 实际上浏览是要花费很长时间的,这您可以通过在WINDOWS资源管理器中打开"整个网络" 来比较一下. 如果你设置SCOPE属性 为nsContext , 你就可以看到和网络邻居中一样的机器列表.
unit NetBrwsr;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TNetScope = (nsConnected, nsGlobal, nsRemembered, nsContext);
TNetResourceType = (nrAny, nrDisk, nrPrint);
TNetDisplay = (ndDomain, ndGeneric, ndServer, ndShare, ndFile, ndGroup,
ndNetwork, ndRoot, ndShareAdmin, ndDirectory, ndTree, ndNDSContainer);
TNetUsage = set of (nuConnectable, nuContainer);
TNetworkItems = class;
TNetworkItem = class
private
FScope: TNetScope;
FResourceType: TNetResourceType;
FDisplay: TNetDisplay;
FUsage: TNetUsage;
FLocalName: string;
FRemoteName: string;
FComment: string;
FProvider: string;
FSubItems: TNetworkItems;
public
constructor Create;
destructor Destroy; override;
property Scope: TNetScope read FScope;
property ResourceType: TNetResourceType read FResourceType;
property Display: TNetDisplay read FDisplay;
property Usage: TNetUsage read FUsage;
property LocalName: string read FLocalName;
property RemoteName: string read FRemoteName;
property Comment: string read FComment;
property Provider: string read FProvider;
property SubItems: TNetworkItems read FSubItems;
end;
TNetworkItems = class
private
FList: TList;
procedure SetItem(Index: Integer; Value: TNetworkItem);
function GetItem(Index: Integer): TNetworkItem;
function GetCount: Integer;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure Add(Item: TNetworkItem);
procedure Delete(Index: Integer);
property Items[Index: Integer]: TNetworkItem read GetItem write
SetItem; default;
property Count: Integer read GetCount;
end;
TNetworkBrowser = class(TComponent)
private
FItems: TNetworkItems;
FScope: TNetScope;
FResourceType: TNetResourceType;
FUsage: TNetUsage;
FActive: Boolean;
procedure Refresh;
procedure SetActive(Value: Boolean);
procedure SetScope(Value: TNetScope);
procedure SetResourceType(Value: TNetResourceType);
procedure SetUsage(Value: TNetUsage);
procedure EnumerateNet(NetItems: TNetworkItems; lpnr: PNetResource);
protected
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Open;
procedure Close;
property Items: TNetworkItems read FItems;
published
property Scope: TNetScope read FScope write SetScope default nsGlobal;
property ResourceType: TNetResourceType read FResourceType
write SetResourceType default nrAny;
property Usage: TNetUsage read FUsage write SetUsage default [];
property Active: Boolean read FActive write SetActive default False;
end;
implementation
type
PNetResourceArray = ^TNetResourceArray;

TNetResourceArray = array[0..0] of TNetResource;
{ TNetworkItem }
constructor TNetworkItem.Create;
begin
inherited;
FSubItems := TNetworkItems.Create;
end;
destructor TNetworkItem.Destroy;
begin
if FSubItems <> nil then
FSubItems.Free;
inherited;
end;
{ TNetworkItems }
constructor TNetworkItems.Create;
begin
inherited;
FList := TList.Create;
end;
destructor TNetworkItems.Destroy;
begin
Clear;
if FList <> nil then
FList.Free;
inherited;
end;
procedure TNetworkItems.SetItem(Index: Integer; Value: TNetworkItem);
begin
if (FList.Items[Index] <> nil) and (FList.Items[Index] <> Value) then
TNetworkItem(FList.Items[Index]).Free;
FList.Items[Index] := Value;
end;
function TNetworkItems.GetItem(Index: Integer): TNetworkItem;
begin
Result := TNetworkItem(FList.Items[Index]);
end;
procedure TNetworkItems.Clear;
begin
while Count > 0 do
Delete(0);
end;
procedure TNetworkItems.Add(Item: TNetworkItem);
begin
FList.Add(Item);
end;
procedure TNetworkItems.Delete(Index: Integer);
begin
if FList.Items[Index] <> nil then
TNetworkItem(FList.Items[Index]).Free;
FList.Delete(Index);
end;
function TNetworkItems.GetCount: Integer;
begin
if FList <> nil then
Result := FList.Count
else
Result := 0;
end;
{ TNetworkBrowser }
constructor TNetworkBrowser.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FItems := TNetworkItems.Create;
FScope := nsGlobal;
FResourceType := nrAny;
FUsage := [];
end;
destructor TNetworkBrowser.Destroy;
begin
if FItems <> nil then
FItems.Free;
inherited;
end;
procedure TNetworkBrowser.EnumerateNet(NetItems: TNetworkItems; lpnr:
PNetResource);
var
dwResult, dwResultEnum: Integer;
hEnum: THandle;
cbBuffer, cEntries, i: Integer;
nrArray: PNetResourceArray;
NewItem: TNetworkItem;
dwScope, dwType, dwUsage: Integer;
begin
cbBuffer := 16384;
cEntries := $FFFFFFFF;
case FScope of
nsConnected: dwScope := RESOURCE_CONNECTED;
nsGlobal: dwScope := RESOURCE_GLOBALNET;
nsRemembered: dwScope := RESOURCE_REMEMBERED;
nsContext: dwScope := RESOURCE_CONTEXT;
else
dwScope := RESOURCE_GLOBALNET;
end;
case FResourceType of
nrAny: dwType := RESOURCETYPE_ANY;
nrDisk: dwType := RESOURCETYPE_DISK;
nrPrint: dwType := RESOURCETYPE_PRINT;
else
dwType := RESOURCETYPE_ANY;
end;
dwUsage := 0;
if nuConnectable in FUsage then
dwUsage := dwUsage or RESOURCEUSAGE_CONNECTABLE;
if nuContainer in FUsage then
dwUsage := dwUsage or RESOURCEUSAGE_CONTAINER;
dwResult := WNetOpenEnum(dwScope, dwType, dwUsage, lpnr, hEnum);
if dwResult <> NO_ERROR then Exit;
GetMem(nrArray, cbBuffer);
repeat
dwResultEnum := WNetEnumResource(hEnum, cEntries, nrArray, cbBuffer);
if dwResultEnum = NO_ERROR then
for i := 0 to cEntries-1 do
begin
NewItem := TNetworkItem.Create;
case nrArray.dwScope of
RESOURCE_CONNECTED: NewItem.FScope := nsConnected;
RESOURCE_GLOBALNET: NewItem.FScope := nsGlobal;
RESOURCE_REMEMBERED: NewItem.FScope := nsRemembered;
RESOURCE_CONTEXT: NewItem.FScope := nsContext;
else
NewItem.FScope := nsGlobal;
end;
case nrArray.dwType of
RESOURCETYPE_ANY: NewItem.FResourceType := nrAny;
RESOURCETYPE_DISK: NewItem.FResourceType := nrDisk;
RESOURCETYPE_PRINT: NewItem.FResourceType := nrPrint;
else
NewItem.FResourceType := nrAny;
end;
case nrArray.dwDisplayType of
RESOURCEDISPLAYTYPE_GENERIC: NewItem.FDisplay := ndGeneric;
RESOURCEDISPLAYTYPE_DOMAIN: NewItem.FDisplay := ndDomain;
RESOURCEDISPLAYTYPE_SERVER: NewItem.FDisplay := ndServer;
RESOURCEDISPLAYTYPE_SHARE: NewItem.FDisplay := ndShare;
RESOURCEDISPLAYTYPE_FILE: NewItem.FDisplay := ndFile;
RESOURCEDISPLAYTYPE_GROUP: NewItem.FDisplay := ndGroup;
RESOURCEDISPLAYTYPE_NETWORK: NewItem.FDisplay := ndNetwork;
RESOURCEDISPLAYTYPE_ROOT: NewItem.FDisplay := ndRoot;
RESOURCEDISPLAYTYPE_SHAREADMIN: NewItem.FDisplay :=
ndShareAdmin;
RESOURCEDISPLAYTYPE_DIRECTORY: NewItem.FDisplay :=
ndDirectory;
RESOURCEDISPLAYTYPE_TREE: NewItem.FDisplay := ndTree;
RESOURCEDISPLAYTYPE_NDSCONTAINER: NewItem.FDisplay :=
ndNDSContainer;
else
NewItem.FDisplay := ndGeneric;
end;
NewItem.FUsage := [];
if nrArray.dwUsage and RESOURCEUSAGE_CONNECTABLE <> 0 then
Include(NewItem.FUsage, nuConnectable);
if nrArray.dwUsage and RESOURCEUSAGE_CONTAINER <> 0 then
Include(NewItem.FUsage, nuContainer);
NewItem.FLocalName := StrPas(nrArray.lpLocalName);
NewItem.FRemoteName := StrPas(nrArray.lpRemoteName);
NewItem.FComment := StrPas(nrArray.lpComment);
NewItem.FProvider := StrPas(nrArray.lpProvider);
NetItems.Add(NewItem);
// if container, call recursively
if (nuContainer in NewItem.FUsage) and (FScope <> nsContext) then
EnumerateNet(NewItem.FSubItems, @nrArray)
end;
until dwResultEnum = ERROR_NO_MORE_ITEMS;
FreeMem(nrArray);
WNetCloseEnum(hEnum);
end;
procedure TNetworkBrowser.Refresh;
begin
FItems.Clear;
if FActive then
EnumerateNet(FItems, nil);
end;
procedure TNetworkBrowser.SetActive(Value: Boolean);
begin
if Value <> FActive then
begin
FActive := Value;
Refresh;
end;
end;
procedure TNetworkBrowser.SetScope(Value: TNetScope);
begin
if Value <> FScope then
begin
FScope := Value;
Refresh;
end;
end;
procedure TNetworkBrowser.SetResourceType(Value: TNetResourceType);
begin
if Value <> FResourceType then
begin
FResourceType := Value;
Refresh;
end;
end;
procedure TNetworkBrowser.SetUsage(Value: TNetUsage);
begin
if Value <> FUsage then
begin
FUsage := Value;
Refresh;
end;
end;
procedure TNetworkBrowser.Open;
begin
Active := True;
end;
procedure TNetworkBrowser.Close;
begin
Active := False;
end;
end.
 
这有何难,下面的例子既可得工作组又可得用户名和ip,你自己加个TListView就行了

procedure TForm1.FindIpClick(Sender: TObject);
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
lpVerInfo : OSVERSIONINFO;

NetRes,NetRes_User : Array[0..9] of TNetResource;
ErrCode:integer;
EnumHandle : THandle;
I,J,K,Group_No,Total : Integer;
EnumEntries : DWord;
BufferSize : DWord;

phe : PHostEnt;
pptr : PaPInAddr;
Buffer : array [0..63] of char;
GInitData : TWSADATA;
IpAddress,GroupName,UserName,lpProvider : string;
p :pchar;
ListItem: TListItem;
begin
StatusBar1.Panels[0].Text := '正在查询所有网上邻居......';
delay(1);
Total := 0;
With NetRes[0] do
begin
dwScope :=RESOURCE_GLOBALNET;
dwType :=RESOURCETYPE_ANY;
dwDisplayType :=RESOURCEDISPLAYTYPE_DOMAIN;
dwUsage :=RESOURCEUSAGE_CONTAINER;
lpLocalName :=NIL;
lpRemoteName :=NIL;
lpComment :=NIL;
lpProvider :=NIL;
end;
ErrCode:=WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,RESOURCEUSAGE_CONTAINER,@NetRes[0],EnumHandle);
If ErrCode = NO_ERROR then
begin
EnumEntries:=100;
BufferSize:=SizeOf(NetRes);
//查找组
ErrCode:=WNetEnumResource(EnumHandle,EnumEntries,@NetRes[0],BufferSize);
lpProvider := NetRes[0].lpProvider;
WNetCloseEnum(EnumHandle);
with ListView1 do
begin
Items.Clear;
Group_No := 0;
//组

//由于NT或2000多了一个"Microsoft Windows 网络"组,因此要多一次查询组操作
//如果是win9X,下面这段不要
lpVerInfo.dwOSVersionInfoSize := SizeOf(lpVerInfo);
GetVersionEx(lpVerInfo);
if (lpVerInfo.dwPlatformId = VER_PLATFORM_WIN32_NT) then
begin
ErrCode:=WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,
RESOURCEUSAGE_CONTAINER,@NetRes[Group_No],EnumHandle);
EnumEntries:=1024;
BufferSize:=SizeOf(NetRes_User);
ErrCode:=WNetEnumResource(EnumHandle,EnumEntries,@NetRes[Group_No],BufferSize);
end;
//

while ((NetRes[Group_No].lpRemoteName<>nil) and (NetRes[Group_No].lpProvider = lpProvider)) do //遍历组
begin
//组用户
GroupName := NetRes[Group_No].lpRemoteName;
ListItem := Items.Add;
Listitem.Caption := GroupName;
ErrCode:=WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,
RESOURCEUSAGE_CONTAINER,@NetRes[Group_No],EnumHandle);
EnumEntries:=1024;
BufferSize:=SizeOf(NetRes_User);
ErrCode:=WNetEnumResource(EnumHandle,EnumEntries,@NetRes_User[0],BufferSize);
IF ErrCode=No_Error then
begin
I:=0;
while ((NetRes_User.lpRemoteName<>nil) and (NetRes_User.lpProvider = lpProvider)) do //遍历组用户
begin
UserName := Copy(NetRes_User.lpRemoteName,3,Length(NetRes_User.lpRemoteName));
p := PChar(UserName);
For J:=0 to Length(UserName)-1 do
Buffer[J] := p[J];
Buffer[J] := #0;
//查该机器的ip
WSAStartup($101, GInitData);
IpAddress := '';
phe :=GetHostByName(Buffer);
if phe = nil then break;
ListItem.SubItems.Add(UserName);
pptr := PaPInAddr(Phe^.h_addr_list);
K := 0;
while pptr^[K] <> nil do
begin
IpAddress := StrPas(inet_ntoa(pptr^[K]^));
ListItem.SubItems.Add(IpAddress);
Inc(K);
end;
ListItem.SubItems.Add(NetRes_User.lpComment);
WSACleanup;

I:=I+1;
Total := Total + 1;
ListItem := Items.Add;
Listitem.Caption := '';
end;
end;
ListView1.Items.Delete(ListView1.Items.IndexOf(ListItem));
Group_No := Group_No + 1;
end;
end;
end;
StatusBar1.Panels[0].Text :='共找到' + IntToStr(Total) + '个用户';
end;
 
PInAddr、PHostEnt报未定义
 
在uses里加上WinSock就行了
 
后退
顶部