积极答复者
WNetAddConnection2在管理员权限下无法显示映射的盘符?(win7/win8)

问题
-
在win7/win8系统下,普通用户权限,利用WNetAddConnection2 函数来映射一个网络共享文 件夹到本地磁盘,映射成功
但是换成管理员权限运行后,资源管理器中不显示盘符,输入路径也无法访问,不过cmd下用net use命令可以看到映射的盘符
请问这是怎么回事,以及能不能解决
// WNetAddConnect.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <winnetwk.h>
#pragma comment(lib, "mpr.lib")
int
_tmain(
int
argc, _TCHAR* argv[])
{
NETRESOURCE nr = {0};
nr.dwType = RESOURCETYPE_ANY;
nr.lpLocalName = _T(
"Z:"
);
nr.lpRemoteName = _T(
"\\\\171.12.12.10\\share"
);
//
// nr.lpProvider = _T("\\\\171.12.12.10\\share");
nr.lpProvider = NULL;
DWORD
res = WNetAddConnection2(&nr, NULL, NULL, 0);
if
(res != NO_ERROR)
{
#define caseErr(x) case(x): { printf(#x "\n"); break; }
switch
(res)
{
caseErr(ERROR_ACCESS_DENIED);
//The caller does not have access to the network resource.
caseErr(ERROR_ALREADY_ASSIGNED);
//The local device specified by the lpLocalName member is already connected to a network resource.
caseErr(ERROR_BAD_DEV_TYPE);
//The type of local device and the type of network resource do not match.
caseErr(ERROR_BAD_DEVICE);
//The value specified by lpLocalName is invalid.
caseErr(ERROR_BAD_NET_NAME);
//The value specified by the lpRemoteName member is not acceptable to any network resource provider, either because the resource name is invalid, or because the named resource cannot be located.
caseErr(ERROR_BAD_PROFILE);
//The user profile is in an incorrect format.
caseErr(ERROR_BAD_PROVIDER);
//The value specified by the lpProvider member does not match any provider.
caseErr(ERROR_BUSY);
//The router or provider is busy, possibly initializing. The caller should retry.
caseErr(ERROR_CANCELLED);
//The attempt to make the connection was canceled by the user through a dialog box from one of the network resource providers, or by a called resource.
caseErr(ERROR_CANNOT_OPEN_PROFILE);
//The system is unable to open the user profile to process persistent connections.
caseErr(ERROR_DEVICE_ALREADY_REMEMBERED);
//An entry for the device specified by lpLocalName is already in the user profile.
caseErr(ERROR_EXTENDED_ERROR);
//A network-specific error occurred. Call the WNetGetLastError function to obtain a description of the error.
caseErr(ERROR_INVALID_PASSWORD);
//The specified password is invalid and the CONNECT_INTERACTIVE flag is not set.
caseErr(ERROR_NO_NET_OR_BAD_PATH);
//The operation cannot be performed because a network component is not started or because a specified name cannot be used.
caseErr(ERROR_NO_NETWORK);
//The network is unavailable.
default
:
{
printf
(
"Error: %ld\n"
, res);
}
}
WNetCancelConnection2(_T(
"Z:"
), CONNECT_UPDATE_PROFILE, TRUE);
}
// Sleep(5000);
return
0;
}
答案
-
你好,
》
DWORD
res = WNetAddConnection2(&nr, NULL, NULL, 0);
这个函数的调用第二个和第三个参数您传入的空,建议您传入相应的用户名和密码再试一下。您可以参考这个链接的例子。如果还是有问题,您可以把GetLastError code 提供给我们。
https://msdn.microsoft.com/en-us/library/windows/desktop/aa385413(v=vs.85).aspx
May
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- 已标记为答案 May Wang - MSFT 2016年1月22日 3:24
全部回复
-
你好,huluwa999
由于你的问题跟C++开发相关,我现在将这个帖子移到C++论坛以便得到更好的帮助,非常感谢你的理解。
Best Regards,
Albert Zhang
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey. -
你好,
》
DWORD
res = WNetAddConnection2(&nr, NULL, NULL, 0);
这个函数的调用第二个和第三个参数您传入的空,建议您传入相应的用户名和密码再试一下。您可以参考这个链接的例子。如果还是有问题,您可以把GetLastError code 提供给我们。
https://msdn.microsoft.com/en-us/library/windows/desktop/aa385413(v=vs.85).aspx
May
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- 已标记为答案 May Wang - MSFT 2016年1月22日 3:24