Listing users in active directory from local machine
Hi.
Here i want to list all active users in active directory by sitting in local machine. Is it possible to do. Please help in programming side.
My code is here
ActiveDir::ActiveDir(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
HRESULT hr;
IADsContainer *pCont = NULL;
CoInitialize(NULL);
hr = ADsGetObject(L"WinNT://avrstudio.com", IID_IADsContainer, (void**) &pCont );
// WinNT://
if ( !SUCCEEDED(hr) )
{
printf("ADsGetObject Failed");
return;
}
else printf("cvdsvdv");
_variant_t var;
IEnumVARIANTPtr pEnum;
ADsBuildEnumerator (pCont,&pEnum);
int cnt=0;
ULONG ulFetched = 0L;
_variant_t vChild;
hr= ADsEnumerateNext(pEnum, 1, &vChild, &ulFetched) ;
printf("@@@ %d ulFetched",ulFetched);
while((SUCCEEDED(ADsEnumerateNext(pEnum, 1, &vChild, &ulFetched)) /*&& (ulFetched>0)*/))
{
IADsUser* pADs;
hr = V_DISPATCH(&vChild)->QueryInterface(IID_IADsUser, (void**)&pADs);
/* Here i am getting error.... Getting vChild as empty. what i have to do.. Not getting any idea..
Please guide me */
if ( !SUCCEEDED(hr) )/* This getting failed, hr returning false*/
{
printf("\n V_DISPATCH Failed");
}
if(hr!=S_OK)
break;
BSTR bstrName;
pADs->get_Name(&bstrName);
SysFreeString(bstrName);
printf("%s\n",bstrName);
pADs->Release();
pADs = NULL;
}
// Cleanup.
if ( pCont )
{
pCont->Release();
}
CoUninitialize();
}
Please help
TrinityPosted Oct 10, 2009, 6:07 PM
public IEnumerable GetUsers(string GroupName)
{
//Users are supposed to be organized within groups
System.DirectoryServices.DirectoryEntry myMachine = new System.DirectoryServices.DirectoryEntry(string.Format("WinNT {0},Computer",
Environment.MachineName.ToString()));
//Catch a group of users from the acvtive directory
System.DirectoryServices.DirectoryEntry group = myMachine.Children.Find(GroupName, "group");
object AllUsers = group.Invoke("members", null);
//Enumerate them then return them as an Inumerable object
System.DirectoryServices.DirectoryEntry userInfo;
foreach (object userGroup in AllUsers as IEnumerable)
{
userInfo = new System.DirectoryServices.DirectoryEntry(userGroup);
yield return userInfo.Name;
}
}
Then you can use it to display all users of a given group like so
//Get the 'Administrators' users
IEnumerable result = GetUsers("Administrators");
IEnumerator resultInfo = result.GetEnumerator();
while (resultInfo.MoveNext())
{
MessageBox.Show(resultInfo.Current.ToString());
}