0

I am trying to programmatically retrieve the network path where the C++ executable is stored. For clarity, when I say network path, I mean the path retrieved in Windows manually by: 'right clicking the folder', 'choosing properties', and 'selecting the sharing tab'. An example of this path can be seen here.

Does anyone know how to adapt my below code to retrieve the network path (as specified above) rather than the local path?

Here is minimal reproducing code:

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

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

using namespace std;

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();
    
    // SOME CODE  IN HERE TO GET THE NETWORK-SHARE PATH   
    //....

    wcout << L"Local path: " << localPath << endl;
    wcout << L"Network-share path: ";   

    return 0;
}

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:

  • trying the WNetGetUniversalName function, as implemented by @Stefan.
    I also asked a previous question related to this that can be found here

  • checking the local path exists (it does, and is found by the sample code)

  • checking the sharing permissions within the local path folder (they are set to share with everyone)

  • running the C++ code directly from the executable (instead of the VS2022 debugger)

Thank you in advance.

user4581301
  • 33,082
  • 7
  • 33
  • 54
Kasi74
  • 23
  • 3
  • There is no one-stop function for this. You'll have to calculate it from `NetShareEnum` and looking for matching paths. – Raymond Chen Feb 24 '23 at 17:27
  • Thank you for the feedback, Raymond. Please let me know if you or anyone else has minimal reproducible code to help implement the function with NetShareEnum. – Kasi74 Feb 27 '23 at 13:58
  • You may be waiting a long time. This site is not about "Please give me prewritten code to solve my problem." – Raymond Chen Feb 27 '23 at 14:18

0 Answers0