【发布时间】:2012-07-15 15:54:21
【问题描述】:
我编写了一些代码来连接远程服务器上的某个共享。如果WNetAddConnection2返回ERROR_SESSION_CREDENTIAL_CONFLICT(1219),我会先取消WNetCancelConnection2的连接(返回NO_ERROR)。然后重新连接。但是WNetAddConnection2 仍然返回1219。
为什么会这样以及如何解决?
这是我的代码
BOOL ADDirectorySearch::IPCConnect(CString strServerName, CString strDomainName, CString strUserName, CString strPassWord)
{
CString strServerNameWithSlash = _T("\\\\") + strServerName; //actually is \\klbnt
CString strFullUserName = strDomainName + _T("\\") + strUserName; //is domaintest\administrator
_bstr_t bstrServerNameWithSlash = strServerNameWithSlash;
_bstr_t bstrFullUserName = strFullUserName;
_bstr_t bstrPassWord = strPassWord;
DWORD dwResult;
NETRESOURCEW netResource;
memset(&netResource, 0, sizeof(netResource));
netResource.dwScope = RESOURCE_GLOBALNET;
netResource.dwType = RESOURCETYPE_DISK;
netResource.dwDisplayType = RESOURCEDISPLAYTYPE_GENERIC;
netResource.dwUsage = RESOURCEUSAGE_CONNECTABLE;
netResource.lpProvider = L"";
netResource.lpRemoteName = bstrServerNameWithSlash;//Remote IP like:\\192.168.1.11
dwResult = WNetAddConnection2W(&netResource, bstrPassWord, bstrFullUserName, CONNECT_INTERACTIVE);
if (dwResult == ERROR_SESSION_CREDENTIAL_CONFLICT)
{
dwResult = WNetCancelConnection2W(bstrServerNameWithSlash, CONNECT_UPDATE_PROFILE, TRUE);
if (dwResult == NO_ERROR)
{
dwResult = WNetAddConnection2W(&netResource, bstrPassWord, bstrFullUserName, CONNECT_INTERACTIVE);
}
else
{
//MyMessageBox_Error(_T("IPCConnect Error."), _T("Error"));
return FALSE;
}
}
if (dwResult == NO_ERROR)
{
return TRUE;
}
else
{
//MyMessageBox_Error(_T("IPCConnect Error."), _T("Error"));
return FALSE;
}
}
仅供参考:在cmd中输入“net use”后,我得到了这个,我觉得有什么错误:
Status Local Remote Network
-------------------------------------------------------------------------------
OK \\klbnt\NRDC1001 Microsoft Windows Network
The command completed successfully.
【问题讨论】:
-
您没有设置 resource.lpLocalName。在 sn-p 中看不到 WNetCancelConnection2()。
-
您可能需要在 WNetCancelConnection2 中指定共享名称以及服务器名称,即
\\klbnt\NRDC1001而不仅仅是\\klbnt。 -
我将 lpLocalName 设置为 "" 但没有任何改变。 WNetCancelConnection2 在我的代码中的 2 个 WNetAddConnection2 调用之间被调用。另外连接\\klbnt和\\klbnt\nrdc1001有什么关系?与 \\klbnt 的连接是否隐含包含与所有 klbnt 共享的连接?还是彼此什么都没有?我只是想授予我的进程访问远程共享的权限,那么这样做的正确方法是什么?
标签: c++ windows winapi active-directory