Directory Services |
The following example code 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]; //Fill in 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 tack it on. 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); //Pick the control sent back 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 sent back 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); }