Click here to Skip to main content
15,888,984 members
Articles / Desktop Programming / MFC
Article

Simple Active Directory Browsing

Rate me:
Please Sign up or sign in to vote.
3.88/5 (10 votes)
2 Apr 2008CPOL 83.3K   1.2K   31   21
With this simple sample, you could query the Active Directory in just a few lines of code.

Introduction

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:

br.OpenLDAP();
 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)
     m_lst.InsertItem(x,(LPSTR)_bstr_t(br.ColValue(colname)),0); 
    else
     m_lst.SetItemText(x,c,(LPSTR)_bstr_t(br.ColValue(colname))); 
   }
   x++;
  } while (br.Next());
 }

Now, you can search by GUIDs with:

br.SearchGUID("{69B3EE5C-89DD-427c-8CC6-764E545ED0AB}");

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;
    pryusers.OpenLDAP();
    s.Format("(&(objectCategory=user)(primaryGroupID=%s))",br.getprimaryGroupToken());
    if (pryusers.Search(_bstr_t(s))) {
     do {
      m_tree.InsertItem((LPSTR)_bstr_t(pryusers.ColValue("cn")),0,0,membercol);
     } while (pryusers.Next());
    }

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

License

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


Written By
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.

Comments and Discussions

 
GeneralMemory Mgt... Pin
-midiman-2-Apr-08 13:21
-midiman-2-Apr-08 13:21 
GeneralRe: Memory Mgt... Pin
Vider2-Apr-08 21:51
Vider2-Apr-08 21:51 
GeneralExcellent Work! Pin
Sainjure5-Nov-07 18:41
Sainjure5-Nov-07 18:41 
GeneralRe: Excellent Work! Pin
Vider5-Nov-07 21:53
Vider5-Nov-07 21:53 
Questionhow to show users in a group? Pin
liuty200625-Aug-06 1:52
liuty200625-Aug-06 1:52 
AnswerRe: how to show users in a group? [modified] Pin
Vider25-Aug-06 1:59
Vider25-Aug-06 1:59 
GeneralRe: how to show users in a group? Pin
liuty200625-Aug-06 2:23
liuty200625-Aug-06 2:23 
GeneralRe: how to show users in a group? Pin
Vider25-Aug-06 2:25
Vider25-Aug-06 2:25 
GeneralRe: how to show users in a group? Pin
liuty200625-Aug-06 3:12
liuty200625-Aug-06 3:12 
GeneralRe: how to show users in a group? Pin
Vider27-Aug-06 22:34
Vider27-Aug-06 22:34 
GeneralRe: how to show users in a group? Pin
liuty200627-Aug-06 23:19
liuty200627-Aug-06 23:19 
GeneralRe: how to show users in a group? Pin
Vider27-Aug-06 23:25
Vider27-Aug-06 23:25 
GeneralRe: how to show users in a group? Pin
liuty200627-Aug-06 23:29
liuty200627-Aug-06 23:29 
GeneralRe: how to show users in a group? Pin
Vider28-Aug-06 1:52
Vider28-Aug-06 1:52 
GeneralRe: how to show users in a group? Pin
liuty200628-Aug-06 2:43
liuty200628-Aug-06 2:43 
GeneralRe: how to show users in a group? Pin
Vider28-Aug-06 2:57
Vider28-Aug-06 2:57 
GeneralRe: how to show users in a group? [modified] Pin
liuty200628-Aug-06 3:17
liuty200628-Aug-06 3:17 
GeneralRe: how to show users in a group? Pin
SystemTools28-Aug-06 15:09
SystemTools28-Aug-06 15:09 
GeneralRe: how to show users in a group? Pin
Vider28-Aug-06 21:11
Vider28-Aug-06 21:11 
GeneralRe: how to show users in a group? Pin
liuty200628-Aug-06 22:51
liuty200628-Aug-06 22:51 
>>To get a true group membership listing, you have to do a search of the entire directory looking for user's whose primary group is set to the group in question.

But that is not what i want. Because usually I don't change the user's default primary group, when add user to a group.
For example : "user1" is add to a group "group1", and not set primary group to "group1" (The primary group is still "Domain users"). Then the "user1" shuold be appeared in "group1", when query users in "group1".

If I "do a search of the entire directory looking for user's whose primary group", then the qury result is not correct.

Another quesstion: What is the difference between "group" and "primary group"?
Is there any influence on user, when change a user's "primary group"?

regards



-- modified at 4:59 Tuesday 29th August, 2006
GeneralRe: how to show users in a group? Pin
Vider28-Aug-06 23:02
Vider28-Aug-06 23:02 

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.