V
Vaidees Chandrasekaran
Guest
I am trying to save the users in a domain to a list.I can't do it.help this. my code in visual c++ is
#include "stdafx.h"
#include<Windows.h>
#include<Iads.h>
#include<AdsHlp.h>
#include<iostream>
#include<atlbase.h>
#include<wchar.h>
#include<comutil.h>
#include<string.h>
#include<AdsErr.h>
#include<list>
#include<stdlib.h>
#pragma comment(lib,"adsiid.lib")
#pragma comment(lib,"activeds.lib")
#ifdef _UNICODE
#define COUT wcout
#else
#define COUT cout
#endif
using namespace std;
class usersclass
{
public:
LPWSTR name;
int str;
};
int main()
{
list<usersclass> l;
CoInitialize(NULL);
string userdetails[10000];
HRESULT hr;
IDirectorySearch *pContainertoSearch;
int count=0;
hr = ADsOpenObject(L"LDAP://DC=fabrikam,DC=com", NULL, NULL,
ADS_SECURE_AUTHENTICATION, IID_IDirectorySearch, (void**)&pContainertoSearch);
if (hr == S_OK)
{
ADS_SEARCH_HANDLE hSearch;
LPCWSTR pszAttr[] = { L"samAccountName" };
ADS_SEARCHPREF_INFO prefInfo2[2];
prefInfo2[0].dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
prefInfo2[0].vValue.dwType = ADSTYPE_INTEGER;
prefInfo2[0].vValue.Integer = ADS_SCOPE_SUBTREE;
prefInfo2[1].dwSearchPref = ADS_SEARCHPREF_PAGESIZE;
prefInfo2[1].vValue.dwType = ADSTYPE_INTEGER;
prefInfo2[1].vValue.Integer = 1000;
hr = pContainertoSearch->SetSearchPreference(prefInfo2, 2);
hr = pContainertoSearch->ExecuteSearch((LPWSTR)L"(&(objectClass=user)(objectCategory=person))",
(LPWSTR*)pszAttr, sizeof(pszAttr) / sizeof(LPWSTR), &hSearch);
if (hr == S_OK)
{
while (pContainertoSearch->GetNextRow(hSearch) != S_ADS_NOMORE_ROWS)
{
ADS_SEARCH_COLUMN col;
hr = pContainertoSearch->GetColumn(hSearch, (LPWSTR)L"samAccountName", &col);
if (hr == S_OK)
{
if (col.dwADsType == ADSTYPE_CASE_IGNORE_STRING)
{
usersclass uc;
uc.name = col.pADsValues->CaseIgnoreString;
uc.str = count;
l.push_back(uc);
//printf("%S\n", col.pADsValues->CaseIgnoreString);
printf("%S\n", uc.name);
count++;
//std::cout << count;
pContainertoSearch->FreeColumn(&col);
}
else
std::cout << "Could not obtain the object\n";
}
else
{
printf("%0x\n", hr);
break;
}
}
pContainertoSearch->CloseSearchHandle(hSearch);
pContainertoSearch->Release();
}
}
CoUninitialize();
list<usersclass>::iterator it;
for (it = l.begin(); it != l.end(); ++it)
{
printf("%S\n",it->name);
cout<<it->str<<endl;
}
return 0;
}
Yeah I got all the users from my domain.I can print it inside that while loop above as u can see (commented lines).But when I try to save that in a list and print outside of the loop, only str is printed and name is not printing properly.
Continue reading...
#include "stdafx.h"
#include<Windows.h>
#include<Iads.h>
#include<AdsHlp.h>
#include<iostream>
#include<atlbase.h>
#include<wchar.h>
#include<comutil.h>
#include<string.h>
#include<AdsErr.h>
#include<list>
#include<stdlib.h>
#pragma comment(lib,"adsiid.lib")
#pragma comment(lib,"activeds.lib")
#ifdef _UNICODE
#define COUT wcout
#else
#define COUT cout
#endif
using namespace std;
class usersclass
{
public:
LPWSTR name;
int str;
};
int main()
{
list<usersclass> l;
CoInitialize(NULL);
string userdetails[10000];
HRESULT hr;
IDirectorySearch *pContainertoSearch;
int count=0;
hr = ADsOpenObject(L"LDAP://DC=fabrikam,DC=com", NULL, NULL,
ADS_SECURE_AUTHENTICATION, IID_IDirectorySearch, (void**)&pContainertoSearch);
if (hr == S_OK)
{
ADS_SEARCH_HANDLE hSearch;
LPCWSTR pszAttr[] = { L"samAccountName" };
ADS_SEARCHPREF_INFO prefInfo2[2];
prefInfo2[0].dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
prefInfo2[0].vValue.dwType = ADSTYPE_INTEGER;
prefInfo2[0].vValue.Integer = ADS_SCOPE_SUBTREE;
prefInfo2[1].dwSearchPref = ADS_SEARCHPREF_PAGESIZE;
prefInfo2[1].vValue.dwType = ADSTYPE_INTEGER;
prefInfo2[1].vValue.Integer = 1000;
hr = pContainertoSearch->SetSearchPreference(prefInfo2, 2);
hr = pContainertoSearch->ExecuteSearch((LPWSTR)L"(&(objectClass=user)(objectCategory=person))",
(LPWSTR*)pszAttr, sizeof(pszAttr) / sizeof(LPWSTR), &hSearch);
if (hr == S_OK)
{
while (pContainertoSearch->GetNextRow(hSearch) != S_ADS_NOMORE_ROWS)
{
ADS_SEARCH_COLUMN col;
hr = pContainertoSearch->GetColumn(hSearch, (LPWSTR)L"samAccountName", &col);
if (hr == S_OK)
{
if (col.dwADsType == ADSTYPE_CASE_IGNORE_STRING)
{
usersclass uc;
uc.name = col.pADsValues->CaseIgnoreString;
uc.str = count;
l.push_back(uc);
//printf("%S\n", col.pADsValues->CaseIgnoreString);
printf("%S\n", uc.name);
count++;
//std::cout << count;
pContainertoSearch->FreeColumn(&col);
}
else
std::cout << "Could not obtain the object\n";
}
else
{
printf("%0x\n", hr);
break;
}
}
pContainertoSearch->CloseSearchHandle(hSearch);
pContainertoSearch->Release();
}
}
CoUninitialize();
list<usersclass>::iterator it;
for (it = l.begin(); it != l.end(); ++it)
{
printf("%S\n",it->name);
cout<<it->str<<endl;
}
return 0;
}
Yeah I got all the users from my domain.I can print it inside that while loop above as u can see (commented lines).But when I try to save that in a list and print outside of the loop, only str is printed and name is not printing properly.
Continue reading...