Directory Services


Use the ldap_compare function to determine whether an attribute for a given entry holds a known value.

ULONG ldap_compare(
  LDAP* ld,
  PCHAR dn,
  PCHAR attr,
  PCHAR value


[in] The session handle.
[in] A pointer to a null-terminated string that contains the distinguished name of the entry.
[in] A pointer to a null-terminated string that contains the attribute to compare.
[in] A pointer to a null-terminated string that contains the string attribute value to compare to the attribute value.

Return Values

If the function succeeds, the message ID of the compare operation is returned.

If the function fails, it returns –1 and sets the session error parameters in the LDAP structure. This error can then be retrieved using LdapGetLastError.


The ldap_compare function initiates an asynchronous compare operation, comparing the value of an attribute to a known string value. Use ldap_compare_ext or ldap_compare_ext_s to compare binary values. Use ldap_compare_s or ldap_compare_ext_s to perform a synchronous compare operation.

As an asynchronous function, ldap_compare returns a message ID for the operation. Call ldap_result with the message ID to get the result of the operation. To cancel an asynchronous operation before it has been completed, call ldap_abandon.

To have the function return the results directly, use the synchronous routine ldap_compare_s. Use ldap_compare_ext or ldap_compare_ext_s to enable support for LDAP 3 server and client controls.

Multithreading: Calls to ldap_compare are thread-safe, provided that LdapGetLastError is used to get the actual session error code when the function call returns the -1 failure code.

Note  When connecting to an LDAP 2 server, the application must perform a bind operation, by calling one of the ldap_bind or ldap_simple_bind routines, before attempting other operations.


Client: Included in Windows XP and Windows 2000 Professional.
Server: Included in Windows Server 2003 and Windows 2000 Server.
Redistributable: Requires Active Directory Client Extension on Windows NT 4.0 SP6a and Windows 95/98/Me.
Unicode: Implemented as Unicode and ANSI versions on all platforms.
Header: Declared in Winldap.h.
Library: Use Wldap32.lib.

See Also

Functions, ldap_abandon, ldap_bind, ldap_compare_ext, ldap_compare_ext_s, ldap_compare_s, ldap_result, ldap_simple_bind