这里有LDAP编程高手吗?请教。。。

silverwgy 2003-12-09 02:06:10
下面是我从MSDN上直接抄下来的代码,连接wldap32.lib库,编译成功
-----------------------
//--------------------------------------------------------
// Establish an LDAP session.
//--------------------------------------------------------

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

int main(int argc, char* argv[])
{
PCHAR hostName = "172.16.229.176";
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(hostName, 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_bind_s(pLdapConnection, NULL, NULL,
LDAP_AUTH_NEGOTIATE);
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;
}
...全文
29 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
silverwgy 2003-12-09
  • 打赏
  • 举报
回复
MS Platform SDK?
这个SDK是不是要另外下载呀?安装的时候没带的吧?
hellion 2003-12-09
  • 打赏
  • 举报
回复
用MS Platform SDK里面的.h和Lib,不要用VC6里面的
silverwgy 2003-12-09
  • 打赏
  • 举报
回复
自己up先。
silverwgy 2003-12-09
  • 打赏
  • 举报
回复
当运行ldap_set_option函数时,弹出一个Access Violation错误对话框
trace里显示如下:
Loaded 'C:\WINNT\system32\USER32.DLL', no matching symbolic information found.
Loaded 'C:\WINNT\system32\GDI32.DLL', no matching symbolic information found.
Loaded 'C:\WINNT\system32\imm32.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\lpk.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\usp10.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\wshtcpip.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\secur32.dll', no matching symbolic information found.
First-chance exception in Test1.exe (WLDAP32.DLL): 0xC0000005: Access Violation.
==================
如果我不调用ldap_set_option,而直接调用ldap_connect函数,也会出现同样的错误。
注:172.16.229.176是ldap server,我通过windows的查找用户都能找到用户信息的。
但编程却在这里卡壳了。
哪位高手帮帮忙。非常感谢。

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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