对于学习很有帮助

源代码在线查看: net_liju.htm

软件大小: 2022 K
上传用户: siclj
关键词:
下载地址: 免注册下载 普通下载 VIP

相关代码

				如何在程序中动态取得Win95/98的网络邻居中的工作组及计算机名?				A{text-transform: none; text-decoration: none;}
				A:hover {color: #F77C02 ; text-decoration: underline;} }
				-->				.font1 {font-size:10.5pt; line-height:15pt; align=center}
				.font2 {font-size:9pt; line-height:13pt; }
				A{text-transform: none; text-decoration:none;color:#0000FF}
				a:hover {text-decoration:underline;color:#FFFFF}
				-->如何在程序中动态取得Win95/98的网络邻居中的工作组及计算机名?[本文不能保证绝对正确, 仅供参考]procedure GetDomainList(TV:TTreeView);vara : Integer;ErrCode : Integer;NetRes : Array[0..1023] of TNetResource;EnumHandle : THandle;EnumEntries : DWord;BufferSize : DWord;s : string;itm : TTreeNode;begin{ Start here }tryWith NetRes[0] do begindwScope :=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 beginEnumEntries:=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 trya:=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;interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;typeTNetScope = (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 = classprivateFScope: TNetScope;FResourceType: TNetResourceType;FDisplay: TNetDisplay;FUsage: TNetUsage;FLocalName: string;FRemoteName: string;FComment: string;FProvider: string;FSubItems: TNetworkItems;publicconstructor 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 = classprivateFList: TList;procedure SetItem(Index: Integer; Value: TNetworkItem);function GetItem(Index: Integer): TNetworkItem;function GetCount: Integer;publicconstructor Create;destructor Destroy; override;procedure Clear;procedure Add(Item: TNetworkItem);procedure Delete(Index: Integer);property Items[Index: Integer]: TNetworkItem read GetItem writeSetItem; default;property Count: Integer read GetCount;end;TNetworkBrowser = class(TComponent)privateFItems: 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);protectedpublicconstructor Create(AOwner: TComponent); override;destructor Destroy; override;procedure Open;procedure Close;property Items: TNetworkItems read FItems;publishedproperty Scope: TNetScope read FScope write SetScope default nsGlobal;property ResourceType: TNetResourceType read FResourceTypewrite SetResourceType default nrAny;property Usage: TNetUsage read FUsage write SetUsage default [];property Active: Boolean read FActive write SetActive default False;end;implementationtypePNetResourceArray = ^TNetResourceArray;TNetResourceArray = array[0..0] of TNetResource;{ TNetworkItem }constructor TNetworkItem.Create;begininherited;FSubItems := TNetworkItems.Create;end;destructor TNetworkItem.Destroy;beginif FSubItems <> nil thenFSubItems.Free;inherited;end;{ TNetworkItems }constructor TNetworkItems.Create;begininherited;FList := TList.Create;end;destructor TNetworkItems.Destroy;beginClear;if FList <> nil thenFList.Free;inherited;end;procedure TNetworkItems.SetItem(Index: Integer; Value: TNetworkItem);beginif (FList.Items[Index] <> nil) and (FList.Items[Index] <> Value) thenTNetworkItem(FList.Items[Index]).Free;FList.Items[Index] := Value;end;function TNetworkItems.GetItem(Index: Integer): TNetworkItem;beginResult := TNetworkItem(FList.Items[Index]);end;procedure TNetworkItems.Clear;beginwhile Count > 0 doDelete(0);end;procedure TNetworkItems.Add(Item: TNetworkItem);beginFList.Add(Item);end;procedure TNetworkItems.Delete(Index: Integer);beginif FList.Items[Index] <> nil thenTNetworkItem(FList.Items[Index]).Free;FList.Delete(Index);end;function TNetworkItems.GetCount: Integer;beginif FList <> nil thenResult := FList.CountelseResult := 0;end;{ TNetworkBrowser }constructor TNetworkBrowser.Create(AOwner: TComponent);begininherited Create(AOwner);FItems := TNetworkItems.Create;FScope := nsGlobal;FResourceType := nrAny;FUsage := [];end;destructor TNetworkBrowser.Destroy;beginif FItems <> nil thenFItems.Free;inherited;end;procedure TNetworkBrowser.EnumerateNet(NetItems: TNetworkItems; lpnr:PNetResource);vardwResult, dwResultEnum: Integer;hEnum: THandle;cbBuffer, cEntries, i: Integer;nrArray: PNetResourceArray;NewItem: TNetworkItem;dwScope, dwType, dwUsage: Integer;begincbBuffer := 16384;cEntries := $FFFFFFFF;case FScope ofnsConnected: dwScope := RESOURCE_CONNECTED;nsGlobal: dwScope := RESOURCE_GLOBALNET;nsRemembered: dwScope := RESOURCE_REMEMBERED;nsContext: dwScope := RESOURCE_CONTEXT;elsedwScope := RESOURCE_GLOBALNET;end;case FResourceType ofnrAny: dwType := RESOURCETYPE_ANY;nrDisk: dwType := RESOURCETYPE_DISK;nrPrint: dwType := RESOURCETYPE_PRINT;elsedwType := RESOURCETYPE_ANY;end;dwUsage := 0;if nuConnectable in FUsage thendwUsage := dwUsage or RESOURCEUSAGE_CONNECTABLE;if nuContainer in FUsage thendwUsage := dwUsage or RESOURCEUSAGE_CONTAINER;dwResult := WNetOpenEnum(dwScope, dwType, dwUsage, lpnr, hEnum);if dwResult <> NO_ERROR then Exit;GetMem(nrArray, cbBuffer);repeatdwResultEnum := WNetEnumResource(hEnum, cEntries, nrArray, cbBuffer);if dwResultEnum = NO_ERROR thenfor i := 0 to cEntries-1 dobeginNewItem := TNetworkItem.Create;case nrArray[i].dwScope ofRESOURCE_CONNECTED: NewItem.FScope := nsConnected;RESOURCE_GLOBALNET: NewItem.FScope := nsGlobal;RESOURCE_REMEMBERED: NewItem.FScope := nsRemembered;RESOURCE_CONTEXT: NewItem.FScope := nsContext;elseNewItem.FScope := nsGlobal;end;case nrArray[i].dwType ofRESOURCETYPE_ANY: NewItem.FResourceType := nrAny;RESOURCETYPE_DISK: NewItem.FResourceType := nrDisk;RESOURCETYPE_PRINT: NewItem.FResourceType := nrPrint;elseNewItem.FResourceType := nrAny;end;case nrArray[i].dwDisplayType ofRESOURCEDISPLAYTYPE_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;elseNewItem.FDisplay := ndGeneric;end;NewItem.FUsage := [];if nrArray[i].dwUsage and RESOURCEUSAGE_CONNECTABLE <> 0 thenInclude(NewItem.FUsage, nuConnectable);if nrArray[i].dwUsage and RESOURCEUSAGE_CONTAINER <> 0 thenInclude(NewItem.FUsage, nuContainer);NewItem.FLocalName := StrPas(nrArray[i].lpLocalName);NewItem.FRemoteName := StrPas(nrArray[i].lpRemoteName);NewItem.FComment := StrPas(nrArray[i].lpComment);NewItem.FProvider := StrPas(nrArray[i].lpProvider);NetItems.Add(NewItem);// if container, call recursivelyif (nuContainer in NewItem.FUsage) and (FScope <> nsContext) thenEnumerateNet(NewItem.FSubItems, @nrArray[i])end;until dwResultEnum = ERROR_NO_MORE_ITEMS;FreeMem(nrArray);WNetCloseEnum(hEnum);end;procedure TNetworkBrowser.Refresh;beginFItems.Clear;if FActive thenEnumerateNet(FItems, nil);end;procedure TNetworkBrowser.SetActive(Value: Boolean);beginif Value <> FActive thenbeginFActive := Value;Refresh;end;end;procedure TNetworkBrowser.SetScope(Value: TNetScope);beginif Value <> FScope thenbeginFScope := Value;Refresh;end;end;procedure TNetworkBrowser.SetResourceType(Value: TNetResourceType);beginif Value <> FResourceType thenbeginFResourceType := Value;Refresh;end;end;procedure TNetworkBrowser.SetUsage(Value: TNetUsage);beginif Value <> FUsage thenbeginFUsage := Value;Refresh;end;end;procedure TNetworkBrowser.Open;beginActive := True;end;procedure TNetworkBrowser.Close;beginActive := False;end;end.==============================================http://leonyxl.yeah.net/ ==============================================版主:西安      梁  明日期:99-07-29E-mail:delphifan@990.net			

相关资源