Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question(* This component was downloaded from the About Delphi Programming site @author(http://delphi.about.com) TNetDrive is a non-visual component that connects a network path to a drive name. Find more: http://delphi.about.com/library/weekly/aa061503a.htm @lastmod(DELPHIPROG, le 01/08/2006 17:15:57) *) unit NetDrive; {$WARN UNIT_PLATFORM OFF} interface uses Windows, SysUtils, Classes, Controls, Forms, Dialogs; type TNetDrive = class (TComponent) private FErrorString: string; FDrive: string; FOnDisconnect: TNotifyEvent; FOnConnect: TNotifyEvent; procedure SetOnConnect(const Value: TNotifyEvent); procedure SetOnDisconnect(const Value: TNotifyEvent); function FreeDriveName: string; function DriveExists(const ADrive: string): boolean; protected public constructor Create(AOwner: TComponent); override; function Connect(const AResource, AUser, APassword: string): string; function Disconnect: boolean; property Drive: stringread FDrive; property ErrorString: stringread FErrorString; published property OnConnect: TNotifyEvent read FOnConnect write SetOnConnect; property OnDisconnect: TNotifyEvent read FOnDisconnect write SetOnDisconnect; end; procedureRegister; implementation uses FileCtrl; resourcestring rsERROR_ACCESS_DENIED = 'Access to the network resource was denied.'; rsERROR_ALREADY_ASSIGNED = 'The local device specified by lpLocalName is already connected to a network resource.'; rsERROR_BAD_DEV_TYPE = 'The type of local device and the type of network resource do not match.'; rsERROR_BAD_DEVICE = 'The value specified by lpLocalName is invalid.'; rsERROR_BAD_NET_NAME = 'The value specified by lpRemoteName is not acceptable to any network resource provider. The resource name is invalid, or the named resource cannot be located.'; rsERROR_BAD_PROFILE = 'The user profile is in an incorrect format.'; rsERROR_BAD_PROVIDER = 'The value specified by lpProvider does not match any provider.'; rsERROR_BUSY = 'The router or provider is busy, possibly initializing. The caller should retry.'; rsERROR_CANCELLED = 'The attempt to make the connection was cancelled by the user through a dialog box from one of the network resource providers, or by a called resource.'; rsERROR_CANNOT_OPEN_PROFILE = 'The system is unable to open the user profile to process persistent connections.'; rsERROR_DEVICE_ALREADY_REMEMBERED = 'An entry for the device specified in lpLocalName is already in the user profile.'; rsERROR_EXTENDED_ERROR = 'A network-specific error occured. Call the WNetGetLastError function to get a description of the error.'; rsERROR_INVALID_PASSWORD = 'The specified password is invalid.'; rsERROR_NO_NET_OR_BAD_PATH = 'A network component has not started, or the specified name could not be handled.'; rsERROR_NO_NETWORK = 'There is no network present.'; rsERROR_UNKNOWN_ERROR = 'An unknown error has occured attempting to connect to %s.'; rsERROR_DEVICE_IN_USE = 'The device is in use by an active process and cannot be disconnected.'; rsERROR_NOT_CONNECTED = 'The name specified by the lpName parameter is not a redirected device, or the system is not currently connected to the device specified by the parameter.'; rsERROR_OPEN_FILES = 'There are open files, and the fForce parameter is FALSE.'; procedure Register; begin RegisterComponents('delphi.about.com', [TNetDrive]); end; { TNetDrive } function TNetDrive.Connect(const AResource, AUser, APassword: string): string; var n: NETRESOURCE; i: integer; begin FErrorString : = EmptyStr; FDrive := ''; n.dwScope := RESOURCE_GLOBALNET; n.dwType := RESOURCETYPE_DISK; n.dwDisplayType := RESOURCEDISPLAYTYPE_GENERIC; n.dwUsage := RESOURCEUSAGE_CONNECTABLE; n.lpLocalName := PChar(FreeDriveName); n.lpRemoteName := PChar(AResource); n.lpComment := ''; n.lpProvider := ''; i := WNetAddConnection2(n, PChar(APassword), PChar(AUser), 0); case i of NO_ERROR: begin Sleep(500); FDrive : = n.lpLocalName; repeatuntil DriveExists(FDrive); if Assigned(FOnConnect) then FOnConnect(self); end; ERROR_ACCESS_DENIED: FErrorString := rsERROR_ACCESS_DENIED; ERROR_ALREADY_ASSIGNED: FErrorString := rsERROR_ALREADY_ASSIGNED; ERROR_BAD_DEV_TYPE:FErrorString := rsERROR_BAD_DEV_TYPE; ERROR_BAD_DEVICE:FErrorString := rsERROR_BAD_DEVICE; ERROR_BAD_NET_NAME:FErrorString := rsERROR_BAD_NET_NAME; ERROR_BAD_PROFILE:FErrorString := rsERROR_BAD_PROFILE; ERROR_BAD_PROVIDER:FErrorString := rsERROR_BAD_PROVIDER; ERROR_BUSY: FErrorString := rsERROR_BUSY; ERROR_CANCELLED: FErrorString := rsERROR_CANCELLED; ERROR_CANNOT_OPEN_PROFILE:FErrorString := rsERROR_CANNOT_OPEN_PROFILE; ERROR_DEVICE_ALREADY_REMEMBERED:FErrorString := rsERROR_DEVICE_ALREADY_REMEMBERED; ERROR_EXTENDED_ERROR: FErrorString := rsERROR_EXTENDED_ERROR; ERROR_INVALID_PASSWORD:FErrorString := rsERROR_INVALID_PASSWORD; ERROR_NO_NET_OR_BAD_PATH:FErrorString := rsERROR_NO_NET_OR_BAD_PATH; ERROR_NO_NETWORK: FErrorString := rsERROR_NO_NETWORK; else FErrorString := Format(rsERROR_UNKNOWN_ERROR, [AResource]); end ; Result : = FDrive; end; constructor TNetDrive.Create(AOwner: TComponent); begin inherited Create(AOwner); // FErrorString := ''; // FDrive := ''; FOnDisconnect := nil ; FOnConnect : = nil; end; function TNetDrive.Disconnect: boolean; begin result := false; FErrorString := EmptyStr; if FDrive <> ''then begin case WNetCancelConnection2(PChar(FDrive), 0, true) of NO_ERROR: begin FDrive : = ''; Result := true; if Assigned(FOnDisconnect) then FOnDisconnect(self); end; ERROR_BAD_PROFILE: FErrorString : = rsERROR_BAD_PROFILE; ERROR_CANNOT_OPEN_PROFILE: FErrorString := rsERROR_CANNOT_OPEN_PROFILE; ERROR_DEVICE_IN_USE: FErrorString := rsERROR_DEVICE_IN_USE; ERROR_EXTENDED_ERROR: FErrorString := rsERROR_EXTENDED_ERROR; ERROR_NOT_CONNECTED: FErrorString := rsERROR_NOT_CONNECTED; ERROR_OPEN_FILES: FErrorString := rsERROR_OPEN_FILES; end ; end; end; function TNetDrive.DriveExists(const ADrive: string): boolean; var buf: string; begin GetDir(0, buf); {$I-} ChDir(ADrive); {$I+} Result : (IOResult 0); ChDir(buf); end ; function TNetDrive.FreeDriveName: string; var l: TStringList; d: TDriveComboBox; t: char; i: integer; begin l : = TStringList.Create; d := TDriveComboBox.Create(Application.MainForm); d.Parent := Application.MainForm; d.Visible := false; l.Assign(d.Items); d.Free; for i : = 0 to l.Count - 1 do l[i] := copy(l[i], 1, 1); t := 'd'; result := ''; while (t < 'z') <gras>and (result</gras> '') do if l.IndexOf(t) = -1then result := t else inc(t); l.Free; if result <> ''then result : = result + ':'; end; procedure TNetDrive.SetOnConnect(const Value: TNotifyEvent); begin FOnConnect := Value; end ; procedure TNetDrive.SetOnDisconnect(const Value: TNotifyEvent); begin FOnDisconnect : = Value; end; end.En espérant que cela t'aidera.