gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 10:06:08 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com