c++ - 使用 Win32 C++ 访问 protected 网络共享

标签 c++ windows winapi

有没有办法使用 Win32 C++ API 访问受登录/密码保护的网络共享、列出文件并获取其名称和创建日期? 我不希望 samba 网络共享出现在我的资源管理器中。 (这可以使用 WNetAddConnection2 方法来完成)。 谢谢大家。

最佳答案

虽然我同意 Ben 在评论中提出的反对意见,但您可以继续使用 WNetAddConnection2 。当您为 lpLocalName 参数传入 NULL 值时,它不会映射驱动器,而只是执行授权,从而允许您使用完整的 用于执行枚举远程系统上的文件等任务的 UNC 路径。

lpLocalName: A pointer to a null-terminated string that specifies the name of a local device to redirect, such as "F:" or "LPT1". The string is treated in a case-insensitive manner. If the string is empty or if lpLocalName is NULL, the function makes a connection to the network resource without redirecting a local device.

MSDN 页面几乎给出了使用它的基本信息,但类似于(我手头没有 Windows 框来验证此代码是否有效):

NETRESOURCE resource;
resource.dwType = RESOURCETYPE_DISK;
resource.lpLocalName = 0;
resource.lpRemoteName = L"\\\\server\\share";
resource.lpProvider = 0;
DWORD conResult;
DWORD result = WNetAddConnection2(&resource, L"password", L"username", CONNECT_TEMPORARY);
if (result == NO_ERROR) {
    // Go hog wild with files in \\server\share
}

要完成使用它,请执行以下操作:

DWORD retval = WNetCancelConnection2(L"\\\\server\\share", 0, TRUE);

现在请记住,如果您已经使用不同的凭据建立了与服务器的连接,那么我非常确定连接将会失败

关于c++ - 使用 Win32 C++ 访问 protected 网络共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23271418/

相关文章:

c++ - 使用CRTP时如何调用派生类的构造函数?

windows - 在 Windows 安装中标配的最接近 grep 的东西是什么?

c - 以编程方式发送 WM_SIZE 消息

c++ - 模板作为模板参数进行特化

c++ - void* 到 vector

c++ - 使用 Ninja 构建系统,我可以清理中间构建产品吗?

C++ Qt - 获取物理磁盘大小 (Win32)

.net - 如何创建(32 位).NET 应用程序以使用 3 GB RAM?

windows - 如何从父文件夹中搜索每个 ABC 文件夹以查找 *.txt 文件?

c++ - 资源窗口在其他系统上无法正常工作 (Visual Studio 2012)