Local name to UNC name

by Michael Rüsweg-Gilbert

Procedure WNetGetConnection retrieves the name of the network resource associated with a local device. In other words, it can be used to get the UNC name for a mapped network drive or printer. If K: is the drive letter of a mapped network drive, the following example finds the UNC name for this network drive.

DEFINE VARIABLE Drive_Name AS CHARACTER NO-UNDO INIT "K:".
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
    UNC_Name
    VIEW-AS ALERT-BOX.

API definitions used in this exampe, not listed in windows.p :

PROCEDURE WNetGetConnectionA EXTERNAL "mpr.dll" :
  DEFINE INPUT        PARAMETER lpDrive    AS CHARACTER.
  DEFINE OUTPUT       PARAMETER lpUNCName  AS CHARACTER.
  DEFINE INPUT-OUTPUT PARAMETER lpnLength  AS LONG.
  DEFINE RETURN       PARAMETER RetBool    AS LONG.
END PROCEDURE.