Click here to Skip to main content
13,294,516 members (58,567 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 24 Aug 2006

Simple Active Directory Browsing

, 2 Apr 2008
Rate this:
Please Sign up or sign in to vote.
With this simple sample, you could query the Active Directory in just a few lines of code.


Browsing the Active Directory is hard work if you don't have COM skills. With the simple class presented here, you will query Active Directory and start use the information on it in just a few minutes.

Sample screenshot

The class is called CNFCActiveDirBrowser. And the basic methods available are:

  • bool OpenLDAP(); - Open the basic context.
  • bool Search(_bstr_t ldappath); - Search.
  • bool First();
  • bool Next();
  • bool Previous();
  • bool Last();
  • bool GetNextColumnName(_bstr_t& colname);
  • _variant_t ColValue(_bstr_t colname);

You will navigate the Active Directory with:

 if (br.Search(_bstr_t(query))) {
  int x = 0;    
_bstr_t columnname;
    int ic = 0;
    while (br.GetNextColumnName(columnname)) {
     m_lst.InsertColumn(ic++,(LPSTR)columnname, LVCFMT_LEFT, 150);

  do {
   char buf[_MAX_PATH];
   LVCOLUMN col;
   col.mask = LVCF_TEXT;
   col.pszText = buf;
   col.cchTextMax = _MAX_PATH;
   for (int c = 0; m_lst.GetColumn(c, &col); c++) {
    _bstr_t colname(col.pszText);
    if (c == 0)
  } while (br.Next());

Now, you can search by GUIDs with:


And navigate all the members of a column:

short  ColValue(DWORD index, _bstr_t colname, _variant_t& result);

Now, following comments, I'll populate the group members that Microsoft doesn't show in the member column. These are the members that have the primary group ID equal to the primary group token in the current group. Thanks to Kevin Stanush (SystemTools Software Inc.) for explaining to me the problem.

The primary group token will be retrieved now from the main class getprimaryGroupToken(), and the users with this primary group token is populated with the same class; as simple as:

CNFCActiveDirBrowser pryusers;
    if (pryusers.Search(_bstr_t(s))) {
     do {
     } while (pryusers.Next());

This class proved to be useful for my Active Directory integration.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Spain Spain
I've a lot of hobbies, like Software Programing (yes, it's also my job Blush | :O ), Electronics Design, IA, Mechanics, Running&Mountain Bike, Telescopes, lathe and mill.

You may also be interested in...

Comments and Discussions

GeneralMemory Mgt... Pin
-midiman-2-Apr-08 14:21
member-midiman-2-Apr-08 14:21 
GeneralRe: Memory Mgt... Pin
Vider2-Apr-08 22:51
memberVider2-Apr-08 22:51 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 3 Apr 2008
Article Copyright 2006 by Vider
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid