Directory Services

Example Code for Displaying Extended Controls Support

The following code example queries an LDAP server for extended control support, and prints a list to the console window. If an error occurs it prints the hex value of the LDAP error instead. For more information and a list of error code values, see Return Values.

#ifndef _UNICODE
#define _UNICODE
#endif

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winldap.h>

// ULONG GetSupportedLdapControls( PCHAR HostName, PCHAR* pValList )
//	 [in] HostName	LDAP server name
//	 [out] pValList		Null-terminated array of control OIDs.
//
// This function queries an LDAP server for a list of its
// supported extended controls. If successful, it returns
// LDAP_SUCCESS and an array of control OIDs.
//
// The array returned in the pValList pointer must be freed with
// ldap_value_free() when no longer required. 

ULONG GetSupportedLdapControls( PCHAR HostName, PCHAR** ppValList )
{
   ULONG version = LDAP_VERSION3;
   ULONG lRtn = LDAP_SUCCESS;;
   LDAP* pSes = NULL;
   LDAPMessage* pMes = NULL;
   LDAPMessage* pEntry = NULL;

   // Initialize return value to NO CONTROLS SUPPORTED
   *ppValList = NULL;

   while(1)
   {
	// Create a new LDAP session.
	pSes = ldap_init(HostName,LDAP_PORT);
	if(pSes == NULL)
	{
		 lRtn = LdapGetLastError();
		 if(lRtn == LDAP_SUCCESS)
			lRtn = LDAP_OTHER;
		 break;
}

	// Be sure to use Version 3 support.
	lRtn = ldap_set_option(pSes,LDAP_OPT_VERSION,(void*)&version);
	if(lRtn != LDAP_SUCCESS)
		 break;

	// Get the RootDSE attributes.
	lRtn = ldap_search_s( pSes,
					 "",
					 LDAP_SCOPE_BASE,
					 "(objectClass=*)",
					 NULL,
					 0,
					 &pMes );

	if( lRtn != LDAP_SUCCESS )
		 break;

	// Return the support controls.
	pEntry = ldap_first_entry(pSes, pMes);
	if( pEntry != NULL )
		 *ppValList = ldap_get_values(pSes,
									pEntry,
									"supportedControl");
	break;
   }
   // Cleanup.
   if(pMes != NULL)
	ldap_msgfree(pMes);
   if(pSes != NULL)
	ldap_unbind_s(pSes);
   return lRtn;
}

PCHAR MyServer = "Fabrikam.com";
int main(int argc, char* argv[])
{
   PCHAR* pValList;
   ULONG lRtn;

   lRtn = GetSupportedLdapControls(MyServer, &pValList);
   if(lRtn == LDAP_SUCCESS)
   {
	PCHAR* pV = pValList;
	printf("Extended Controls Supported:\n");
	while(*pV)
	{
		 printf("	%s\n",*pV);
		 pV++;
}
	if(pValList != NULL)
		 ldap_value_free(pValList);
   }
   else
	printf("ERROR: code = 0x%0lX\n",lRtn);
   return 0;
}