windows2003下ldap的使用

gyningwd 2009-04-09 07:40:05
#include "stdio.h"
#include "windows.h"
#include "winldap.h"
#pragma comment (lib,"Wldap32.lib")

#define HOSTNAME "localhost"
#define PORT_NUMBERLDAP_PORT
#define FIND_DN "uid=bjensen, ou=People, o=Airius.com"

int main()
{
LDAP*ld;
LDAPMessage*result, *e;
BerElement*ber;
char*a;
char**vals;
int i, rc;

/* Get a handle to an LDAP connection. */
if ( (ld = ldap_open( HOSTNAME, 50000 )) == NULL )
{
perror( "ldap_open" );
return( 1 );
}

/* Bind anonymously to the LDAP server. */
rc = ldap_simple_bind_s( ld, NULL, NULL );
if ( rc != LDAP_SUCCESS )
{
fprintf(stderr, "ldap_simple_bind_s: %s\n", ldap_err2string(rc));
return( 1 );
}

/* Search for the entry. */
if ( ( rc = ldap_search_ext_s( ld, FIND_DN, LDAP_SCOPE_BASE,
"(objectclass=*)", NULL, 0, NULL, NULL, LDAP_NO_LIMIT,
LDAP_NO_LIMIT, &result ) ) != LDAP_SUCCESS )
{
fprintf(stderr, "ldap_search_ext_s: %s\n", ldap_err2string(rc));
return( 1 );
}

/* Since we are doing a base search, there should be only one matching entry. */
e = ldap_first_entry( ld, result );
if ( e != NULL )
{
printf( "\nFound %s:\n\n", FIND_DN );

/* Iterate through each attribute in the entry. */
for ( a = ldap_first_attribute( ld, e, &ber );
a != NULL; a = ldap_next_attribute( ld, e, ber ) )
{
/* For each attribute, print the attribute name and values. */
if ((vals = ldap_get_values( ld, e, a)) != NULL )
{
for ( i = 0; vals[i] != NULL; i++ )
{
printf( "%s: %s\n", a, vals[i] );
}
ldap_value_free( vals );
}
ldap_memfree( a );
}
/*
if ( ber != NULL )
{
ber_free( ber, 0 );
} */
}
ldap_msgfree( result );
ldap_unbind( ld );
return( 0 );
}
运行上述程序结果是在bind()函数中返回信息协议错误
若使用ldap_init()则无法连接ldap
...全文
52 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
siguol 2009-04-09
  • 打赏
  • 举报
回复
学习...收藏先

6,205

社区成员

发帖
与我相关
我的任务
社区描述
windows网络管理与配置
社区管理员
  • 网络管理与配置社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧