Recent

Author Topic: Access to a shared folder from remote server  (Read 857 times)

SteenJorgensen

  • Jr. Member
  • **
  • Posts: 66
Access to a shared folder from remote server
« on: August 10, 2021, 09:41:21 am »
Hi

I have some shared directory from another server i need to connect, and sometimes username and password is needed for access.

How can i send username/password for the share. Without making a new process and execute "Net use "\\myShare" "Mypasswd" /User:Username"

eg.:
If not DirectoryExists(\\myshare) then
  SetUpMyShare; <-- Needed procedure
FindAllFiles(slFiles, '\\myshare', '*.*', true);
----------------------------------------
Lazarus version 2.0.12 64-bit
FPC 3.2.0

af0815

  • Hero Member
  • *****
  • Posts: 778
Re: Access to a shared folder from remote server
« Reply #1 on: August 10, 2021, 02:09:40 pm »
You mean a soloution for windows only or from linux ?

Windows only, you can call the Windows-API functions to handle the shares like the 'net use'.

Here an old snippet for example. I don't know if it works with the actual fpc/lazarus and maybe win32 only.

https://docs.microsoft.com/en-us/windows/win32/api/winnetwk/nf-winnetwk-wnetaddconnection2a

Code: Pascal  [Select][+][-]
  1. unit WNNetShare;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils;
  9.  
  10. function ConnectNetworkDrive(const ADrive: String;
  11.   const ADirectory, AUsername, APassword: String;
  12.   const ARestoreAtLogon: Boolean ): Boolean;
  13.  
  14. procedure DisconnetNetworkDrive(const ADrive: String);
  15.  
  16.  
  17. implementation
  18.  
  19. uses
  20.   windows;
  21.  
  22. function ConnectNetworkDrive(const ADrive: String;
  23.   const ADirectory, AUsername, APassword: String;
  24.   const ARestoreAtLogon: Boolean ): Boolean;
  25. var
  26.   NetResource: TNetResource;
  27.   dwFlags:     DWORD;
  28.   lPwd, lUser: PChar;
  29. begin
  30.   NetResource.dwType:=RESOURCETYPE_DISK;
  31.   NetResource.lpLocalName:=PChar(ADrive);
  32.   NetResource.lpRemoteName:=PChar(ADirectory);
  33.   NetResource.lpProvider:=nil;
  34.  
  35.   if ARestoreAtLogon then
  36.     dwFlags:=CONNECT_UPDATE_PROFILE
  37.   else
  38.     dwFlags:=0;
  39.   if AUsername = '' then
  40.     lUser:=PChar(AUsername)
  41.   else
  42.     lUser:=nil;
  43.   if APassword = '' then
  44.     lPwd:=PChar(APassword)
  45.   else
  46.     lPwd:=nil;
  47.   Result:=WNetAddConnection2(NetResource,
  48.                              lPwd,
  49.                              lUser,
  50.                              dwFlags)=NO_ERROR;
  51. end;
  52.  
  53. procedure DisconnetNetworkDrive(const ADrive: String);
  54. begin
  55.   if not WNetCancelConnection2(PChar(ADrive),0, True)=NO_ERROR then
  56.     RaiseLastOSError;
  57. end;
  58.  
  59.  
  60. end.
  61.  
  62.  

regards
Andreas

 

TinyPortal © 2005-2018