Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a UNC path for a file that is accessed through a network drive?

I am working on a application in VC++ where network drives are used to access files. The drives are assigned manually by the users and then select the drive in the application. This results in drives not always being mapped to the same servers.

How would I go about obtaining the UNC path to such a file? This is mostly for identification purposes.

like image 444
Paradoxyde Avatar asked Feb 10 '12 19:02

Paradoxyde


2 Answers

here's the function I use to convert a normal path to an UNC path:

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;;
} 
like image 70
Stefan Avatar answered Oct 20 '22 13:10

Stefan


Suggest you use WNetGetConnection.

API description is here: http://msdn.microsoft.com/en-us/library/windows/desktop/aa385453(v=vs.85).aspx

like image 34
Joseph Willcoxson Avatar answered Oct 20 '22 13:10

Joseph Willcoxson