Elementary problems using winldap

When I try to perform a very simple connection using an example I found here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ldap/ldap/example_code_for_establishing_a_session_without_encryption.asp

with a few minor changes (such as using simple authentication) I get the following error :

Unhandled exception in ldaptest.exe (WLDAP32.DLL): 0xC0000005: Access Violation.

on the line with ldap_set_option...

I HAVEN'T EVEN CONNECTED TO ANYTHING YET!

When I remove the ldap_set and ldap_connect that both fail with the same error, the bind still fails...

Something is seriously wrong and I don't know what. I am trying to connect to a SunOne LDAP server, but that shouldn't really matter I suppose at it follows LDAP v3...

I have also tried to perform the simple bind with NULL NULL as username and password and get the same error, but I know it the LDAP directory I'm trying to connect to accepts anonymous connections...

I have submitted the code below.

Any help would be MUCH appreciated...

Thanks /Dave
--------------------------

//------------------
// Establish an LDAP session.
//------------------

#include <windows.h>
#include <winldap.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
LDAP* pLdapConnection = NULL;
ULONG version = LDAP_VERSION3;
ULONG getOptSuccess = 0;
ULONG connectSuccess = 0;
INT iRtn = 0;

//------------------
// Initialize a session. LDAP_PORT is the default port, 389.
//------------------
pLdapConnection = ldap_init("192.168.2.143", LDAP_PORT);

if (pLdapConnection == NULL)
{
printf( "ldap_init failed with 0x%x.\n",GetLastError());
goto error_exit;
}
else
printf("ldap_init succeeded \n");

/*
//-----------------
// Set the version to 3.0 (default is 2.0).
//-----------------
iRtn = ldap_set_option(pLdapConnection,
LDAP_OPT_PROTOCOL_VERSION,
(void*)&version);
if(iRtn == LDAP_SUCCESS)
printf("ldap_set_option succeeded version set to 3\n");
else
{
printf("SetOption Error:%0X\n", iRtn);
goto error_exit;
}

//------------------
// Connect to the server.
//------------------
connectSuccess = ldap_connect(pLdapConnection, NULL);

if(connectSuccess == LDAP_SUCCESS)
printf("ldap_connect succeeded \n");
else
{
printf("ldap_connect failed with 0x%x.\n",connectSuccess);
goto error_exit;
}
*/
//------------------
// Bind with current credentials (login credentials). Be
// aware that the password itself is never sent over the
// network, and encryption is not used.
//------------------
printf("Binding ...\n");

iRtn = ldap_simple_bind(pLdapConnection, "cn=Directory Manager", "xxxxx2222");
if (iRtn == LDAP_SUCCESS)
printf("The bind was successful");
else
goto error_exit;

//------------------
// Normal cleanup and exit.
//------------------
ldap_unbind(pLdapConnection);

return 0;

//------------------
// On error cleanup and exit.
//------------------
error_exit:
ldap_unbind(pLdapConnection);

return -1;
}
[3703 byte] By [abramowi] at [2007-11-19 1:10:25]
# 1 Re: Elementary problems using winldap
A couple of thoughts....

Try adding a call to ULONG LdapGetLastError(void) to get the any errors after the call to ldap_init - maybe after the existing else statement.

Also, you might want to use the ldap_simple_bind_s function to perform your bind.

ULONG ldap_simple_bind_s( LDAP* ld,
PCHAR dn,
PCHAR passwd
);

Parameters
ld
[in] The session handle.
dn
[in] The name of the user to bind as. The bind operation uses the dn and passwd parameters to authenticate the user.
passwd
[in] The password of the user specified in the dn parameter.

I would interested in knowing what errors you see from the LdapGetLastError when you are getting the access violation errors.

HTH
f1shrman at 2007-11-10 3:39:17 >
# 2 Re: Elementary problems using winldap
hi,
I'm having exactly the same problem; did anyone manage to solve this problem?
thanks
cel
marcel808 at 2007-11-10 3:40:17 >
# 3 Re: Elementary problems using winldap
hi.
I got the same problem and I added LdapGetLastError() below ldap_init. The return values is 0x00. Anybody have another idea to deal with this problem.
thanks
Nok
Nokko at 2007-11-10 3:41:11 >
# 4 Re: Elementary problems using winldap
By chance, can you run the code in debug mode and step through the code to see what pLdapConnection looks like after the call to ldap_init() just to make sure that the ldap structure is being properly set up. Also check it and all the other variables used in the ldap_set_option() call.
f1shrman at 2007-11-10 3:42:15 >
# 5 Re: Elementary problems using winldap
Hi all!
See if you have more than one version of wldap32.lib, I have 2, one from VC 6.0 ++, and another one from Visual Studio .Net 2003. The thing is, when I installed the late version it replaced the old dll with a new one, so the lib from VC 6.0++ doesnt work with the dll from .Net 2003. Just copy the wldap32.lib from:
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Lib
to
C:\Program Files\Microsoft Visual Studio\VC98\Lib

Hope it helps

Orlando Sa Morais
o.samorais at 2007-11-10 3:43:14 >
# 6 Re: Elementary problems using winldap
i only got 1 wldap32.lib but error still occurs, is it because the code is only for microsoft active directory?
sarcasigan at 2007-11-10 3:44:16 >
# 7 Re: Elementary problems using winldap
I had the problem too, but is now solved!

.Net studio is not installed, and I have just one wldap32.lib.

Details of my installed files:
wldap32.lib 13may98 49.462 bytes
WINLDAP.H 24apr98 86.863 bytes
wldap32.dll 4aug04 172.032 bytes

system :Microsoft Windows XP [Version 5.1.2600]
visual studo sp6

I have copied a better Wldap32.Lib to
C:\Program Files\Microsoft Visual Studio\VC98\Lib and now it works:

good lib: 26/03/2003 16:36 53.222 Wldap32.Lib
bad lib: 13/05/1998 00:00 49.462 WLDAP32.LIB

I have found the good lib by installing sdk from a cd and then copying from
C:\Program Files\Microsoft SDK\Lib\Wldap32.Lib

Note:
I have found this article:
http://support.microsoft.com/?kbid=283199#toc

This looks like our problem, except that the article does not mention windows xp platform.

Success!
fgh at 2007-11-10 3:45:20 >
# 8 Re: Elementary problems using winldap
I had same problem about LDAP API.
I was not able to find that solution during 4days...
Now I can solve it. Thank you very much.
mproduct at 2007-11-10 3:46:19 >