要连接到由NETRESOURCE结构描述的网络资源,应用程序可以调用WNetAddConnection2功能,如以下示例所示。
DWORD dwResult;
NETRESOURCE nr;
dwResult = WNetAddConnection2(&nr, // NETRESOURCE从枚举
(LPSTR) NULL, //没有密码
(LPSTR) NULL, //登录用户
CONNECT_UPDATE_PROFILE); //使用连接信息更新配置文件
if (dwResult == ERROR_ALREADY_ASSIGNED)
{
TextOut(hdc, 10, 10, "Already connected to specified resource.", 40);
return FALSE;
}
else if (dwResult == ERROR_DEVICE_ALREADY_REMEMBERED)
{
TextOut(hdc,10,10,
"Attempted reassignment of remembered device.", 44);
return FALSE;
}
else if(dwResult != NO_ERROR)
{
//应用程序定义的错误处理程序在
//标题为“Retrieving Network Errors”的部分。
NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetAddConnection2");
return FALSE;
}
TextOut(hdc, 10, 10, "Connected to specified resource.", 32);
与早期版本的Windows for Workgroups兼容,支持WNetAddConnection功能。对于新的应用程序,请使用WNetAddConnection2或WNetAddConnection3.