无法连接到Windows服务器active directory轻型服务



在使用windows服务器时,我想通过c++创建active directory轻量级服务用户,即通过使用ldap连接的c++。我的服务器在虚拟箱中运行,我将网络适配器设置为桥接网络,它可以很好地连接到网络。现在,当我使用微软文档中的代码时,ldap绑定没有连接。我得到的错误是发生了操作错误。

#include "atlbase.h"
#include "activeds.h"
int main(int argc, char* argv[])
{
HRESULT hr;
IADsContainer* pCont;
IDispatch* pDisp = NULL;
IADs* pUser;
// Initialize COM before calling any ADSI functions or interfaces.
CoInitialize(NULL);
hr = ADsGetObject(L"LDAP://ABC.local",
IID_IADsContainer,
(void**)&pCont);
if (!SUCCEEDED(hr))
{
return 0;
}
//-----------------
// Create a user
//-----------------
hr = pCont->Create(CComBSTR("user"), CComBSTR("cn=jeffsmith"), &pDisp);
// Release the container object.    
pCont->Release();
if (!SUCCEEDED(hr))
{
return 0;
}
hr = pDisp->QueryInterface(IID_IADs, (void**)&pUser);
// Release the dispatch interface.
pDisp->Release();
if (!SUCCEEDED(hr))
{
return 0;
}
// Commit the object data to the directory.
pUser->SetInfo();
// Release the object.
pUser->Release();
CoUninitialize();
}

这是我用来检查连接是否成功的代码。任何帮助都会非常有用,谢谢。

您要求一个容器(IID_IADsContainer(,但您给它的路径是到域的根(LDAP://ABC.local(。尝试指定要将用户放入的OU的可分辨名称。例如,如果要将用户置于域根的UsersOU中,则如下所示:

hr = ADsGetObject(L"LDAP://ABC.local/OU=Users,DC=ABC,DC=local",
IID_IADsContainer,
(void**)&pCont);

您可以在此处阅读有关LDAP路径格式的更多信息:LDAP ADsPath

最新更新