Elementary problems using winldap
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;
}

