如何得知網路連接正常? |
答題得分者是:ddy
|
junlin
初階會員 發表:66 回覆:94 積分:42 註冊:2002-03-13 發送簡訊給我 |
|
turboted
版主 發表:95 回覆:754 積分:452 註冊:2002-07-23 發送簡訊給我 |
|
領航天使
站長 發表:12216 回覆:4186 積分:4084 註冊:2001-07-25 發送簡訊給我 |
|
ddy
站務副站長 發表:262 回覆:2105 積分:1169 註冊:2002-07-13 發送簡訊給我 |
junlin 兄
這個問題問得有點玄呀…:p
\\b 不能連就代表不正常呀…,可以就知道可以連呀,不是這樣嗎? 提供一段由程式可取得網路上的芳鄰,可藉此判斷某電腦是否存在
//在程式中動態取得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[i].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[i].dwType of RESOURCETYPE_ANY: NewItem.FResourceType := nrAny; RESOURCETYPE_DISK: NewItem.FResourceType := nrDisk; RESOURCETYPE_PRINT: NewItem.FResourceType := nrPrint; else NewItem.FResourceType := nrAny; end; case nrArray[i].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[i].dwUsage and RESOURCEUSAGE_CONNECTABLE <> 0 then Include(NewItem.FUsage, nuConnectable); if nrArray[i].dwUsage and RESOURCEUSAGE_CONTAINER <> 0 then Include(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 recursively if (nuContainer in NewItem.FUsage) and (FScope <> nsContext) then EnumerateNet(NewItem.FSubItems, @nrArray[i]) 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;噓~~~~~~沉思中… |
hagar
版主 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
小弟遇到如下的問題:
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; cbBuffer, cEntries 要宣告成 Cardinal 型態 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); 上述 cbBuffer, cEntries 兩個變數如果宣告成 integer 在上面這行會出現錯誤訊息 if dwResultEnum = NO_ERROR then for i := 0 to cEntries - 1 do begin NewItem := TNetworkItem.Create; case nrArray[i].dwScope of 在這上面這一行會出現如下的錯誤訊息: Record, Object or Class Type required 請問一下大家, 要如何解? 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; |
Clarinet
高階會員 發表:9 回覆:97 積分:126 註冊:2002-05-08 發送簡訊給我 |
引言: 小弟遇到如下的問題: //恕以下刪除hagar版主大人您好! 我沒有實際測試過,所以您自行試試看下列的作法。 將 cbBuffer, cEntries 宣告成 Cardinal 型態,然後在 ....... repeat cEntries :=dword(-1);//加入這一行即可 dwResultEnum := WNetEnumResource(hEnum, cEntries, nrArray, cbBuffer); ........ ............. 請您自行試試看吧! GPS(全球衛星定位系統) http://www.allgps.com
------
GPS(全球衛星定位系統) http://www.allgps.com |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |