Re: accessing a network device with Delphi

Posted by Stefan Loeners *DClub*

In Reply to: accessing a network device posted by Stefan Loeners *DClub*

I think I found a solution.

To ping the workstation, I use the IPICMP component from Internet Professional (used to be a set of commercial VCLs from TurboPower, but now available for free at htp://sourceforge.net/projects/tpipro). Check out the exPING sample code.

To map a drive, I use the API call: WNETADDCONNECTION2.
Here is some sample code:

function MakeDriveMapping(DriveLetter: string; DirectoryPath: string; 
  Username: string; Password: string; RestoreAtLogon: Boolean): DWORD; 
var 
  NetResource: TNetResource; 
  dwFlags: DWORD; 
begin 
  with NetResource do 
  begin
    dwType := RESOURCETYPE_DISK; 
    lpLocalName := PChar(DriveLetter); 
    lpRemoteName := PChar(DirectoryPath);
  lpProvider := nil; 
  end; 
  if (RestoreAtLogon) then
    dwFlags := CONNECT_UPDATE_PROFILE 
  else 
    dwFlags := 0; 
  Result := WNetAddConnection2(NetResource, PChar(Password), 
    PChar(Username), dwFlags); 
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  if (MakeDriveMapping('X:', '\\192.16.2.101\dir',
      'Administrator', 'adminpassword', False) = 0) then
    ShowMessage('OK') 
  else
    ShowMessage('Error!'); 
   { possible causes:
     if mapping already exists or if credentials incorrect 
     or directory path incorrect }
end;