Retrieve the UNC from a mapped driveTag(s): WinAPI/Registry


To convert a normal paths (N:\PATH) to UNC (\\SERVER\PATH).
[local external function declaration]
FUNCTION ulong WNetGetConnectionA &
  ( ref string drv, ref string unc, ref ulong buf ) &
  LIBRARY "mpr.dll"

[powerscript]
string    ls_tmp, ls_unc
Ulong     ll_rc, ll_size

ls_tmp = upper(left(as_path,2))
IF right(ls_tmp,1) <> ":" THEN RETURN as_path

ll_size = 255
ls_unc = Space(ll_size)

ll_rc = WNetGetConnectionA (ls_tmp, ls_unc, ll_size)
IF  ll_rc = 2250 THEN
    // prbably local drive
    RETURN as_path
END IF

IF ll_rc <> 0 THEN
    MessageBox("UNC Error", &
       "Error " + string(ll_rc) + " retrieving UNC for " + ls_tmp)
    RETURN as_path
END IF

// Concat and return full path
IF len(as_path) > 2 THEN
   ls_unc = ls_unc + mid(as_path,3)
END IF

RETURN ls_unc

blog comments powered by Disqus