Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / ATL
Article

User Management using ADSI

Rate me:
Please Sign up or sign in to vote.
4.00/5 (6 votes)
23 Mar 2001 152K   1.3K   24   27
This article demonstrates user management using Active Directory Services

Sample Image - adsiuserdmo.gif

Introduction

The listing demonstrates how to use Active Directory Service to manage the users on WinNT and Windows 2000.

IADsContainer interface lets you crawl through the active directory. To manage users, you need to get ADS object with path WinNT://DomianName/MachineName. IADsWinNTSystemInfo interface provides this information. Once we have a container, we enumerate through the container and if the object supports IADsUser interface, we add it as the user in the ListView. IADsUser then lets you manage properties for individual users.

//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
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Nice Example, but? Pin
SW13-Feb-02 9:12
SW13-Feb-02 9:12 
GeneralRe: Nice Example, but? Pin
iknowindigo27-Sep-06 12:01
iknowindigo27-Sep-06 12:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.