Getting UNC from Drive Letter

Chris Kelleher

Administrator
Staff member
I know this has been asked before; I asked it myself in a previous life.
But I no longer have access to the answer.

What is the Windows API routine to convert a drive letter to a UNC-style
directory, i.e. X:\ to \\machine\directory?

Tim Townsend
NxTrend Technology, Inc.
 

Chris Kelleher

Administrator
Staff member
Tim,

You can use the following function to retrieve the UNC for a drive letter.

<BLOCKQUOTE><font size="1" face="Arial, Verdana">code:</font><HR><pre>
Procedure WNetGetConnectionA External "mpr.dll":
define input parameter lpszLocalName as char. /* Contains the name of
the local device the caller is interested in. */
define output parameter lpszRemoteName as char. /* The buffer to receive
the remote name used to make the connection. */
define input-output parameter lpBufferSize as long. /* This is used to
specify the size of the buffer passed in. */
/* If the call fails because the buffer is not big enough, this */

/* location will be used to return the required buffer size. */
define return parameter iRetCode as long.
END.
[/code]
The following or some of the error codes you can expect back.
<BLOCKQUOTE><font size="1" face="Arial, Verdana">code:</font><HR><pre>
&GLOB NO_ERROR 0
&GLOB ERROR_BAD_DEVICE 1200 lpLocalName is invalid
&GLOB ERROR_NOT_CONNECTED 2250 lpLocalName not a redirected
device
&GLOB ERROR_MORE_DATA 234 the buffer is too small
&GLOB ERROR_CONNECTION_UNAVAIL 1201
&GLOB ERROR_NO_NETWORK 1222 network is not present
&GLOB ERROR_EXTENDED_ERROR 1208
&GLOB ERROR_NO_NET_OR_BAD_PATH 1203
[/code]

Hope this helps.

Todd G. Nist
tnist@fbos.com
 

Chris Kelleher

Administrator
Staff member
<BLOCKQUOTE><font size="1" face="Arial, Verdana">code:</font><HR><pre>
define variable Drive_Name as character no-undo init "X:".
define variable UNC_Name as character no-undo.
define variable namelen as integer no-undo initial 100.
define variable retBool as integer no-undo.

UNC_Name = fill("x",namelen).
RUN WNetGetConnectionA ( Drive_Name,
OUTPUT UNC_Name,
INPUT-OUTPUT namelen,
OUTPUT retBool).
if retBool = 0 then UNC_Name = SUBSTRING(UNC_Name, 1, namelen).
else UNC_Name = "".

message Drive_Name " " UNC_Name view-as alert-box.

PROCEDURE WNetGetConnectionA EXTERNAL "mpr.dll" :
DEFINE INPUT PARAMETER lpDrive AS CHAR.
DEFINE OUTPUT PARAMETER lpUNCName AS CHAR.
DEFINE INPUT-OUTPUT PARAMETER lpnLength AS LONG.
DEFINE RETURN PARAMETER RetBool AS LONG.
END PROCEDURE.
[/code]

HTH

Dmitri Levin
Protech Systems
 
Top