Convert a mapped drive to a full UNC path

Submitted by:Jhon Jhon

Date added:30 June, 2012

Category:Delphi

Delphi's RTL ExpandUNCFilename function retrieves the full path and filename of a specified (relative) file using Universal Naming Convention for network files (thus making my *very old* custom function obsolete).

Tags: convert a mapped drive , unc path

Code Snippet:

Usage:
UNCLabel.Caption := ConvertToUNCPath(ExtractFileDrive(Edit1.Text)) ;

~~~~~~~~~~~~~~~~~~~~~~~~~
function ConvertToUNCPath
(MappedDrive: string) : string;
var
RemoteString : array[0..255] of char;
lpRemote : PChar;
StringLen : Integer;
begin
lpRemote := @RemoteString;
StringLen := 255;
WNetGetConnection
(Pchar(ExtractFileDrive(MappedDrive)) ,
lpRemote,
StringLen) ;
Result := RemoteString;
end;

Note: Delphi's RTL ExpandUNCFilename function retrieves the full path and filename of a specified (relative) file using Universal Naming Convention for network files (thus making my *very old* custom function obsolete).

A note from Jan Lund:

I have used the function mentioned above, and it works great. However, if the drive selected isn't a networked drive the function will return garbage.

Therefore the call to WNetGetConnection should have some kind of check whether it succeded or not :

If WNetGetConnection(Pchar(ExtractFileDrive(MappedDrive)) ,
lpRemote,
StringLen) = NO_ERROR Then
Result := RemoteString
Else
Result:=''; // Alternatively return an errorcode, Raise an exception or something like that.
~~~~~~~~~~~~~~~~~~~~~~~~~
 
 

Comments