65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 ADSI 进行用户管理

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (6投票s)

2001年3月24日

viewsIcon

155343

downloadIcon

1308

本文演示了使用 Active Directory Services 进行用户管理

Sample Image - adsiuserdmo.gif

引言

此示例演示如何使用 Active Directory 服务来管理 WinNT 和 Windows 2000 上的用户。

IADsContainer 接口允许您遍历 Active Directory。要管理用户,您需要使用路径 WinNT://DomianName/MachineName 获取 ADS 对象。IADsWinNTSystemInfo 接口提供此信息。一旦我们拥有一个容器,我们就会遍历该容器,如果对象支持 IADsUser 接口,我们就将其作为用户添加到 ListView 中。然后,IADsUser 允许您管理单个用户的属性。

//Get the Information about system name and domain name

IADsWinNTSystemInfo *pNTsys;
HRESULT hr = CoCreateInstance(CLSID_WinNTSystemInfo, 
   NULL,CLSCTX_INPROC_SERVER, 
   IID_IADsWinNTSystemInfo,  (void**)&pNTsys);
pNTsys->AddRef();
BSTR bstrCompName;
pNTsys->get_ComputerName(&bstrCompName);
CString cscompName=bstrCompName;
SysFreeString(bstrCompName);
BSTR bstrDomainName;
pNTsys->get_DomainName(&bstrDomainName);
CString CSDomainName=bstrDomainName;
SysFreeString(bstrDomainName);
pNTsys->Release();

//Form ADSPath
ADSPath.Format("WinNT://%s/%s",CSDomainName,cscompName);

//Get the container object
hr=ADsGetObject(ADSPath.AllocSysString(),IID_IADsContainer,(void **)&pUsers);

//Now Enumerate through the container
IEnumVARIANTPtr pEnum; 
ADsBuildEnumerator (pUsers,&pEnum);
int cnt=0;
while(1)
{
    _variant_t vChild;
    hr=ADsEnumerateNext (pEnum,1,&vChild,NULL);
    //Iterate as long as you get S_OK 
    if(hr!=S_OK)
        break;
    hr=vChild.pdispVal->QueryInterface (IID_IADsUser,(void **)&pChild);
    if(hr!=S_OK)
        continue;
    else
        //This object is user
}
© . All rights reserved.