Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
499 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


Reconnecting to network shares with the help of a Component.Go to Vassilis Perantzakis's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
A helpfull util for your networked apps.
Product:
Delphi 5.x (or higher)
Category:
VCL-General
Skill Level:
Scoring:
Last Update:
04/12/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet Delphi5 Delphi6 Network WinApi Windows VCL
Times Scored:
2
Visits:
2687
Uploader: Vassilis Perantzakis
Company: B-logica soft
Reference: www.devconone.com
 
Question/Problem/Abstract:
Ever lost a networked share and didn't know how to connect to it?
Well with this component you can search the network for a specific share containing a file or a directory and automatically reconnect to it.
Answer:



NOTE: IF YOU ALLREADY KNOW THE LOCATION OF THE SHARE YOU SHOULDN'T USE THIS COMPONENT AS IN LARGE NETWORKS WILL BE SLOW. THIS IS ONLY IF YOU DON'T KNOW THE EXACT LOCATION BUT CAN LOCATE IT BY USING A MARKER SUCH AS A SPECIFIC FILE OR FOLDER.

TIP: Use the BeforeConnect Event to specify whether a connection should be made.

unit Reconnect;

interface

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

type
  TSIsType=(itDir,itIniFile,itApp,itOther);
  TBeforeConnectEvent=procedure(Owner:TObject;AssignPath:string;var Accept:boolean) of object;
  TAfterConnectEvent=procedure(Owner:TObject;AssignedPath:string) of object;
  TOnFail=procedure(Owner:TObject;FailMessage:string) of object;
  TReconnect = class(TComponent)
  private
    { Private declarations }
    DidAssign:boolean;
    FItemToLookFor:String;
    FUserName:String;
    FPassword:String;
    FLetterToAssign:Char;
    FIsType:TSIsType;
    FOutputLabel:TLabel;
    FFailMessage:String;
    FBeforeConnect:TBeforeConnectEvent;
    FAfterConnect:TAfterConnectEvent;
    FOnFail:TOnFail;
    function DoEnum(NetResT:PNetResourceA):integer;
    function addbs(g:string):string;OVERLOAD;
    function addbs(g:string;SLASH:CHAR):string;OVERLOAD;
    function SearchFor(NetResT:NETRESOURCE;Path,param:string):boolean;
  protected
    { Protected declarations }
  public
    { Public declarations }
  published
    { Published declarations }
    function SearchAndAssign:boolean;
    property ItemToLookFor:String read FItemToLookFor write FItemToLookFor;
    property LetterToAssign:Char read FLetterToAssign write FLetterToAssign;
    property IsType:TSIsType read FIsType write FIsType default itDir;
    property OutputLabel:TLabel read FOutputLabel write FOutputLabel;
    property UserName:String read FUserName write FUserName;
    property Password:String read FPassword write FPassword;
    property BeforeConnect:TBeforeConnectEvent read FBeforeConnect write FBeforeConnect;
    property AfterConnect:TAfterConnectEvent read FAfterConnect write FAfterConnect;
    property OnFail:TOnFail read FOnFail write FOnFail;
  end;


procedure Register;

implementation


Function TReconnect.addbs(g:string;SLASH:CHAR):string;
begin
  g:=trim(g);
  if g<>''
  then begin
    if g[length(g)]<>SLASH
    then result:=g+SLASH
    else result:=g;
  end
  else result:=g;
end;

function TReconnect.addbs(g:string):string;
begin
result:=addbs(g,'\');
end;

function TReconnect.SearchFor(NetResT:NETRESOURCE;Path,param:string):boolean;
var
  cont:boolean;
  Exists:boolean;
begin
  Exists:=false;
  path:=addbs(path);
  SearchFor:=false;
  if IsType=itDir then
    Exists:=directoryExists(path+param);
  if IsType=itIniFile then
    Exists:=FileExists(path+param);
  if IsType=itApp then
    Exists:=FileExists(path+param);
  if IsType=itOther then
    Exists:=FileExists(path+param);
  if Exists then
    begin
      cont:=true;
      try
      if assigned(FBeforeConnect) then
        BeforeConnect(self,path,cont);
      except
        showmessage('Failed to call BeforeConnect.');
      end;
      if cont then
        begin
          try
          NetResT.lpLocalName:=pchar(string(FLetterToAssign)+':');
          WNetAddConnection2A(NetResT,pchar(UserName),pchar(Password),CONNECT_UPDATE_PROFILE);
          DidAssign:=true;
            try
            if assigned(FAfterConnect) then
              AfterConnect(self,path);
            except
              showmessage('Failed to call AfterConnect.');
            end;
          except on E: Exception do
            Showmessage(E.Message);
          end;
          SearchFor:=true;
        end;
    end;
end;

function TReconnect.DoEnum(NetResT:PNetResourceA):integer;
var
  EnumH:THandle;
  cnt:cardinal;
  buffsize:cardinal;
  NetResBuf:array [0..200] of NETRESOURCE;
  res:word;
  i:integer;
begin
  if DidAssign then
    exit;
  try
  cnt:=255;
  WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_DISK,0,NetResT,EnumH);
  res:=0;
  while (res=NO_ERROR) do
    begin
      buffsize:=sizeof(NetResBuf);
      res:=WNetEnumResource(EnumH,cnt,@NetResBuf,buffsize);
      for i:=0 to cnt-1 do
         begin
           if Assigned(OutputLabel) then
             begin
               OutputLabel.Caption:=NetResBuf[i].lpRemoteName;
               OutputLabel.Refresh;
             end;
           if NetResBuf[i].dwDisplayType=RESOURCEDISPLAYTYPE_SHARE then
             begin
               if not DidAssign then
                 if SearchFor(NetResBuf[i],string(NetResBuf[i].lpRemoteName),ItemToLookFor) then
                   begin
                     result:=0;
                     exit;
                   end;
             end;
           if (NetResBuf[i].dwScope=RESOURCEUSAGE_CONTAINER) then
        doEnum(@NetResBuf[i]);
        end;
    end;
  WNetCloseEnum(EnumH);
  result:=1;
  except on E: Exception do
    begin
      FFailMessage:=E.Message;
      if Assigned(FOnFail) then
        OnFail(Owner,FFailMessage);
      result:=0;
    end;
  end;
end;

function TReconnect.SearchAndAssign:boolean;
begin
  DidAssign:=false;
  DoEnum(nil);
  result:=true;
end;

procedure Register;
begin
  RegisterComponents('VNPVcls', [TReconnect]);
end;

end.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
Hans Gulö
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)