有没有办法使用 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/