列出用户并将其存储在stl列表中 [英] List users and store it in stl list

查看:90
本文介绍了列出用户并将其存储在stl列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将域中的用户保存到列表中。我不能这样做。帮助这个。我在visual c ++中的代码是

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

使用命名空间std;

class usersclass

{

public:

LPWSTR名称;

int str;

}; < br $>


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);

< span style ="white-space:pre"> 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)

{

#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)
{

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; usersclass uc;

                                                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<< "无法获取对象\ n";;


}

其他

{

printf("%0x \ n",hr);

break;

}

}

pContainertoSearch - > CloseSearchHandle(hSearch);

pContainertoSearch-> Release();

}

}

CoUninitialize();





list< usersclass> :: iterator i t;



for(it = l.begin();它!= l.end(); ++ it)

{

printf("%S \ n",it-> name);

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);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; cout<< it-> str<< endl;

                cout<<it->str<<endl;

}

返回0;

}



}
return 0;
}

是的,我从我的域中获得了所有用户。我可以在上面的while循环中打印它,因为你可以看到(注释行)。但是当我尝试将其保存在列表中并在循环外打印时,只打印str并且名称打印不正确。

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.

推荐答案

您好,

做什么你的意思是"名字打印不正确"?

what do you mean by "name is not printing properly"?

你为什么要使用printf和cout?你只能使用cout。而不是printf,也使用cout。 名称打印正确吗?我认为这是"%S"的问题。在printf中,你应该只使用cout。 我不会混合printf和cout。

Why do you use printf and cout? You can only use cout. Instead of printf, use cout too. Is the name printed correctly then? I think it is a problem with "%S" in printf, you should use cout only. I would not mix printf and cout.

问候,Guido


这篇关于列出用户并将其存储在stl列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆