2023年6月21日发(作者:)

#include

#include

#include

#include

void main()

{

HRESULT hr;

IDirectorySearch *pSearch;

// Inititalization

CoInitialize(NULL);

// Bind to the base search object

hr = ADsOpenObject(CComBSTR("LDAP://192.168.31.91"), //地址

CComBSTR("testuser"), //用户

CComBSTR("abc@123"), //密码

ADS_SECURE_AUTHENTICATION,

IID_IDirectorySearch,

(void**)&pSearch);

ADS_SEARCHPREF_INFO prefInfo[1];

prefInfo[0].dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;

prefInfo[0]. = ADSTYPE_INTEGER;

prefInfo[0].r = 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< alues; x++) wprintf(L"%s:

%s ",rName,lues[x].CaseIgnoreString);

}

pSearch->FreeColumn( &col );

}

FreeADsMem( pszColumn );

//Get the next row

hr = pSearch->GetNextRow( hSearch);

wprintf(L"n");

}

}

}

CoUninitialize();

//return S_OK;

}

2023年6月21日发(作者:)

#include

#include

#include

#include

void main()

{

HRESULT hr;

IDirectorySearch *pSearch;

// Inititalization

CoInitialize(NULL);

// Bind to the base search object

hr = ADsOpenObject(CComBSTR("LDAP://192.168.31.91"), //地址

CComBSTR("testuser"), //用户

CComBSTR("abc@123"), //密码

ADS_SECURE_AUTHENTICATION,

IID_IDirectorySearch,

(void**)&pSearch);

ADS_SEARCHPREF_INFO prefInfo[1];

prefInfo[0].dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;

prefInfo[0]. = ADSTYPE_INTEGER;

prefInfo[0].r = 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< alues; x++) wprintf(L"%s:

%s ",rName,lues[x].CaseIgnoreString);

}

pSearch->FreeColumn( &col );

}

FreeADsMem( pszColumn );

//Get the next row

hr = pSearch->GetNextRow( hSearch);

wprintf(L"n");

}

}

}

CoUninitialize();

//return S_OK;

}