Related Pages
Delphi Tips and Code
- Clearing edit fields
- Starting email client
- Starting default browser
- Starting applications
Delphi Applications

Delphi Tips and Code

Managing a Network Connection

It is possible to connect and disconnect from a windows network from within your application using the WNetAddConnection2 API. The connection code can be placed in the On Click event of a button or on the form's On Create event. The disconnect code could also be activated by a button or in the form's On Close event. Details of the parameters of WNetAddConnection2 can be found in the win32 help that comes with Delphi or at MSDN.

To connect:

procedure TForm.Button1Click(Sender: TObject);
var
   NetResource: TNetResource;
begin
   //code that makes network connection to the server
   NetResource.dwType := RESOURCETYPE_DISK;
   //the mapped drive letter for server
   NetResource.lpLocalName := '[Drive Letter e.g.J:]';
   //name of server
   NetResource.lpRemoteName := '\\[Server name]';
   NetResource.lpProvider := '';
   if WNetAddConnection2(NetResource, nil, nil, CONNECT_UPDATE_PROFILE) <> NO_ERROR       then raise EDisconnectError.Create('ERROR: ' + GetErrorMessage(GetLastError()));
...
end;

To disconnect:

try
   WNetCancelConnection2('[Drive Letter e.g.J:]', CONNECT_UPDATE_PROFILE, True);
except
   on EDisconnectError do GetErrorMessage(GetLastError());
end;

A function that returns "friendly" error messages:

function TForm.GetErrorMessage(AErrorNum: DWORD): string;
begin
   case AErrorNum of
   ERROR_ACCESS_DENIED:
      Result := 'Access to network resource denied';
   ERROR_ALREADY_ASSIGNED:
      Result := 'Local device already assigned';
   ERROR_BAD_DEV_TYPE:
      Result := 'Local device type does not match network resource type';
   ERROR_BAD_DEVICE:
      Result := 'Local device is invalid';
   ERROR_BAD_NET_NAME:
      Result := 'Network resource name is invalid or unlocatable';
   ERROR_BAD_NETPATH:
      Result := 'Network path not found';
   ERROR_BAD_PROFILE:
      Result := 'User profile is in an incorrect format';
   ERROR_BAD_PROVIDER:
      Result := 'Provider property does not match any provider';
   ERROR_BUSY:
      Result := 'Provider is busy';
   ERROR_CANCELLED:
      Result := 'Connection attempt cancelled';
   ERROR_CANNOT_OPEN_PROFILE:
      Result := 'Cannot save reconnect at logon information';
   ERROR_DEVICE_ALREADY_REMEMBERED:
      Result := 'Connection already remembered';
   ERROR_DEVICE_IN_USE:
      Result := 'Device in use by active process, cannot disconnect';
   ERROR_EXTENDED_ERROR:
      Result := 'A network specific error occurred';
   ERROR_INVALID_PASSWORD:
      Result := 'Invalid password';
   ERROR_NO_NET_OR_BAD_PATH:
      Result := 'Network not started or name could not be handled';
   ERROR_NO_NETWORK:
      Result := 'No network present';
   ERROR_NOT_CONNECTED:
      Result := 'Not connected to specified resource or on specified device';
   ERROR_OPEN_FILES:
      Result := 'Files are open on resource and force disconnect not specified';
   ERROR_LOGON_FAILURE:
      Result := 'User name or password incorrect';
   else
      Result := IntToStr(AErrorNum) + ': Unhandled Error';
   end;
end;


  • Home
  • |
  • Sitemap
  • |
  • Databases
  • |
  • Web development
  • |
  • Delphi development
  • |
  • Image manipulation
  • |
  • KaSaB
  • |
  • Contact