如果你问如何能得到网络邻居的列表,那是一个非常复杂的任务 <br>您将不得不和非常不友好的OLE对象打交道<br>下面两个过程可能会对你有用<br>procedure GetDomainList(TV:TTreeView);<br>var<br> a : Integer;<br> ErrCode : Integer;<br> NetRes : Array[0..1023] of TNetResource;<br> EnumHandle : THandle;<br> EnumEntries : DWord;<br><br> BufferSize : DWord;<br> s : string;<br> itm : TTreeNode;<br>begin<br> { Start here }<br> try<br> With NetRes[0] do begin<br> dwScope :=RESOURCE_GLOBALNET;<br> dwType :=RESOURCETYPE_ANY;<br> dwDisplayType :=RESOURCEDISPLAYTYPE_DOMAIN;<br> dwUsage :=RESOURCEUSAGE_CONTAINER;<br> lpLocalName :=NIL;<br> lpRemoteName :=NIL;<br> lpComment :=NIL;<br> lpProvider :=NIL;<br> end;<br> { get net root }<br> ErrCode:=WNetOpenEnum(<br> RESOURCE_GLOBALNET,<br> RESOURCETYPE_ANY,<br> RESOURCEUSAGE_CONTAINER,<br> @NetRes[0],<br><br> EnumHandle<br> );<br> If ErrCode=NO_ERROR then begin<br> EnumEntries:=1;<br> BufferSize:=SizeOf(NetRes);<br> ErrCode:=WNetEnumResource(<br> EnumHandle,<br> EnumEntries,<br> @NetRes[0],<br> BufferSize<br> );<br> WNetCloseEnum(EnumHandle);<br> ErrCode:=WNetOpenEnum(<br> RESOURCE_GLOBALNET,<br> RESOURCETYPE_ANY,<br> RESOURCEUSAGE_CONTAINER,<br> @NetRes[0],<br> EnumHandle<br> );<br> EnumEntries:=1024;<br> BufferSize:=SizeOf(NetRes);<br> ErrCode:=WNetEnumResource(<br> EnumHandle,<br> EnumEntries,<br> @NetRes[0],<br> BufferSize<br> );<br> IF ErrCode=No_Error then with TV do try<br> a:=0;<br> Items.BeginUpDate;<br> Items.Clear;<br><br> Itm:=Items.Add(TV.Selected,string(NetRes[0].lpProvider));<br> Itm.ImageIndex:=0;<br> Itm.SelectedIndex:=0;<br><br> { get domains }<br> While a How can I get the contents of the Neighborhood?<br><br>The following unit defines a component, TNetworkBrowser, which can be used<br>to enumerate all resources on the network in a hierarchical tree. The<br>actual browsing takes a long time (try opening "Entire Network" in Windows<br>Explorer). If you set the Scope property to nsContext, you'll see the list<br><br>of machines from the "Network Neighborhood" window.<br>下面的一个单元定义了一个组件. TNetworkBrowser, 可以枚举hierachical树上所有<br>的网络资源. 实际上浏览是要花费很长时间的,这您可以通过在WINDOWS资源管理器<br>中打开"整个网络" 来比较一下. 如果你设置SCOPE属性 为nsContext , 你就可以看到<br>和网络邻居中一样的机器列表<br><br>Yorai Aminov<br>El-On Software Systems<br><br>unit NetBrwsr;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;<br><br>type<br> TNetScope = (nsConnected, nsGlobal, nsRemembered, nsContext);<br><br> TNetResourceType = (nrAny, nrDisk, nrPrint);<br> TNetDisplay = (ndDomain, ndGeneric, ndServer, ndShare, ndFile, ndGroup,<br> ndNetwork, ndRoot, ndShareAdmin, ndDirectory, ndTree, ndNDSContainer);<br> TNetUsage = set of (nuConnectable, nuContainer);<br><br> TNetworkItems = class;<br><br> TNetworkItem = class<br> private<br> FScope: TNetScope;<br> FResourceType: TNetResourceType;<br> FDisplay: TNetDisplay;<br> FUsage: TNetUsage;<br> FLocalName: string;<br> FRemoteName: string;<br> FComment: string;<br> FProvider: string;<br> FSubItems: TNetworkItems;<br> public<br> constructor Create;<br> destructor Destroy; override;<br> property Scope: TNetScope read FScope;<br><br> property ResourceType: TNetResourceType read FResourceType;<br> property Display: TNetDisplay read FDisplay;<br> property Usage: TNetUsage read FUsage;<br> property LocalName: string read FLocalName;<br> property RemoteName: string read FRemoteName;<br> property Comment: string read FComment;<br> property Provider: string read FProvider;<br> property SubItems: TNetworkItems read FSubItems;<br> end;<br><br> TNetworkItems = class<br> private<br> FList: TList;<br> procedure SetItem(Index: Integer; Value: TNetworkItem);<br> function GetItem(Index: Integer): TNetworkItem;<br><br> function GetCount: Integer;<br> public<br> constructor Create;<br> destructor Destroy; override;<br> procedure Clear;<br> procedure Add(Item: TNetworkItem);<br> procedure Delete(Index: Integer);<br> property Items[Index: Integer]: TNetworkItem read GetItem write<br>SetItem; default;<br> property Count: Integer read GetCount;<br> end;<br><br> TNetworkBrowser = class(TComponent)<br> private<br> FItems: TNetworkItems;<br> FScope: TNetScope;<br> FResourceType: TNetResourceType;<br> FUsage: TNetUsage;<br> FActive: Boolean;<br> procedure Refresh;<br> procedure SetActive(Value: Boolean);<br> procedure SetScope(Value: TNetScope);<br><br> procedure SetResourceType(Value: TNetResourceType);<br> procedure SetUsage(Value: TNetUsage);<br> procedure EnumerateNet(NetItems: TNetworkItems; lpnr: PNetResource);<br> protected<br> public<br> constructor Create(AOwner: TComponent); override;<br> destructor Destroy; override;<br> procedure Open;<br> procedure Close;<br> property Items: TNetworkItems read FItems;<br> published<br> property Scope: TNetScope read FScope write SetScope default nsGlobal;<br> property ResourceType: TNetResourceType read FResourceType<br> write SetResourceType default nrAny;<br> property Usage: TNetUsage read FUsage write SetUsage default [];<br><br> property Active: Boolean read FActive write SetActive default False;<br> end;<br><br>implementation<br><br>type<br> PNetResourceArray = ^TNetResourceArray;<br> TNetResourceArray = array[0..0] of TNetResource;<br><br>{ TNetworkItem }<br><br>constructor TNetworkItem.Create;<br>begin<br> inherited;<br> FSubItems := TNetworkItems.Create;<br>end;<br><br>destructor TNetworkItem.Destroy;<br>begin<br> if FSubItems <> nil then<br> FSubItems.Free;<br> inherited;<br>end;<br><br>{ TNetworkItems }<br><br>constructor TNetworkItems.Create;<br>begin<br> inherited;<br> FList := TList.Create;<br>end;<br><br>destructor TNetworkItems.Destroy;<br><br>begin<br> Clear;<br> if FList <> nil then<br> FList.Free;<br> inherited;<br>end;<br><br>procedure TNetworkItems.SetItem(Index: Integer; Value: TNetworkItem);<br>begin<br> if (FList.Items[Index] <> nil) and (FList.Items[Index] <> Value) then<br> TNetworkItem(FList.Items[Index]).Free;<br> FList.Items[Index] := Value;<br>end;<br><br>function TNetworkItems.GetItem(Index: Integer): TNetworkItem;<br>begin<br> Result := TNetworkItem(FList.Items[Index]);<br>end;<br><br>procedure TNetworkItems.Clear;<br>begin<br> while Count > 0 do<br> Delete(0);<br>end;<br><br>procedure TNetworkItems.Add(Item: TNetworkItem);<br>begin<br><br> FList.Add(Item);<br>end;<br><br>procedure TNetworkItems.Delete(Index: Integer);<br>begin<br> if FList.Items[Index] <> nil then<br> TNetworkItem(FList.Items[Index]).Free;<br> FList.Delete(Index);<br>end;<br><br>function TNetworkItems.GetCount: Integer;<br>begin<br> if FList <> nil then<br> Result := FList.Count<br> else<br> Result := 0;<br>end;<br><br>{ TNetworkBrowser }<br><br>constructor TNetworkBrowser.Create(AOwner: TComponent);<br>begin<br> inherited Create(AOwner);<br> FItems := TNetworkItems.Create;<br> FScope := nsGlobal;<br> FResourceType := nrAny;<br> FUsage := [];<br>end;<br><br>destructor TNetworkBrowser.Destroy;<br>begin<br> if FItems <> nil then<br> FItems.Free;<br><br> inherited;<br>end;<br><br>procedure TNetworkBrowser.EnumerateNet(NetItems: TNetworkItems; lpnr:<br>PNetResource);<br>var<br> dwResult, dwResultEnum: Integer;<br> hEnum: THandle;<br> cbBuffer, cEntries, i: Integer;<br> nrArray: PNetResourceArray;<br> NewItem: TNetworkItem;<br> dwScope, dwType, dwUsage: Integer;<br>begin<br> cbBuffer := 16384;<br> cEntries := $FFFFFFFF;<br><br> case FScope of<br> nsConnected: dwScope := RESOURCE_CONNECTED;<br> nsGlobal: dwScope := RESOURCE_GLOBALNET;<br> nsRemembered: dwScope := RESOURCE_REMEMBERED;<br><br> nsContext: dwScope := RESOURCE_CONTEXT;<br> else<br> dwScope := RESOURCE_GLOBALNET;<br> end;<br> case FResourceType of<br> nrAny: dwType := RESOURCETYPE_ANY;<br> nrDisk: dwType := RESOURCETYPE_DISK;<br> nrPrint: dwType := RESOURCETYPE_PRINT;<br> else<br> dwType := RESOURCETYPE_ANY;<br> end;<br> dwUsage := 0;<br> if nuConnectable in FUsage then<br> dwUsage := dwUsage or RESOURCEUSAGE_CONNECTABLE;<br> if nuContainer in FUsage then<br> dwUsage := dwUsage or RESOURCEUSAGE_CONTAINER;<br><br> dwResult := WNetOpenEnum(dwScope, dwType, dwUsage, lpnr, hEnum);<br><br> if dwResult <> NO_ERROR then Exit;<br><br> GetMem(nrArray, cbBuffer);<br> repeat<br> dwResultEnum := WNetEnumResource(hEnum, cEntries, nrArray, cbBuffer);<br> if dwResultEnum = NO_ERROR then<br> for i := 0 to cEntries-1 do<br> begin<br> NewItem := TNetworkItem.Create;<br> case nrArray.dwScope of<br> RESOURCE_CONNECTED: NewItem.FScope := nsConnected;<br> RESOURCE_GLOBALNET: NewItem.FScope := nsGlobal;<br> RESOURCE_REMEMBERED: NewItem.FScope := nsRemembered;<br> RESOURCE_CONTEXT: NewItem.FScope := nsContext;<br><br> else<br> NewItem.FScope := nsGlobal;<br> end;<br> case nrArray.dwType of<br> RESOURCETYPE_ANY: NewItem.FResourceType := nrAny;<br> RESOURCETYPE_DISK: NewItem.FResourceType := nrDisk;<br> RESOURCETYPE_PRINT: NewItem.FResourceType := nrPrint;<br> else<br> NewItem.FResourceType := nrAny;<br> end;<br> case nrArray.dwDisplayType of<br> RESOURCEDISPLAYTYPE_GENERIC: NewItem.FDisplay := ndGeneric;<br> RESOURCEDISPLAYTYPE_DOMAIN: NewItem.FDisplay := ndDomain;<br><br> RESOURCEDISPLAYTYPE_SERVER: NewItem.FDisplay := ndServer;<br> RESOURCEDISPLAYTYPE_SHARE: NewItem.FDisplay := ndShare;<br> RESOURCEDISPLAYTYPE_FILE: NewItem.FDisplay := ndFile;<br> RESOURCEDISPLAYTYPE_GROUP: NewItem.FDisplay := ndGroup;<br> RESOURCEDISPLAYTYPE_NETWORK: NewItem.FDisplay := ndNetwork;<br> RESOURCEDISPLAYTYPE_ROOT: NewItem.FDisplay := ndRoot;<br> RESOURCEDISPLAYTYPE_SHAREADMIN: NewItem.FDisplay :=<br><br>ndShareAdmin;<br> RESOURCEDISPLAYTYPE_DIRECTORY: NewItem.FDisplay :=<br>ndDirectory;<br> RESOURCEDISPLAYTYPE_TREE: NewItem.FDisplay := ndTree;<br> RESOURCEDISPLAYTYPE_NDSCONTAINER: NewItem.FDisplay :=<br>ndNDSContainer;<br> else<br> NewItem.FDisplay := ndGeneric;<br> end;<br> NewItem.FUsage := [];<br> if nrArray.dwUsage and RESOURCEUSAGE_CONNECTABLE <> 0 then<br> Include(NewItem.FUsage, nuConnectable);<br> if nrArray.dwUsage and RESOURCEUSAGE_CONTAINER <> 0 then<br><br> Include(NewItem.FUsage, nuContainer);<br> NewItem.FLocalName := StrPas(nrArray.lpLocalName);<br> NewItem.FRemoteName := StrPas(nrArray.lpRemoteName);<br> NewItem.FComment := StrPas(nrArray.lpComment);<br> NewItem.FProvider := StrPas(nrArray.lpProvider);<br> NetItems.Add(NewItem);<br> // if container, call recursively<br> if (nuContainer in NewItem.FUsage) and (FScope <> nsContext) then<br> EnumerateNet(NewItem.FSubItems, @nrArray)<br><br> end;<br> until dwResultEnum = ERROR_NO_MORE_ITEMS;<br><br> FreeMem(nrArray);<br> WNetCloseEnum(hEnum);<br>end;<br><br>procedure TNetworkBrowser.Refresh;<br>begin<br> FItems.Clear;<br> if FActive then<br> EnumerateNet(FItems, nil);<br>end;<br><br>procedure TNetworkBrowser.SetActive(Value: Boolean);<br>begin<br> if Value <> FActive then<br> begin<br> FActive := Value;<br> Refresh;<br> end;<br>end;<br><br>procedure TNetworkBrowser.SetScope(Value: TNetScope);<br>begin<br> if Value <> FScope then<br> begin<br> FScope := Value;<br> Refresh;<br> end;<br>end;<br><br> <br>procedure TNetworkBrowser.SetResourceType(Value: TNetResourceType);<br>begin<br> if Value <> FResourceType then<br> begin<br> FResourceType := Value;<br> Refresh;<br> end;<br>end;<br><br>procedure TNetworkBrowser.SetUsage(Value: TNetUsage);<br>begin<br> if Value <> FUsage then<br> begin<br> FUsage := Value;<br> Refresh;<br> end;<br>end;<br><br>procedure TNetworkBrowser.Open;<br>begin<br> Active := True;<br>end;<br><br>procedure TNetworkBrowser.Close;<br>begin<br> Active := False;<br>end;<br><br>end.<br><br><br><br>----------------------------------------------------------<br><br><br>unit FindComp; <br><br> <br><br>interface <br><br> <br><br>uses <br><br> Windows, Classes; <br><br> <br><br>function FindComputers: DWORD; <br><br> <br><br>var <br><br> Computers: TStringList; <br><br> <br><br>implementation <br><br> <br><br>uses <br><br> SysUtils; <br><br> <br><br>const <br><br> MaxEntries = 250; <br><br> <br><br>function FindComputers: DWORD; <br><br> <br><br>var <br><br> EnumWorkGroupHandle, EnumComputerHandle: THandle; <br><br> EnumError: DWORD; <br><br> Network: TNetResource; <br><br> WorkGroupEntries, ComputerEntries: DWORD; <br><br> EnumWorkGroupBuffer, EnumComputerBuffer: array[1..MaxEntries] of TNetResource; <br><br> EnumBufferLength: DWORD; <br><br> I, J: DWORD; <br><br> <br><br>begin <br><br> <br><br> Computers.Clear; <br><br> <br><br> FillChar(Network, SizeOf(Network), 0); <br><br> with Network do <br><br> begin <br><br> dwScope := RESOURCE_GLOBALNET; <br><br> dwType := RESOURCETYPE_ANY; <br><br> dwUsage := RESOURCEUSAGE_CONTAINER; <br><br> end; <br><br> <br><br> EnumError := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, @Network, EnumWorkGroupHandle); <br><br> <br><br> if EnumError = NO_ERROR then <br><br> begin <br><br> WorkGroupEntries := MaxEntries; <br><br> EnumBufferLength := SizeOf(EnumWorkGroupBuffer); <br><br> EnumError := WNetEnumResource(EnumWorkGroupHandle, WorkGroupEntries, @EnumWorkGroupBuffer, EnumBufferLength); <br><br> <br><br> if EnumError = NO_ERROR then <br><br> begin <br><br> for I := 1 to WorkGroupEntries do <br><br> begin <br><br> EnumError := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, @EnumWorkGroupBuffer, EnumComputerHandle); <br><br> if EnumError = NO_ERROR then <br><br> begin <br><br> ComputerEntries := MaxEntries; <br><br> EnumBufferLength := SizeOf(EnumComputerBuffer); <br><br> EnumError := WNetEnumResource(EnumComputerHandle, ComputerEntries, @EnumComputerBuffer, EnumBufferLength); <br><br> if EnumError = NO_ERROR then <br><br> for J := 1 to ComputerEntries do <br><br> Computers.Add(Copy(EnumComputerBuffer[J].lpRemoteName, 3, Length(EnumComputerBuffer[J].lpRemoteName) - 2)); <br><br> WNetCloseEnum(EnumComputerHandle); <br><br> end; <br><br> end; <br><br> end; <br><br> WNetCloseEnum(EnumWorkGroupHandle); <br><br> end; <br><br> <br><br> if EnumError = ERROR_NO_MORE_ITEMS then <br><br> EnumError := NO_ERROR; <br><br> Result := EnumError; <br><br>end; <br><br> <br><br>initialization <br><br> Computers := TStringList.Create; <br><br>finalization <br><br> Computers.Free; <br><br>end. <br><br> <br>