1

I am trying to convert the local path where the C++ executable is stored to a UNC path using the implementation suggested by Stefan. But I get a 2250 error, which according to Microsoft's Network Management Codes means 'The network connection could not be found'

Does anyone know how I can adapt my code to get the UNC path?

Here is minimal reproducing code.

#include <iostream>
#include <Windows.h>
#include <sstream>
#include <shlwapi.h>
#include <windows.h>
#include <string>

#pragma comment(lib, "netapi32.lib")
#pragma comment(lib, "Shlwapi.lib")

using namespace std;

std::wstring ConvertToUNC(wstring sPath);

std::wstring GetExecutableDirectory()
{
    // Get the full path to the executable file
    wchar_t buffer[MAX_PATH];
    GetModuleFileName(NULL, buffer, MAX_PATH);

    // Remove the file name from the path
    PathRemoveFileSpec(buffer);

    return std::wstring(buffer);
}

int main()
{
    std::wstring localPath = GetExecutableDirectory();
    std::wstring uncPath = ConvertToUNC(localPath);

    wcout << L"Local path: " << localPath << endl;
    wcout << L"UNC path: " << uncPath << endl;   

    return 0;
}


std::wstring int_to_wstring(int i) {
    std::wstringstream ss;
    ss << i;
    return ss.str();
}

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
    DWORD result = WNetGetUniversalName(sPath.c_str(), UNIVERSAL_NAME_INFO_LEVEL, (LPVOID)&temp, &bufsize);

    if (result == 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;

        result = WNetGetUniversalName(sPath.c_str(), UNIVERSAL_NAME_INFO_LEVEL, (LPVOID)puni, &bufsize);
        if (result == NO_ERROR)
        {
            sRet = wstring(puni->lpUniversalName);
        }
        else
        {
            // Failed to retrieve UNC path
            sRet = L"Error retrieving UNC path: " + int_to_wstring(result);
        }

        delete[] buf;
    }
    else if (result != NO_ERROR)
    {
        // Failed to retrieve UNC path
        sRet = L"Error retrieving UNC path: " + int_to_wstring(result);
    }

    return sRet;
}

I use Visual Studio 2022 as my coding environment, and my OS is Windows 11. For context, I am new to this language and still learning.

I've tried several things to troubleshoot (but had no luck), including:

  • checking the local path exists (it does, and is found by the sample code)
  • checking the sharing permissions within the local path folder
  • running the C++ code directly from the executable (instead of the VS2022 debugger)

Thank you in advance.

drescherjm
  • 10,365
  • 5
  • 44
  • 64
Kasi74
  • 23
  • 3

1 Answers1

2

I get a 2250 error, which according to Microsoft's Network Management Codes means 'The network connection could not be found'

In this situation, error 2250 is referring to the standard system error ERROR_NOT_CONNECTED, not the network error NERR_UseNotFound, which happens to have the same numeric value.

If you read the documentation for WNetGetUniversalName() itself, it says:

The WNetGetUniversalName function takes a drive-based path for a network resource and returns an information structure that contains a more universal form of the name.

...

If the function fails, the return value is a system error code, such as one of the following values.

Return code Description
ERROR_NOT_CONNECTED The device specified by the lpLocalPath parameter is not redirected.

Basically, that means the path you are passing in to WNetGetUniversalName() is just a local path, it is not using a drive letter that is mapped to a network share, hence there is no UNC path to retrieve for it.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thank you for your answer, Remy. I misunderstood what the function does. Would you happen to know how to retrieve the network-path using C++? This is the network path that you can manually get when right-clicking a folder and going to properties ([example here] (https://imgur.com/p8hatxG)). If it's more appropriate, I can also ask this as a separate question. – Kasi74 Feb 24 '23 at 01:42
  • @Kasi74 "*If it's more appropriate, I can also ask this as a separate question*" - please do – Remy Lebeau Feb 24 '23 at 07:20
  • Thank you. I've added a new question [here](https://stackoverflow.com/questions/75558657/how-do-i-retrieve-the-network-path-of-the-source-executable-using-c) – Kasi74 Feb 24 '23 at 15:54