LocalMappedUNCPath

Gets the local path that is currently mapped to the specified UNC path.

Archived content: This source code is currently inactive and may be outdated or no longer maintained or functional.

UNC-Pfade der Form \\Server\Freigabe\Pfad dienen eigentlich dazu, Dateien auf einem Netzwerkcomputer unkompliziert zu adressieren. In manchen Fällen muss aber immer noch ein lokaler Pfad mit Laufwerksbuchstabe angegeben werden, um ein Programm zu starten oder eine Datei zu öffnen. Um in einem automatisierten Ablauf nicht blind einen weiteren Buchstaben für den gewünschten UNC-Pfad zu reservieren, sondern erst einmal zu prüfen, ob vielleicht schon ein solcher Pfad verbunden ist, lässt sich die LocalMappedUNCPath-Funktion verwenden.

Eine ähnliche Funktion in C#, die den lokalen Pfad der Freigabe auf der Serverseite ermittelt, ist bei Brian Pedersen verfügbar.

Note on the code quality: The content on this page is possibly a bit outdated. I’m not using C++ anymore for quite some time, but I would like to keep the existing and once useful functions available.

Download

LocalMappedUNCPath.cpp2.8 KiBQuelltext der LocalMappedUNCPath-Funktion

Contents of the file LocalMappedUNCPath.cpp:

// Gets the local path that is currently mapped to the specified UNC path.
//
// If no drive is mapped to the UNC path, an empty string is returned.
//
// Ex:
// "\\Server\share\pathname" -> "X:\pathname"
// "C:\windows\filename" -> "C:\windows\filename"
// "\\NotMappedServer\share" -> ""
//
// Dependencies: mpr.lib
//
// Web: http://unclassified.software/source/localmappeduncpath
//
int LocalMappedUNCPath(const char *uncname, char *mapped)
{
    CString szHiFe, szUNC, szUNCListEntryUNC, szMap;
    int ret = 1;

    HANDLE lphEnum;
    DWORD cbBuffer = 16384;           // 16K is a good size
    DWORD cEntries = 0xFFFFFFFF;      // enumerate all possible entries
    LPNETRESOURCE lpnrLocal = NULL;   // pointer to enumerated structures
    DWORD i;
    DWORD dwResult, dwResultEnum;

    strcpy(mapped, "");

    szUNC.Format("%s", uncname);
    szUNC.MakeUpper();

    if (szUNC.Find(":\\") == 1)
    {
        strcpy(mapped, uncname);
        return 0;
    }

    dwResult = WNetOpenEnum(RESOURCE_CONNECTED, RESOURCETYPE_DISK, 0, lpnrLocal, &lphEnum);
    if (dwResult == NO_ERROR)
    {
          lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer);

          do
          {
              ZeroMemory(lpnrLocal, cbBuffer);
              dwResultEnum = WNetEnumResource(lphEnum,   // resource handle
                                    &cEntries,    // defined locally as 0xFFFFFFFF
                                    lpnrLocal,    // LPNETRESOURCE
                                    &cbBuffer);   // buffer size
               if (dwResultEnum == NO_ERROR)
               {
                    for (i = 0; i < cEntries; i++)
                    {
                        szUNCListEntryUNC = lpnrLocal[i].lpRemoteName;
                        szUNCListEntryUNC.MakeUpper();
                        if (szUNC.Find(szUNCListEntryUNC) > -1)
                        {
                            szMap = szUNC;
                            szMap.Replace(szUNCListEntryUNC, lpnrLocal[i].lpLocalName);
                            dwResultEnum = (DWORD) ERROR_NO_MORE_ITEMS;
                            ret = 0;
                            break;
                        }
                    }
               }
               else if (dwResultEnum != ERROR_NO_MORE_ITEMS)
               {
                   //NetErrorHandler(hwnd, dwResultEnum, (LPSTR)"WNetEnumResource");
                   ret = 1;
                   break;
               }
          }
          while (dwResultEnum != ERROR_NO_MORE_ITEMS);
          GlobalFree((HGLOBAL) lpnrLocal);

          strcpy(mapped, szMap.operator LPCTSTR());
    }
    else
    {
        ret = 1;
        switch (dwResult)
        {
            case ERROR_NOT_CONTAINER:
                strcpy(mapped, "Kein Container");
                break;
            case ERROR_INVALID_PARAMETER:
                strcpy(mapped, "Invalid Parameter");
                break;
            case ERROR_NO_NETWORK:
                strcpy(mapped, "Kein Netzwerk");
                break;
            case ERROR_EXTENDED_ERROR:
                strcpy(mapped, "ErrorExtendedError");
                break;
            default:
                szHiFe.Format("Sonstiger Fehler!\nFehlercode: %d", dwResult);
                strcpy(mapped, szHiFe);
                break;
        }
    }
    dwResult = WNetCloseEnum(lphEnum);

    if (dwResult != NO_ERROR)
    {
        //NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetCloseEnum");
        strcpy(mapped, "Fehler bei EnumClose");
        ret = 1;
    }
    return ret;
}

Licence and terms of use

This software is freely available as source code and compiled version, without restrictions (“public domain”). There is no warranty, not even for merchantability or fitness for a particular purpose. I am not liable for any damage caused through appropriate or inappropriate use.

Statistic data

  • Created on 2007-04-02.