Can someone tell me what's wrong with the function below? I have a user
trying to connect to a mapped network drive accessed through a VPN. If
my app is the first one that accesses the drive the connection fails.
If they first browse to the drive through Explorer it prompts for the
username and password and then my app can connect without any problems.
What do I need to change in order to get the username/password prompt?
Thanks,
Craig Peterson
Scooter Software
procedure WNetCheck(Error: DWORD);
begin
if Error <> ERROR_SUCCESS then RaiseLastOSError;
end;
procedure ConnectToShare(const AShare: string);
var
S: string;
NetResource: TNetResource;
RemoteName: array [0..MAX_PATH] of char;
RemoteLen: DWORD;
Error: DWord;
begin
// Don't do anything for local drives and drives already connected
if Length(AShare) = 2 then
begin
S := IncludeTrailingPathDelimiter(AShare);
if (GetDriveType(PChar(S)) <> DRIVE_REMOTE) or
DirectoryExists(S) then
Exit;
end
else if DirectoryExists(AShare) then
Exit;
FillChar(NetResource, SizeOf(TNetResource), 0);
NetResource.dwType := RESOURCETYPE_DISK;
// If it's a mapped drive retrieve the remote path to connect to
if Length(AShare) = 2 then
begin
RemoteLen := MAX_PATH;
WNetCheck(WNetGetConnection(PChar(AShare), RemoteName,
RemoteLen));
NetResource.lpLocalName := PChar(AShare);
NetResource.lpRemoteName := RemoteName;
end
else
NetResource.lpRemoteName := PChar(AShare);
Error := WNetAddConnection3(Application.Handle, NetResource, nil, nil,
CONNECT_INTERACTIVE);
// If the connection is already open but the DirectoryExists failed
// above, try killing the existing connection and connect again
if (Error = ERROR_ALREADY_ASSIGNED) or
(Error = ERROR_DEVICE_ALREADY_REMEMBERED) then
begin
WNetCancelConnection2(NetResource.lpLocalName, 0, false);
Error := WNetAddConnection3(Application.Handle, NetResource,
nil, nil, CONNECT_INTERACTIVE);
end;
WNetCheck(Error);
end;