Directory Services


The ldap_search function searches search the LDAP directory and returns a requested set of attributes for each entry matched.

ULONG ldap_search(
  LDAP* ld,
  PCHAR base,
  ULONG scope,
  PCHAR filter,
  PCHAR attrs[],
  ULONG attrsonly


[in] The session handle.
[in] A pointer to a null-terminated string that contains the distinguished name of the entry at which to start the search.
[in] Specifies one of the following values to indicate the scope of the search.
Value Meaning
LDAP_SCOPE_BASE Search the base entry only.
LDAP_SCOPE_ONELEVEL Search all entries in the first level below the base entry, excluding the base entry.
LDAP_SCOPE_SUBTREE Search the base entry and all entries in the tree below the base.
[in] A pointer to a null-terminated string that specifies the search filter. For more information, see Search Filter Syntax.
[in] A null-terminated array of null-terminated strings indicating which attributes to return for each matching entry. Pass NULL to retrieve all available attributes.
[in] A boolean value that should be zero if both attribute types and values are to be returned, nonzero if types only are wanted.

Return Values

If the function succeeds, it returns the message ID of the search operation.

If the function fails, it returns –1 and sets the session error parameters in the LDAP data structure.


The ldap_search function initiates an asynchronous search operation.

You can use the ldap_set_option function with the ld session handle to set the LDAP_OPT_SIZELIMIT, LDAP_OPT_TIMELIMIT, and LDAP_OPT_DEREF options that determine how the search is performed. For more information, see Session Options.

As an asynchronous function, ldap_search 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 search operation before it has completed, call ldap_abandon.

If you prefer to have the function return the results directly, use the synchronous routine ldap_search_s. Use ldap_search_ext or ldap_search_ext_s if you need support for LDAP 3 server and client controls.

Multithreading: Calls to ldap_search are thread-safe, provided that LdapGetLastError is used to retrieve 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, ldap_abandon, ldap_bind, ldap_result, ldap_search_ext, ldap_search_ext_s, ldap_search_s, ldap_simple_bind