views:

210

answers:

3

In C++, how can I retrieve the location of a mounted drive? for example, if I have mounted drive s: to c:\temp (using subst in the command line) "subst c:\temp s:" how can I get "c:\temp" by passing "s:"

I would also like to know how can it be done for a network drive. (if s: is mounted to "\MyComputer\Hello", then I want to retrieve "\MyComputer\Hello" and then to retrieve "c:\Hello" from that)

It might be a very easy question but I just couldn't find information about it.

Thanks,

Adam

A: 

You can probably use the GetVolumeInformation function. From the documentation:

Symbolic link behavior

If the path points to a symbolic link, the function returns volume information for the target.

Haven't tested it myself, though.

csl
A: 

To find the path of a mounted network share, you have to use the WNet APIs:

wstring ConvertToUNC(wstring sPath)
{
    WCHAR temp;
    UNIVERSAL_NAME_INFO * puni = NULL;
    DWORD bufsize = 0;
    wstring sRet = sPath;
    //Call WNetGetUniversalName using UNIVERSAL_NAME_INFO_LEVEL option
    if (WNetGetUniversalName(sPath.c_str(),
     UNIVERSAL_NAME_INFO_LEVEL,
     (LPVOID) &temp,
     &bufsize) == ERROR_MORE_DATA)
    {
     // now we have the size required to hold the UNC path
     WCHAR * buf = new WCHAR[bufsize+1];
     puni = (UNIVERSAL_NAME_INFO *)buf;
     if (WNetGetUniversalName(sPath.c_str(),
      UNIVERSAL_NAME_INFO_LEVEL,
      (LPVOID) puni,
      &bufsize) == NO_ERROR)
     {
      sRet = wstring(puni->lpUniversalName);
     }
     delete [] buf;
    }

    return sRet;;
}
Stefan
+1  A: 

If you've used SUBST, the API you want is QueryDosDevice. You can SUBST things yourself by using DefineDosDevice.

Roger Lipscombe