网站首页  词典首页

请输入您要查询的函数:

 

术语 wnetaddconnection2
释义 WNetAddConnection2
语法:
C++
DWORD WNetAddConnection2(
__in LPNETRESOURCE lpNetResource,
__in LPCTSTR lpPassword,
__in LPCTSTR lpUsername,
__in DWORD dwFlags
);
WNetAddConnection2函数
该WNetAddConnection2函数使连接到网络资源。该函数可以重定向本地设备网络资源。
该WNetAddConnection2函数取代WNetAddConnection功能。如果您可以传递一个句柄一个窗口,对网络资源的提供者可以使用一个对话框的所有者窗口,调用WNetAddConnection3函数来代替。
参数
lpNetResource [in]
一个 NETRESOURCE结构,指定连接的建议,如有关网络资源的信息,详细的指针,本地设备和网络资源提供商。
您必须指定的NETRESOURCE结构以下成员。
MemberMeaning
网络资源dwTypeThe类型连接。
如果lpLocalName成员指向一个非空字符串,该成员可以等于RESOURCETYPE_DISK或RESOURCETYPE_PRINT。
如果lpLocalName是NULL,或者如果它指向一个空字符串,却提示可以等于RESOURCETYPE_DISK,RESOURCETYPE_PRINT,或RESOURCETYPE_ANY。
虽然这个成员是必需的,它的信息可能被忽略的网络服务供应商。
lpLocalNameA指向一个空结束的字符串,它指定了一个本地设备名,如重定向的“F:”或“中将”lpt1。该字符串被视为一个不区分大小写的方式。
如果字符串为空,或者lpLocalName为NULL,则函数将网络资源没有重定向本地设备的连接。
lpRemoteNameA指向一个空结束的字符串,它指定了网络资源的连接。该字符串可达MAX_PATH字符的长度,而且必须遵循网络提供商的命名约定。
lpProviderA指向一个空结束的字符串,指定供应商的网络连接。
如果lpProvider是NULL,或者如果它指向一个空字符串解析字符串,操作系统尝试确定正确的供应商指出,由lpRemoteName成员。
如果这个成员不是NULL,操作系统尝试建立连接只名为网络提供商。
您应该设置这个成员仅当您知道您的网络提供商要使用。否则,让操作系统确定供应商网络的名称映射到。
该WNetAddConnection2函数忽略了NETRESOURCE结构的其他成员。
lpPassword [in]
阿为一个恒定的空指针结尾的字符串,指定要使用的密码,在使网络连接。
如果lpPassword为NULL,则函数使用当前默认密码与由lpUserName参数指定的用户关联。
如果lpPassword指向一个空字符串,该函数不使用密码。
如果连接失败,因为一个无效的密码和CONNECT_INTERACTIVE值在dwFlags参数设置,函数显示一个对话框,要求用户输入密码。
Windows 95/98中:此参数必须为NULL或空字符串。
lpUsername [in]
阿为一个恒定的空指针结尾的字符串,用于指定建立连接的用户名。
如果lpUserName为NULL,则函数使用默认的用户名。 (在这个进程的用户上下文提供了默认用户名。)
该lpUserName参数指定当用户想连接到网络资源,他们被分配一个用户名或帐户比默认的用户名或账户。
用户名字符串都代表一个安全上下文。这可能是特定于网络提供商。
Windows 95/98中:此参数必须为NULL或空字符串。
dwFlags [in]
一个连接选项。在连接选项可能的值是定义在Winnetwk.h头文件。下面的值目前可以使用。
ValueMeaning
CONNECT_UPDATE_PROFILE
0x00000001The网络资源的连接应该记住。
如果此位标志设置,操作系统会自动尝试恢复连接时,用户登录。
该操作系统还记得成功的连接重定向本地设备。它不记得连接不成功或deviceless连接。 (阿deviceless连接时发生lpLocalName成员为NULL或为空字符串点。)
如果此位标志清晰,操作系统不会尝试恢复连接时,用户登录。
CONNECT_UPDATE_RECENT
0x00000002The网络资源的连接不应该忘记。如果此标志设置,操作系统将不会尝试恢复连接当用户再次登录。
CONNECT_TEMPORARY
0x00000004The网络资源的连接不应该忘记。如果此标志设置,操作系统将不会尝试恢复连接当用户再次登录。
CONNECT_INTERACTIVE
0x00000008If设置此标志,操作系统可能会互动的用户进行身份验证。
CONNECT_PROMPT
0x00000010This标志指示系统不使用情况下为用户提供的机会,提供一个可供选择的用户名或密码任何默认设置。这个标志被忽略,除非CONNECT_INTERACTIVE也设置。
CONNECT_REDIRECT
0x00000080This旗部队的地方时,连接设备重定向。
如果NETRESOURCE lpLocalName成员指定本地设备重定向,这个标志没有任何效果,因为操作系统仍然试图重定向指定的设备。当操作系统自动选择本地设备,却提示成员必须不等于RESOURCETYPE_ANY。
如果此标志没有设置,本地设备自动重定向选择了只有在网络要求本地设备被重定向。
Windows Server 2003和Windows XP的:当系统自动分配网络驱动器字母,字母Z开头指派:,则Y:,并结束与C:。这将减少每次登录(如网络驱动器号)和全球驱动器号(如磁盘驱动器)驱动器号相撞。请注意,早期版本的Windows分配驱动器号开始的C:和结束与Z:。
CONNECT_CURRENT_MEDIA
0x00000200If这标志设置,操作系统会提示使用命令行,而不是一个图形用户界面(GUI)认证的用户。这个标志被忽略,除非CONNECT_INTERACTIVE也设置。
CONNECT_COMMANDLINE
0x00000800If这标志设置,操作系统会提示使用命令行,而不是一个图形用户界面(GUI)认证的用户。这个标志被忽略,除非CONNECT_INTERACTIVE也设置。
Windows 2000中:此值是在Windows 2000及更高版本的支持。
CONNECT_CMD_SAVECRED
0x00001000If设置此标志,并在操作系统提示,凭据应当由认证管理器保存的凭据。如果凭据经理是调用者的登录会话,或者如果网络运营商不支持保存凭据残疾人,此标志将被忽略。这个标志被忽略,除非CONNECT_INTERACTIVE也设置。这个标志也被忽略,除非您设置CONNECT_COMMANDLINE标志。
Windows 2000中:此值是在Windows 2000及更高版本的支持。
CONNECT_CRED_RESET
0x00002000If设置此标志,和操作系统提示的凭据,凭据是重置凭证管理。如果凭据经理是调用者的登录会话,或者如果网络运营商不支持保存凭据残疾人,此标志将被忽略。这个标志也被忽略,除非您设置CONNECT_COMMANDLINE标志。
Windows Vista的:此值是在Windows Vista和更高版本的支持。
返回值
如果函数成功,返回值为NO_ERROR。
如果函数失败,返回值可以是下面的错误代码或系统错误代码之一。
返回codeDescription
ERROR_ACCESS_DENIEDThe调用方不具有访问网络资源。
ERROR_ALREADY_ASSIGNEDThe本地设备的lpLocalName成员中指定已经连接到网络资源。
ERROR_BAD_DEV_TYPEThe类型的本地设备和网络资源类型不匹配。
ERROR_BAD_DEVICEThe指定的设备名无效。返回此错误如果NETRESOURCE结构lpLocalName委员指出的lpNetResource参数指定的设备不redirectable。
ERROR_BAD_NET_NAMEThe网络名称无法找到。该值是返回如果NETRESOURCE结构lpRemoteName委员指出的lpNetResource参数指定的资源,是不能接受的任何网络资源供应商,或者是因为资源名称是空的,无效的,或者因为命名的资源不能定位。
ERROR_BAD_PROFILEThe用户配置文件是不正确的格式。
ERROR_BAD_PROVIDERThe指定的网络提供程序名称无效。返回此错误如果NETRESOURCE结构lpProvider委员指出的lpNetResource参数指定的值不匹配任何网络供应商。
ERROR_BAD_USERNAMEThe指定的用户名无效。
ERROR_BUSYThe路由器或供应商正忙,可能初始化。打电话者应该重试。
ERROR_CANCELLEDThe企图使连接被取消通过一个对话框,从网络资源供应商,或由一个所谓的资源由用户。
ERROR_CANNOT_OPEN_PROFILEThe系统无法打开用户配置文件来处理持久连接。
ERROR_DEVICE_ALREADY_REMEMBEREDThe本地设备名已到另一网络资源的记录连接。返回此错误,如果是由NETRESOURCE结构lpLocalName成员中指定的设备项指出的lpNetResource参数指定一个值,在用户配置文件里已经有一个比不同的连接中指定lpNetResource参数。
ERROR_EXTENDED_ERRORA网络特定的错误。调用WNetGetLastError函数获取错误的描述。
ERROR_INVALID_ADDRESSAn试图访问无效地址。返回此错误,如果dwFlags参数指定一个CONNECT_REDIRECT的价值,而是NETRESOURCE结构lpLocalName委员指出的lpNetResource参数是不确定的。
ERROR_INVALID_PARAMETERA参数不正确。返回此错误如果NETRESOURCE结构却提示委员指出的lpNetResource参数指定一个值大于RESOURCETYPE_DISK,RESOURCETYPE_PRINT,或RESOURCETYPE_ANY等。此错误也返回如果dwFlags参数指定一个不正确的或未知的值。
ERROR_INVALID_PASSWORDThe指定的密码无效,CONNECT_INTERACTIVE未设置标志。
ERROR_LOGON_FAILUREA因为有一个未知的用户名或错误密码登录失败。
ERROR_NO_NET_OR_BAD_PATHNo网络提供程序接受指定的网络路径。返回此错误,如果没有网络供应商承认NETRESOURCE结构lpRemoteName委员指出的lpNetResource参数。
ERROR_NO_NETWORKThe网络不可用。
OtherUse FormatMessage获取返回的错误消息字符串。
备注
在Windows Server 2003和Windows XP中,WNet函数创建和删除网络驱动器的MS字母- DOS设备命名与登录会话,因为MS - DOS设备是由AuthenticationID确定(本地唯一的标识符,或的LUID,与相关的关联登录会话。)这可能会影响应用程序的WNet函数调用来创建下一个用户登录网络驱动器号,现有的网络驱动器字母,但在不同的用户登录查询。这种情况的一个例子是,当用户的第二个登录,在登录会话中创建,例如,通过调用运行CreateProcessAsUser函数,第二个登录的应用程序调用GetLogicalDrives函数。到GetLogicalDrives函数调用不返回的网络驱动器由WNet函数创建信呼吁在首次登录。请注意,在前面的例子中第一个登录会话仍然存在,而且例子可以适用于任何登录会话,包括终端服务会话。有关更多信息,请参见定义一个MS - DOS设备名。
在Windows Server 2003和Windows XP中,如果一个服务作为LocalSystem运行WNetAddConnection2函数调用,然后映射的驱动器是大家有目共睹的用户登录会话。
对于微软网络供应商,该NETRESOURCE结构lpRemoteName委员指出的lpNetResource参数可以包含在点分十进制IPv4地址。一种每股例如可以如下:
\\ \\ 192.168.1.1 \\共享
在Windows Vista和更高版本Microsoft网络供应商,该NETRESOURCE结构lpRemoteName委员指出的lpNetResource参数可以包含一个IPv6地址。但是,IPv6的文字格式必须使用,使IPv6的地址是正确解析。一个IPv6地址是文字的形式:
IPv6的与':'由'替换的字符地址- '字符其次是“。IPv6的literal.net”字符串。
例如,下面的IPv6地址:
2001:4898:9:3:c069:aa97:fe76:2449
为每股例如可以如下:
\\ \\ 2001 - 4898 - 9 - 3 - c069 - aa97 - fe76 - 2449.ipv6 - literal.net \\共享
其他网络供应商可能会支持NETRESOURCE结构lpRemoteName委员指出的lpNetResource参数包含一个IPv4或IPv6地址,但是这是符合特定网络提供商。
Windows 7和Windows服务器2008 R2:如果WNetAddConnection2函数调用与明确的用户在pUsername和lpPassword指定的凭据,以建立与特定服务器上的网络资源的连接,然后要求与这些参数为NULL或再次(使用默认的用户名或默认密码)到相同的服务器,与呼叫失败。返回的错误将ERROR_BAD_USERNAME或ERROR_INVALID_PASSWORD。
实例
#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "mpr.lib")
#include
#include
#include
#include
// Need to link with Netapi32.lib and Mpr.lib
int wmain(int argc, wchar_t * argv[])
{
DWORD dwRetVal;
NETRESOURCE nr;
DWORD dwFlags;
if (argc != 5) {
wprintf(L"Usage: %s \\n",
argv[0]);
wprintf(L" %s X: \\\\\\\\contoso\\\\public testuser testpasswd\\n",
argv[0]);
exit(1);
}
wprintf(L"Calling WNetAddConnection2 with\\n");
wprintf(L" lpLocalName = %s\\n", argv[1]);
wprintf(L" lpRemoteName = %s\\n", argv[2]);
wprintf(L" lpUsername = %s\\n", argv[3]);
wprintf(L" lpPassword = %s\\n", argv[4]);
// Zero out the NETRESOURCE struct
memset(&nr, 0, sizeof (NETRESOURCE));
// Assign our values to the NETRESOURCE structure.
nr.dwType = RESOURCETYPE_ANY;
nr.lpLocalName = argv[1];
nr.lpRemoteName = argv[2];
nr.lpProvider = NULL;
// Assign a value to the connection options
dwFlags = CONNECT_UPDATE_PROFILE;
//
// Call the WNetAddConnection2 function to assign
// a drive letter to the share.
//
dwRetVal = WNetAddConnection2(&nr, argv[4], argv[3], dwFlags);
//
// If the call succeeds, inform the user; otherwise,
// print the error.
//
if (dwRetVal == NO_ERROR)
wprintf(L"Connection added to %s\\n", nr.lpRemoteName);
else
wprintf(L"WNetAddConnection2 failed with error: %u\\n", dwRetVal);
exit(1);
}

至于其他的代码示例,说明了如何连接到网络资源使用WNetAddConnection2函数,请参阅添加网络连接和指定驱动器的共享。
要求:
最低支持:client-Windows 2000专业版
最低支持server-Windows 2000服务器
HeaderWinnetwk.h
LibraryMpr.lib
DLLMpr.dll
Unicode和ANSI namesWNetAddConnection2W(Unicode)和WNetAddConnection2A(ANSI)的
参见
Windows网络(WNet)概述
Windows网络函数
WNetAddConnection3
WNetCancelConnection2
WNetGetConnection
NETRESOURCE
如果有任何问题和意见,请发送给微软(wsddocfb@microsoft.com)
生成日期:2009年8月6日
==英文原文==WNetAddConnection2 Function
The WNetAddConnection2 function makes a connection to a network resource. The function can redirect a local device to the network resource.
The WNetAddConnection2 function supersedes the WNetAddConnection function. If you can pass a handle to a window that the provider of network resources can use as an owner window for dialog boxes, call the WNetAddConnection3 function instead.
Syntax
C++
DWORD WNetAddConnection2(
__in LPNETRESOURCE lpNetResource,
__in LPCTSTR lpPassword,
__in LPCTSTR lpUsername,
__in DWORD dwFlags
);
Parameters
lpNetResource [in]
A pointer to a NETRESOURCE structure that specifies details of the proposed connection, such as information about the network resource, the local device, and the network resource provider.
You must specify the following members of the NETRESOURCE structure.
MemberMeaning
dwTypeThe type of network resource to connect to.
If the lpLocalName member points to a nonempty string, this member can be equal to RESOURCETYPE_DISK or RESOURCETYPE_PRINT.
If lpLocalName is NULL, or if it points to an empty string, dwType can be equal to RESOURCETYPE_DISK, RESOURCETYPE_PRINT, or RESOURCETYPE_ANY.
Although this member is required, its information may be ignored by the network service provider.
lpLocalNameA 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.
lpRemoteNameA pointer to a null-terminated string that specifies the network resource to connect to. The string can be up to MAX_PATH characters in length, and must follow the network provider's naming conventions.
lpProviderA pointer to a null-terminated string that specifies the network provider to connect to.
If lpProvider is NULL, or if it points to an empty string, the operating system attempts to determine the correct provider by parsing the string pointed to by the lpRemoteName member.
If this member is not NULL, the operating system attempts to make a connection only to the named network provider.
You should set this member only if you know the network provider you want to use. Otherwise, let the operating system determine which provider the network name maps to.

The WNetAddConnection2 function ignores the other members of the NETRESOURCE structure.
lpPassword [in]
A pointer to a constant null-terminated string that specifies a password to be used in making the network connection.
If lpPassword is NULL, the function uses the current default password associated with the user specified by the lpUserName parameter.
If lpPassword points to an empty string, the function does not use a password.
If the connection fails because of an invalid password and the CONNECT_INTERACTIVE value is set in the dwFlags parameter, the function displays a dialog box asking the user to type the password.
Windows Me/98/95: This parameter must be NULL or an empty string.
lpUsername [in]
A pointer to a constant null-terminated string that specifies a user name for making the connection.
If lpUserName is NULL, the function uses the default user name. (The user context for the process provides the default user name.)
The lpUserName parameter is specified when users want to connect to a network resource for which they have been assigned a user name or account other than the default user name or account.
The user-name string represents a security context . It may be specific to a network provider.
Windows Me/98/95: This parameter must be NULL or an empty string.
dwFlags [in]
A set of connection options. The possible values for the connection options are defined in the Winnetwk.h header file. The following values can currently be used.
ValueMeaning
CONNECT_UPDATE_PROFILE
0x00000001The network resource connection should be remembered.
If this bit flag is set, the operating system automatically attempts to restore the connection when the user logs on.
The operating system remembers only successful connections that redirect local devices. It does not remember connections that are unsuccessful or deviceless connections. (A deviceless connection occurs when the lpLocalName member is NULL or points to an empty string.)
If this bit flag is clear, the operating system does not try to restore the connection when the user logs on.
CONNECT_UPDATE_RECENT
0x00000002The network resource connection should not be remembered. If this flag is set, the operating system will not attempt to restore the connection when the user logs on again.
CONNECT_TEMPORARY
0x00000004The network resource connection should not be remembered. If this flag is set, the operating system will not attempt to restore the connection when the user logs on again.
CONNECT_INTERACTIVE
0x00000008If this flag is set, the operating system may interact with the user for authentication purposes.
CONNECT_PROMPT
0x00000010This flag instructs the system not to use any default settings for user names or passwords without offering the user the opportunity to supply an alternative. This flag is ignored unless CONNECT_INTERACTIVE is also set.
CONNECT_REDIRECT
0x00000080This flag forces the redirection of a local device when making the connection.
If the lpLocalName member of NETRESOURCE specifies a local device to redirect, this flag has no effect, because the operating system still attempts to redirect the specified device. When the operating system automatically chooses a local device, the dwType member must not be equal to RESOURCETYPE_ANY.
If this flag is not set, a local device is automatically chosen for redirection only if the network requires a local device to be redirected.
Windows Server 2003 and Windows XP: When the system automatically assigns network drive letters, letters are assigned beginning with Z:, then Y:, and ending with C:. This reduces collision between per-logon drive letters (such as network drive letters) and global drive letters (such as disk drives). Note that earlier versions of Windows assigned drive letters beginning with C: and ending with Z:.
CONNECT_CURRENT_MEDIA
0x00000200If this flag is set, the operating system prompts the user for authentication using the command line instead of a graphical user interface (GUI). This flag is ignored unless CONNECT_INTERACTIVE is also set.
CONNECT_COMMANDLINE
0x00000800If this flag is set, the operating system prompts the user for authentication using the command line instead of a graphical user interface (GUI). This flag is ignored unless CONNECT_INTERACTIVE is also set.
Windows 2000: This value is supported on Windows 2000 and later.
CONNECT_CMD_SAVECRED
0x00001000If this flag is set, and the operating system prompts for a credential, the credential should be saved by the credential manager. If the credential manager is disabled for the caller's logon session, or if the network provider does not support saving credentials, this flag is ignored. This flag is ignored unless CONNECT_INTERACTIVE is also set. This flag is also ignored unless you set the CONNECT_COMMANDLINE flag.
Windows 2000: This value is supported on Windows 2000 and later.
CONNECT_CRED_RESET
0x00002000If this flag is set, and the operating system prompts for a credential, the credential is reset by the credential manager. If the credential manager is disabled for the caller's logon session, or if the network provider does not support saving credentials, this flag is ignored. This flag is also ignored unless you set the CONNECT_COMMANDLINE flag.
Windows Vista: This value is supported on Windows Vista and later.

Return Value
If the function succeeds, the return value is NO_ERROR.
If the function fails, the return value can be one of the following error codes or one of the system error codes .
Return codeDescription
ERROR_ACCESS_DENIEDThe caller does not have access to the network resource.
ERROR_ALREADY_ASSIGNEDThe local device specified by the lpLocalName member is already connected to a network resource.
ERROR_BAD_DEV_TYPEThe type of local device and the type of network resource do not match.
ERROR_BAD_DEVICEThe specified device name is not valid. This error is returned if the lpLocalName member of the NETRESOURCE structure pointed to by the lpNetResource parameter specifies a device that is not redirectable.
ERROR_BAD_NET_NAMEThe network name cannot be found. This value is returned if the lpRemoteName member of the NETRESOURCE structure pointed to by the lpNetResource parameter specifies a resource that is not acceptable to any network resource provider, either because the resource name is empty, not valid, or because the named resource cannot be located.
ERROR_BAD_PROFILEThe user profile is in an incorrect format.
ERROR_BAD_PROVIDERThe specified network provider name is not valid. This error is returned if the lpProvider member of the NETRESOURCE structure pointed to by the lpNetResource parameter specifies a value that does not match any network provider.
ERROR_BAD_USERNAMEThe specified user name is not valid.
ERROR_BUSYThe router or provider is busy, possibly initializing. The caller should retry.
ERROR_CANCELLEDThe 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.
ERROR_CANNOT_OPEN_PROFILEThe system is unable to open the user profile to process persistent connections.
ERROR_DEVICE_ALREADY_REMEMBEREDThe local device name has a remembered connection to another network resource. This error is returned if an entry for the device specified by lpLocalName member of the NETRESOURCE structure pointed to by the lpNetResource parameter specifies a value that is already in the user profile for a different connection than that specified in the lpNetResource parameter.
ERROR_EXTENDED_ERRORA network-specific error occurred. Call the WNetGetLastError function to obtain a description of the error.
ERROR_INVALID_ADDRESSAn attempt was made to access an invalid address. This error is returned if the dwFlags parameter specifies a value of CONNECT_REDIRECT, but the lpLocalName member of the NETRESOURCE structure pointed to by the lpNetResource parameter was unspecified.
ERROR_INVALID_PARAMETERA parameter is incorrect. This error is returned if the dwType member of the NETRESOURCE structure pointed to by the lpNetResource parameter specifies a value other than RESOURCETYPE_DISK, RESOURCETYPE_PRINT, or RESOURCETYPE_ANY. This error is also returned if the dwFlags parameter specifies an incorrect or unknown value.
ERROR_INVALID_PASSWORDThe specified password is invalid and the CONNECT_INTERACTIVE flag is not set.
ERROR_LOGON_FAILUREA logon failure because of an unknown user name or a bad password.
ERROR_NO_NET_OR_BAD_PATHNo network provider accepted the given network path. This error is returned if no network provider recognized the lpRemoteName member of the NETRESOURCE structure pointed to by the lpNetResource parameter.
ERROR_NO_NETWORKThe network is unavailable.
OtherUse FormatMessage to obtain the message string for the returned error.

Remarks
On Windows Server 2003 and Windows XP, the WNet functions create and delete network drive letters in the MS-DOS device namespace associated with a logon session because MS-DOS devices are identified by AuthenticationID (a locally unique identifier , or LUID, associated with a logon session.) This can affect applications that call one of the WNet functions to create a network drive letter under one user logon, but query for existing network drive letters under a different user logon. An example of this situation could be when a user's second logon is created within a logon session, for example, by calling the CreateProcessAsUser function, and the second logon runs an application that calls the GetLogicalDrives function. The call to the GetLogicalDrives function does not return network drive letters created by WNet function calls under the first logon. Note that in the preceding example the first logon session still exists, and the example could apply to any logon session, including a Terminal Services session. For more information, see Defining an MS-DOS Device Name .
On Windows Server 2003 and Windows XP, if a service that runs as LocalSystem calls the WNetAddConnection2 function, then the mapped drive is visible to all user logon sessions.
For Microsoft network providers, the lpRemoteName member of the NETRESOURCE structure pointed to by the lpNetResource parameter can contain an IPv4 address in dotted-decimal notation. An example for a share might be the following:
\\\\192.168.1.1\\share
For Microsoft network providers on Windows Vista and later, the lpRemoteName member of the NETRESOURCE structure pointed to by the lpNetResource parameter can contain an IPv6 address. However, the IPv6 literal format must be used so that the IPv6 address is parsed correctly. An IPv6 literal address is of the form:
ipv6-address with the ':' characters replaced by '-' characters followed by the ".ipv6-literal.net" string.
For example, for the following IPv6 address:
2001:4898:9:3:c069:aa97:fe76:2449
an example for a share might be the following:
\\\\2001-4898-9-3-c069-aa97-fe76-2449.ipv6-literal.net\\share
Other network providers may support the lpRemoteName member of the NETRESOURCE structure pointed to by the lpNetResource parameter that contains an IPv4 or IPv6 address, but this is up to specific network provider.
Windows 7 and Windows Server 2008 R2: If the WNetAddConnection2 function is called with explicit user credentials specified in the pUsername and lpPassword to establish a connection with a network resource on a specific server and then called again with either of these parameters as NULL (to use the default user name or default password) to the same server, the call with fail. The error returned will be ERROR_BAD_USERNAME or ERROR_INVALID_PASSWORD.
Examples
The following code sample illustrates how to use the WNetAddConnection2 function to make connection to a network resource.
#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "mpr.lib")
#include
#include
#include
#include
// Need to link with Netapi32.lib and Mpr.lib
int wmain(int argc, wchar_t * argv[])
{
DWORD dwRetVal;
NETRESOURCE nr;
DWORD dwFlags;
if (argc != 5) {
wprintf(L"Usage: %s \\n",
argv[0]);
wprintf(L" %s X: \\\\\\\\contoso\\\\public testuser testpasswd\\n",
argv[0]);
exit(1);
}
wprintf(L"Calling WNetAddConnection2 with\\n");
wprintf(L" lpLocalName = %s\\n", argv[1]);
wprintf(L" lpRemoteName = %s\\n", argv[2]);
wprintf(L" lpUsername = %s\\n", argv[3]);
wprintf(L" lpPassword = %s\\n", argv[4]);
// Zero out the NETRESOURCE struct
memset(&nr, 0, sizeof (NETRESOURCE));
// Assign our values to the NETRESOURCE structure.
nr.dwType = RESOURCETYPE_ANY;
nr.lpLocalName = argv[1];
nr.lpRemoteName = argv[2];
nr.lpProvider = NULL;
// Assign a value to the connection options
dwFlags = CONNECT_UPDATE_PROFILE;
//
// Call the WNetAddConnection2 function to assign
// a drive letter to the share.
//
dwRetVal = WNetAddConnection2(&nr, argv[4], argv[3], dwFlags);
//
// If the call succeeds, inform the user; otherwise,
// print the error.
//
if (dwRetVal == NO_ERROR)
wprintf(L"Connection added to %s\\n", nr.lpRemoteName);
else
wprintf(L"WNetAddConnection2 failed with error: %u\\n", dwRetVal);
exit(1);
}

For other code samples that illustrates how to make a connection to a network resource using the WNetAddConnection2 function, see Adding a Network Connection and Assigning a Drive to a Share .
Requirements
Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderWinnetwk.h
LibraryMpr.lib
DLLMpr.dll
Unicode and ANSI namesWNetAddConnection2W (Unicode) and WNetAddConnection2A (ANSI)
See Also
Windows Networking (WNet) Overview
Windows Networking Functions
WNetAddConnection3
WNetCancelConnection2
WNetGetConnection
NETRESOURCE
Send comments about this topic to Microsoft
Build date: 8/6/2009
==原始网址==http://msdn.microsoft.com/en-us/library/aa385413(VS.85).aspx\n
随便看

 

windows api函数参考手册包含2258条windows api函数文档,详细介绍nodejs、java、rust调用windows api的方法技巧,是学习windows api编程的入门中文文档。

 

Copyright © 2004-2023 Winrtm.com All Rights Reserved
京ICP备2021023879号-40 更新时间:2024/10/6 11:25:15