Ldap Query

Just starting out? Need help? Post your questions and find answers here.
TeddyLM
Enthusiast
Enthusiast
Posts: 133
Joined: Wed Apr 30, 2003 2:04 pm
Location: Germany (French expat)

Post by TeddyLM »

Hi Ajm

Thank you for sharing.
I have the same problem... I can't get this to work.
'ldap_count_entries' returns values other than -1 but 'ldap_first_entryA' still returns 0 (pointer).
Any ideas... anybody?

Ajm wrote:
I'm not sure I fully understand what i'm doing
same problem here :D

-where did you find the LDAP structure? MSDN says it's an opaque data type (same as LDAPMESSAGE structure)-

Code: Select all

#LDAP_PORT = 389
#LDAP_MSG_ONE = $00 ;retrieves one message at a time
#LDAP_MSG_ALL = $01 ;requests that all results of a search be received before returning all results in a single chain
#LDAP_MSG_RECEIVED = $02 ;indicates that all results retrieved so far should be returned in the result chain 
#LDAP_SCOPE_BASE = 0 ;Searches the base entry only. 
#LDAP_SCOPE_ONELEVEL = 1 ;Searches all entries in the first level below the base entry, excluding the base entry. 
#LDAP_SCOPE_SUBTREE = 2 ;Searches the base entry And all entries in the tree below the base.
#LDAP_SUCCESS = $00 ;The call completed successfully. 

Structure LDAP 
  ld_deref.l 
  ld_timelimit.l 
  ld_sizelimit.l 
  ld_errno.l 
  ld_matched.s 
  ld_error.s 
EndStructure 
    
lpres.l = 0
Domainname$ = "xxxxx"
base$ = "dc=xxxxx,dc=de" 
user$ = "xxxxx" 
password$ = "xxxxx" 
filter$ = "(objectClass=*)"  

If OpenLibrary(0, "wldap32.dll") 
    Debug "wldap32.dll Library found"
    *ld.LDAP = CallCFunction(0, "ldap_init", Domainname$, #LDAP_PORT) 
    If *ld 
        Debug "ldap_init successfully called"
        rt = CallCFunction(0, "ldap_connect", *ld, 0) 
        If rt = #LDAP_SUCCESS
            Debug "ldap_connect successfully called" 
            rt = CallCFunction(0, "ldap_simple_bindA", *ld, user$, password$)
            If rt <> -1
                Debug "ldap_simple_bind successfully called"                                
                rt = CallCFunction(0, "ldap_search_sA", *ld, base$, #LDAP_SCOPE_BASE, filter$, #Null, 0, @lpres)
                If rt = #LDAP_SUCCESS
                    Debug "ldap_search_s successfully called"                                     
                    rt = CallCFunction(0, "ldap_count_entries", *ld, lpres)  ;returns -1 (not successfull) or the number of entries
                    Debug "ldap_count_entries: "+Str(rt)
                    If rt <> -1                        
                        *fe = CallCFunction(0,"ldap_first_entryA",*ld, *rt) 
                        ;***
                        Debug *fe     ;always 0 ??
                        ;***                       
                    EndIf
                Else
                    Debug "Unable to search. $" + Hex(CallCFunction(0, "LdapGetLastError"))
                EndIf                                 
            Else
                Debug "Unable to bind. $" + Hex(CallCFunction(0, "LdapGetLastError"))
            EndIf
        Else
            Debug "Unable to connect. $" + Hex(CallCFunction(0, "LdapGetLastError"))
        EndIf
        CallCFunction(0, "ldap_unbind", *ld)
    Else
        Debug "Unable to call ldap_init. $" + Hex(CallCFunction(0, "LdapGetLastError"))  
    EndIf   
    CloseLibrary(0)
Else
    Debug "Unable to open the library. $" + Hex(CallCFunction(0, "LdapGetLastError"))
EndIf
End
User avatar
Shardik
Addict
Addict
Posts: 2075
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Ldap Query

Post by Shardik »

In TeddyLM's example code the following lines

Code: Select all

...
            rt = CallCFunction(0, "ldap_simple_bindA", *ld, user$, password$)
...
                rt = CallCFunction(0, "ldap_search_sA", *ld, base$, #LDAP_SCOPE_BASE, filter$, #Null, 0, @lpres)
...
                        *fe = CallCFunction(0,"ldap_first_entryA",*ld, *rt)
...
have to be modified in order for the example to work (Strings as parameters have to be prefixed with @, the A in ldap_first_entryA has to be removed and *rt has to be replaced by lpres):

Code: Select all

...
            rt = CallCFunction(0, "ldap_simple_bindA", *ld, @user$, @password$)
...
                rt = CallCFunction(0, "ldap_search_sA", *ld, @base$, #LDAP_SCOPE_BASE, @filter$, #Null, 0, @lpres)
...
                        *fe = CallCFunction(0,"ldap_first_entry",*ld, lpres)
...
A further hint is to use ImportC "Wldap32.Lib" to import the required functions from Wldap32.dll instead of using CallCFunction(). Wldap32.dll is normally contained in the Windows\System32 directory on Windows XP SP3 and Windows 7 x86 SP1, but to obtain Wldap32.Lib I had to extract this file from the Windows Platform SDK February 2003.
Post Reply