【问题标题】:boost create_directories() fails on restricted remote serverboost create_directories() 在受限远程服务器上失败
【发布时间】:2015-08-06 12:14:13
【问题描述】:

我在使用 boost create_directories() 在远程计算机上的共享文件夹上创建目录时遇到问题。我只有读取此共享的权限。但我使用具有完全读写权限的用户凭据创建连接。

使用 WNetAddConnection2 启用连接。然后应用程序应创建目录“\139.22.XXX.XXX\temp\Test\1”并使用 WNetCancelConnection2 禁用连接。

代码如下:

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;
}

捕获的异常显示“boost::filesystem::create_directory: Access denied:”\139.22.XXX.XXX\temp\Test\1 显然,操作系统拒绝访问。虽然我在创建目录之前启用了完全访问连接。

任何建议如何解决这个问题?

【问题讨论】:

  • 我真的不明白如何简单地与 WinAPI 函数建立连接会让 boost::filesystem 使用该经过身份验证的连接。您可以尝试分配一个本地设备名称,例如 Z: 来挂载远程文件系统,然后通过它访问它?

标签: c++ boost filesystems wnet


【解决方案1】:

感谢 melak47 的快速回复。 但问题是,在我的实际代码中,我使用了 servername 别名作为 directoryPath-string。

以前,我是这样的: std::string directoryPath = "\\\\ServerName\\temp\\Test\\1"; 在听从您的建议后,我将其替换为它的 IP 地址。

现在创建目录的工作如上所示。

【讨论】:

    猜你喜欢
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    • 2014-09-29
    相关资源
    最近更新 更多