1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
-------------------------------------------------------------------------------------
unit network;
interface
uses Windows;
const
// exemple, à remplacer, bien sur !!!!
_NET_LETT_ = 'W:';
_NET_RESS_ = '\\MACHINE\E$';
_NET_USER_ = 'DOMAINE\USER';
_NET_PASS_ = 'PASSWORD';
function ConnectDrive(_handle: HWND; _drvLetter: string; _netPath: string; _user: string; _pass: string; _showError: Boolean; _reconnect: Boolean): DWORD;
function DisconnectNetDrive(_locDrive: string; _showError: Boolean; _force: Boolean; _save: Boolean): DWORD;
function NetError(err: DWORD): string;
implementation
{ ------------------------------------------------------------------------- }
function ConnectDrive(_handle: HWND; _drvLetter: string; _netPath: string; _user: string; _pass: string; _showError: Boolean; _reconnect: Boolean): DWORD;
var
nRes: TNetResource;
errCode: DWORD;
dwFlags: DWORD;
begin
{ Fill NetRessource with #0 to provide uninitialized values }
FillChar(NRes, SizeOf(NRes), #0);
nRes.dwType := RESOURCETYPE_DISK;
{ Set Driveletter and Networkpath }
nRes.lpLocalName := PChar(_drvLetter);
nRes.lpRemoteName := PChar(_netPath); { Example: \\Test\C }
{ Check if it should be saved for use after restart and set flags }
if _reconnect then
dwFlags := CONNECT_UPDATE_PROFILE and CONNECT_INTERACTIVE
else
dwFlags := CONNECT_INTERACTIVE;
{errCode := WNetAddConnection3(Form1.Handle, nRes, nil, nil, dwFlags);}
errCode := WNetAddConnection3(_handle, nRes, PChar(_pass), PChar(_user), dwFlags);
{ Show Errormessage, if flag is set }
if (errCode <> NO_ERROR) and (_showError) then
begin
{
Application.MessageBox(PChar('An error occured while connecting:' + #13#10 +
SysErrorMessage(GetLastError)),
'Error while connecting!',
MB_OK);
}
end;
Result := errCode; { NO_ERROR }
end;
{ ------------------------------------------------------------------------- }
function DisconnectNetDrive(_locDrive: string; _showError: Boolean; _force: Boolean; _save: Boolean): DWORD;
var
dwFlags: DWORD;
errCode: DWORD;
begin
{ Set dwFlags, if necessary }
if _save then
dwFlags := CONNECT_UPDATE_PROFILE
else
dwFlags := 0;
{ Cancel the connection }
errCode := WNetCancelConnection2(PChar(_locDrive), dwFlags, _force);
{ Show Errormessage, if flag is set }
if (errCode <> NO_ERROR) and (_showError) then
begin
{
Application.MessageBox(PChar('An error occured while disconnecting:' + #13#10 +
SysErrorMessage(GetLastError)),
'Error while disconnecting',
MB_OK);
}
end;
Result := errCode; { NO_ERROR }
end;
{ ------------------------------------------------------------------------- }
function NetError(err: DWORD): string;
begin
case err of
{ Connect }
ERROR_ACCESS_DENIED: NetError := 'ERROR_ACCESS_DENIED';
ERROR_ALREADY_ASSIGNED: NetError := 'ERROR_ALREADY_ASSIGNED';
ERROR_BAD_DEV_TYPE: NetError := 'ERROR_BAD_DEV_TYPE';
ERROR_BAD_DEVICE: NetError := 'ERROR_BAD_DEVICE';
ERROR_BAD_NET_NAME: NetError := 'ERROR_BAD_NET_NAME';
ERROR_BAD_PROFILE: NetError := 'ERROR_BAD_PROFILE';
ERROR_BAD_PROVIDER: NetError := 'ERROR_BAD_PROVIDER';
ERROR_BUSY: NetError := 'ERROR_BUSY';
ERROR_CANCELLED: NetError := 'ERROR_CANCELLED';
ERROR_CANNOT_OPEN_PROFILE: NetError := 'ERROR_CANNOT_OPEN_PROFILE';
ERROR_DEVICE_ALREADY_REMEMBERED: NetError := 'ERROR_DEVICE_ALREADY_REMEMBERED';
ERROR_EXTENDED_ERROR: NetError := 'ERROR_EXTENDED_ERROR';
ERROR_INVALID_PASSWORD: NetError := 'ERROR_INVALID_PASSWORD';
ERROR_NO_NET_OR_BAD_PATH: NetError := 'ERROR_NO_NET_OR_BAD_PATH';
ERROR_NO_NETWORK: NetError := 'ERROR_NO_NETWORK';
{ DisConnect }
ERROR_DEVICE_IN_USE: NetError := 'ERROR_DEVICE_IN_USE';
ERROR_NOT_CONNECTED: NetError := 'ERROR_NOT_CONNECTED';
ERROR_OPEN_FILES: NetError := 'ERROR_OPEN_FILES';
else
NetError := 'Erreur Inconnu';
end;
end;
{ ----------------------------------------------------------------------- }
{ ----------------------------------------------------------------------- }
end.
------------------------------------------------------------------------------------- |
Partager