【发布时间】: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