#include "stdafx.h"
#include <activeds.h>
#include <adsiid.h>
#include <stdio.h>
#include <atlbase.h>
#pragma comment(lib, "Activeds.lib")
#pragma comment(lib, "adsiid.lib")
void main()
{
HRESULT hr;
IDirectorySearch *pSearch;
// Inititalization
CoInitialize(NULL);
// Bind to the base search object
//此函数也可以验证用户和用户名是否合法
hr = ADsOpenObject(CComBSTR("LDAP://192.168.1.191"), //地址
CComBSTR("administrator"), //用户
CComBSTR("Admin123"), //密码
ADS_SECURE_AUTHENTICATION,
IID_IDirectorySearch,
(void**)&pSearch);
ADS_SEARCHPREF_INFO prefInfo[1];
prefInfo[0].dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
prefInfo[0].vValue.dwType = ADSTYPE_INTEGER;
prefInfo[0].vValue.Integer = 100;
hr = pSearch->SetSearchPreference( prefInfo, 1);
if (FAILED(hr))
{
pSearch->Release();
//return hr;
}
// Prepare for attributes to be returned
LPWSTR pszSearchFilter = L"(&(objectClass=user)(objectCategory=person))";
//LPWSTR pszAttr[] = {L"sAMAccountName"};
LPWSTR pszAttr[] = {L"name"};
ADS_SEARCH_HANDLE hSearch;
LPOLESTR pszColumn = NULL;
int iCount = 0;
DWORD x = 0;
DWORD dwCount= sizeof(pszAttr)/sizeof(LPWSTR);
// Search
hr = pSearch->ExecuteSearch(
pszSearchFilter,
pszAttr,
dwCount,
&hSearch
);
// Enumerate the search result
ADS_SEARCH_COLUMN col;
if ( SUCCEEDED(hr) )
{
// Call IDirectorySearch::GetNextRow() to retrieve the next row of data
hr = pSearch->GetFirstRow(hSearch);
if (SUCCEEDED(hr))
{
while( hr != S_ADS_NOMORE_ROWS )
{
//Keep track of count.
iCount++;
// loop through the array of passed column names,
// print the data for each column
while( pSearch->GetNextColumnName( hSearch, &pszColumn ) != S_ADS_NOMORE_COLUMNS )
{
hr = pSearch->GetColumn( hSearch, pszColumn, &col );
if ( SUCCEEDED(hr) )
{
for (x = 0; x< col.dwNumValues; x++)
wprintf(L"%s: %s ",col.pszAttrName,col.pADsValues[x].CaseIgnoreString);
}
pSearch->FreeColumn( &col );
}
FreeADsMem( pszColumn );
//Get the next row
hr = pSearch->GetNextRow( hSearch);
wprintf(L"\n");
}
}
}
CoUninitialize();
//return S_OK;
}
获取AD域下的所有用户
最新推荐文章于 2024-11-01 20:05:40 发布