使用Win32 C ++访问受保护的网络共享 [英] Access to a protected network share using Win32 C++

查看:182
本文介绍了使用Win32 C ++访问受保护的网络共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法访问登录/传递受保护的网络共享,列出文件,并获得他们的名称和创建日期,使用Win32 C ++ API?
我不想让samba网络共享出现在我的浏览器中。 (这可以使用WNetAddConnection2方法)。
感谢所有。

Is there a way to access a login/pass protected network share, to list files and get their names and creation dates, using the Win32 C++ API ? I do not want the samba network share to be present in my explorer. (this can be done with WNetAddConnection2 method). Thanks for all.

推荐答案

虽然我同意Ben在他的评论中提出的反对意见, WNetAddConnection2 。当您为 lpLocalName 参数传递 NULL 值时,它不会映射驱动器, ,因此允许您使用完整的 UNC 路径来执行任务,例如枚举远程系统上的文件。

While I agree with the objections Ben raises in his comment, you can keep using the WNetAddConnection2. When you pass in a NULL value for the lpLocalName parameter it will not map a drive, but will simply perform the authorization, and thus allow you to use the full UNC path to perform tasks such as enumerating the files on the remote system.

lpLocalName :指向以空字符结束的字符串的指针,指定要重定向的本地设备的名称,例如F:或LPT1。字符串以不区分大小写的方式处理。
如果字符串为空或者如果lpLocalName为NULL,则该函数将连接到网络资源,而不重定向本地设备。

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页面使用它很简单,但类似的东西(我没有手头的窗口框来验证任何代码的工作原理):

The MSDN page pretty much gives the skinny on using it, but something akin to (I don't have a windows box at hand to verify any of this code works):

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

现在请注意,如果您已经使用不同的凭据建立了与服务器的连接,我很确定连接会失败

Now bear in mind, if you've already established a connection to the server using different credentials, then I'm pretty certain the connection will fail

这篇关于使用Win32 C ++访问受保护的网络共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆