Click to Rate and Give Feedback

  Switch on low bandwidth view
Example Code for Using LDAP VLV

The following code example shows how to create and use a VLV control in LDAP.

#define SECURITY_WIN32
#include <windows.h>
#include <stdio.h>
#include <winldap.h>
#include <rpc.h>
#include <rpcdce.h>
#include <security.h>

void __cdecl main (int argc, char* argv[])
{
   LDAP *ld;
   ULONG ldaperr;

   ld = ldap_open("servername", 389);

   if (ld) 
   {
      printf("Open succeeded\n");
   } 
   else 
   {
      printf("Open failed\n");
      return;
   }

   ldaperr = ldap_bind_s(ld, NULL, NULL, LDAP_AUTH_NEGOTIATE);

   if (ldaperr == LDAP_SUCCESS) 
   {

      printf("Bind succeeded\n");
   } 
   else 
   {
      printf("Bind failed with 0x%x  0x%x\n", ldaperr, GetLastError);
      ldap_unbind(ld);
      return;
   }

   // Create the VLV control and send it to the server.

   LDAPVLVInfo     vlvInfo;
   PLDAPControl    pvlvctrl;
   PLDAPControl    psortctrl;
   PLDAPControl    ctrlArr[3];

   // Enter the members of the VLV control.

   vlvInfo.ldvlv_after_count = 1;
   vlvInfo.ldvlv_attrvalue = NULL;
   vlvInfo.ldvlv_before_count = 1;
   vlvInfo.ldvlv_context = NULL;
   vlvInfo.ldvlv_count = 0;
   vlvInfo.ldvlv_extradata = NULL;
   vlvInfo.ldvlv_offset = 2;
   vlvInfo.ldvlv_version = LDAP_VLVINFO_VERSION;

   ldaperr = ldap_create_vlv_control( ld,
                                      &vlvInfo,
                                      TRUE,
                                      &pvlvctrl);

   printf("Create VLV control returned 0x%x\n",ldaperr);
   if (LDAP_SUCCESS != ldaperr)
   return;

   // Create a sort control and attach it.

   LDAPSortKey sortkeys;
   PLDAPSortKey sortkeyList[2];

   sortkeys.sk_attrtype = "name";
   sortkeys.sk_matchruleoid = NULL;
   sortkeys.sk_reverseorder = FALSE;

   sortkeyList[0] = &sortkeys;
   sortkeyList[1] = NULL;

   ldaperr = ldap_create_sort_control( ld,
                                       sortkeyList,
                                       TRUE,
                                       &psortctrl
                                       );

   printf("Create Sort control returned 0x%x\n",ldaperr);
   if (LDAP_SUCCESS != ldaperr)
   return;
   
   ctrlArr[0] = psortctrl;
   ctrlArr[1] = pvlvctrl;
   ctrlArr[2] = NULL;

   // Send the VLV control, with a search request, to the server.

   PLDAPMessage pRes;

   ldaperr = ldap_search_ext_s( ld,
                                "OU=Users,DC=DCname,
                                 DC=Fabrikam,DC=com",
                                LDAP_SCOPE_SUBTREE,
                                (objectClass=*),
                                NULL,
                                1,
                                ctrlArr,
                                NULL,
                                NULL,
                                0,
                                &pRes
                                );

   printf("Ldap search returned 0x%x\n",ldaperr);
   
   // Choose the control returned from the server.

   PLDAPControl    *pvlvresponse = NULL;

   ldaperr = ldap_parse_result( ld,
                                pRes,
                                NULL,
                                NULL,
                                NULL,
                                NULL,
                                &pvlvresponse,
                                FALSE
                                );

   printf("Ldap parse_result returned 0x%x\n",ldaperr);

   // Parse the VLV control returned from the server.

   ULONG   targetpos =0;
   ULONG   listcount = 0;
   int     errcode = LDAP_SUCCESS;
   PBERVAL context = NULL;

   ldaperr = ldap_parse_vlv_control( ld,
                                     pvlvresponse,
                                     &targetpos,
                                     &listcount,
                                     &context,
                                     &errcode
                                     );

   printf("Ldap parse vlv control returned 0x%x\n",ldaperr);
   printf("TargetPos = %d\n", targetpos);
   printf("ListCount = %d\n", listcount);
   printf("errcode = 0x%x\n", errcode);

   ldap_unbind(ld);
	  ldap_control_free(pvlvctrl);
	  ldap_control_free(psortctrl);
	  ldap_msgfree(pRes);
	  ldap_controls_free(pvlvresponse);
   ber_bvfree(context);
}

Send comments about this topic to Microsoft

Build date: 1/15/2009

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker