Search code examples
c++boostfilesystemswnet

boost create_directories() fails on restricted remote server


I have a problem creating directories on a share folder on a remote computer with boost create_directories(). I have only the access to read on this share. But I create a connection with user credentials with full access to read and write.

The connection is enabled with WNetAddConnection2. Then the app should create the directories "\139.22.XXX.XXX\temp\Test\1" and the connection disables with WNetCancelConnection2.

The code is shown below:

using namespace boost::filesystem;

typedef boost::filesystem::path Path;
typedef boost::filesystem::basic_filesystem_error<boost::filesystem::path> boost_filesystem_error;


DWORD loginDicomImporter()
{
    DWORD dwRetVal;
    NETRESOURCE nr;
    DWORD dwFlags;

    LPTSTR remoteName = _T("\\\\139.22.XXX.XXX\\temp");

    nr.dwType = RESOURCETYPE_ANY;
    nr.lpLocalName = NULL;
    nr.lpRemoteName = remoteName;
    nr.lpProvider = NULL;

    dwFlags = CONNECT_TEMPORARY;

    dwRetVal = WNetAddConnection2(&nr, _T("xxxPWDxxx"), _T("xxxUSERxxx"), dwFlags);

    if (dwRetVal == NO_ERROR)
    {

        return dwRetVal;
    }
    else
    {

        return dwRetVal;
    }

}

DWORD logoutDicomImporter()
{

    DWORD dwResult;
    LPCTSTR lpName = _T("\\\\139.22.XXX.XXX\\temp");
    dwResult = WNetCancelConnection2(lpName, CONNECT_UPDATE_PROFILE, FALSE);

    if (dwResult == NO_ERROR)
    {

        return dwResult;
    }
    else if (dwResult == ERROR_NOT_CONNECTED)
    {

        return dwResult;
    }
    else
    {

        return dwResult;
    }

}

int main()
{
    DWORD loginResult;
    std::string directoryPath = "\\\\139.22.XXX.XXX\\temp\\Test\\1";

    loginResult = loginDicomImporter();

    std::cout << loginResult << std::endl;

    try
    {
        boost::filesystem::create_directories(directoryPath);
    }
    catch (boost_filesystem_error &e)
    {
        auto msg = e.what();
        return 1;
    }


    loginResult = logoutDicomImporter();
    std::cout << loginResult << std::endl;
}

The catched exception says "boost::filesystem::create_directory: Access denied: "\139.22.XXX.XXX\temp\Test\1" Obviously, the OS denies the access. Although I enabled a full access connection before creating directories.

Any suggestions how solve this?


Solution

  • Thanky you melak47 for your quik response. But the problem was that in my actual code I used the servername alias for the directoryPath-string.

    Previously, I had it like: std::string directoryPath = "\\\\ServerName\\temp\\Test\\1"; After following your advice I replaced it with its ip-address.

    Now creating directories works as shown above.