Click here to Skip to main content
Licence CPOL
First Posted 24 Aug 2006
Views 44,801
Downloads 225
Bookmarked 27 times

Simple Active Directory Browsing

By Vider | 2 Apr 2008
With this simple sample, you could query the Active Directory in just a few lines of code.

1
1 vote, 11.1%
2
3 votes, 33.3%
3
1 vote, 11.1%
4
4 votes, 44.4%
5
3.90/5 - 9 votes
μ 3.90, σa 2.04 [?]

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)

About the Author

Vider

Web Developer

Spain Spain

Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMemory Mgt... Pinmember-midiman-14:21 2 Apr '08  
GeneralRe: Memory Mgt... PinmemberVider22:51 2 Apr '08  
GeneralExcellent Work! PinmemberSainjure19:41 5 Nov '07  
GeneralRe: Excellent Work! PinmemberVider22:53 5 Nov '07  
Questionhow to show users in a group? Pinmemberliuty20062:52 25 Aug '06  
AnswerRe: how to show users in a group? [modified] PinmemberVider2:59 25 Aug '06  
GeneralRe: how to show users in a group? Pinmemberliuty20063:23 25 Aug '06  
GeneralRe: how to show users in a group? PinmemberVider3:25 25 Aug '06  
GeneralRe: how to show users in a group? Pinmemberliuty20064:12 25 Aug '06  
GeneralRe: how to show users in a group? PinmemberVider23:34 27 Aug '06  
GeneralRe: how to show users in a group? Pinmemberliuty20060:19 28 Aug '06  
GeneralRe: how to show users in a group? PinmemberVider0:25 28 Aug '06  
GeneralRe: how to show users in a group? Pinmemberliuty20060:29 28 Aug '06  
GeneralRe: how to show users in a group? PinmemberVider2:52 28 Aug '06  
GeneralRe: how to show users in a group? Pinmemberliuty20063:43 28 Aug '06  
GeneralRe: how to show users in a group? PinmemberVider3:57 28 Aug '06  
GeneralRe: how to show users in a group? [modified] Pinmemberliuty20064:17 28 Aug '06  
GeneralRe: how to show users in a group? PinmemberSystemTools16:09 28 Aug '06  
GeneralRe: how to show users in a group? PinmemberVider22:11 28 Aug '06  
GeneralRe: how to show users in a group? Pinmemberliuty200623:51 28 Aug '06  
GeneralRe: how to show users in a group? PinmemberVider0:02 29 Aug '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.5.120210.1 | Last Updated 3 Apr 2008
Article Copyright 2006 by Vider
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid