|
//Establish a remote connection xPath: remote path xNetUser/xNetPassword: user name and password can be specified
function AddNetConnection(xPath, xNetUser, xNetPassword: string): string;
var
mNetSource: TNetResource;
mNetUser, mNetPassword: PChar;
mErrorCode: Cardinal;
begin
Result :='';
if Copy(xPath, 1, 2) <> '\\' then Exit; //Non-remote mode, exit
try
with mNetSource do
begin
dwScope := RESOURCE_GLOBALNET;
dwType := RESOURCETYPE_ANY;
dwDisplayType := RESOURCEDISPLAYTYPE_SHARE;
dwUsage := RESOURCEUSAGE_CONNECTABLE;
lpLocalName := nil; //You can specify the local drive name here, then map xPath to the local drive, pass nil, and only establish a remote connection with xPath
lpRemoteName := LPTSTR(xPath);
lpComment := nil;
lpProvider := nil;
end;
if xNetUser <>'' then
begin
mNetUser := LPTSTR(xNetUser);
mNetPassword := LPTSTR(xNetPassword);
end
else
begin
mNetUser := nil;
mNetPassword := nil;
end;
mErrorCode := WNetAddConnection2(mNetSource, mNetPassword, mNetUser, CONNECT_UPDATE_PROFILE);
if mErrorCode <> NO_ERROR then //mapping failed
Result := Format('An error occurred while establishing a network connection with %s! Error message: %s', [xPath, SysErrorMessage(mErrorCode)]);
except
on E: Exception do
Result := Format('An error occurred while establishing a network connection with %s! Error message: %s', [xPath, E.Message]);
end;
end;
//mPath if the remote path is passed in, disconnect the network connection with the remote path
//mPath if the local drive name is passed in, the local drive will be disconnected
function CancelNetConnection(xPath: string): string;
var
mErrorCode: Cardinal;
begin
Result :='';
if Copy(xPath, 1, 2) <> '\\' then Exit; //Non-remote mode, exit
try
mErrorCode := WNetCancelConnection2(LPTSTR(xPath), CONNECT_UPDATE_PROFILE, True);
if mErrorCode = NO_ERROR then
Result := Format('An error occurred while disconnecting from %s! Error message: %s', [xPath, SysErrorMessage(mErrorCode)]);
except
on E: Exception do
Result := Format('An error occurred while disconnecting from %s! Error message: %s', [xPath, E.Message]);
end;
end; |
|