来自 MSDN C++ 示例的 LDAP "The search filter is bad."错误
ldap "The search filter is bad." error from MSDN c++ example
i在MSDN的c++中运行LDAP示例。我发布这个是因为我不知道。代码连接和绑定很好,但当我运行搜索操作时,会出现错误的搜索过滤器错误,奇怪的是我正在运行MSDN教程中的基本过滤器:"(objectClass=*)"。这是代码:
#include "stdafx.h"
#include <windows.h>
#include <ntldap.h>
#include <winldap.h>
#include <winber.h>
#include <rpc.h>
#include <rpcdce.h>
#include <schnlsp.h>
#include <stdlib.h>
const size_t newsize = 100;
int main(int argc, char* argv[])
{
PWCHAR hostName = NULL;
LDAP* pLdapConnection = NULL;
ULONG version = LDAP_VERSION3;
ULONG getOptSuccess = 0;
ULONG connectSuccess = 0;
INT returnCode = 0;
PCHAR pUserName="israel";
PCHAR pPassword="israel";
argc=2;
argv[1]="sincronica.spo.br";
// Verify that the user passed a hostname.
if (argc > 1)
{
// Convert argv[] to a wchar_t*
size_t origsize = strlen(argv[1]) + 1;
size_t convertedChars = 0;
wchar_t wcstring[newsize];
mbstowcs_s(&convertedChars, wcstring, origsize, argv[1], _TRUNCATE);
wcscat_s(wcstring, L" (wchar_t *)");
hostName = wcstring;
}
else
{
hostName = NULL;
}
// Initialize a session. LDAP_PORT is the default port, 389.
pLdapConnection = ldap_init(hostName, LDAP_PORT);
if (pLdapConnection == NULL)
{
// Set the HRESULT based on the Windows error code.
char hr = HRESULT_FROM_WIN32(GetLastError());
printf( "ldap_init failed with 0x%x.n",hr);
goto error_exit;
}
else
printf("ldap_init succeeded n");
// Set the version to 3.0 (default is 2.0).
returnCode = ldap_set_option(pLdapConnection,
LDAP_OPT_PROTOCOL_VERSION,
(void*)&version);
if(returnCode == LDAP_SUCCESS)
printf("ldap_set_option succeeded - version set to 3n");
else
{
printf("SetOption Error:%0Xn", returnCode);
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;
}
//-------------------------------------------------------
// Set session options.
//-------------------------------------------------------
ULONG numReturns = 10;
ULONG lRtn = 0;
// 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");
//--------------------------------------------------------
// Bind with credentials.
//--------------------------------------------------------
PCHAR pMyDN = "DC=sincronia,DC=spo,DC=br";
SEC_WINNT_AUTH_IDENTITY secIdent;
secIdent.User = (unsigned short*)pUserName;
secIdent.UserLength = strlen(pUserName);
secIdent.Password = (unsigned short*)pPassword;
secIdent.PasswordLength = strlen(pPassword);
secIdent.Domain = (unsigned short*)hostName;
secIdent.DomainLength = strlen((char*)hostName);
secIdent.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
lRtn = ldap_bind_s(
pLdapConnection, // Session Handle
(PWCHAR)pMyDN, // Domain DN
(PWCHAR)&secIdent, // Credential structure
LDAP_AUTH_NEGOTIATE); // Auth mode
if(lRtn == LDAP_SUCCESS)
{
printf("ldap_bind_s succeeded n");
secIdent.Password = NULL; // Remove password pointer
pPassword = NULL; // Remove password pointer
}
else
{
printf("ldap_bind_s failed with 0x%lx.n",lRtn);
ldap_unbind(pLdapConnection);
return -1;
}
//----------------------------------------------------------
// Perform a synchronous search of fabrikam.com for
// all user objects that have a "person" category.
//----------------------------------------------------------
ULONG errorCode = LDAP_SUCCESS;
LDAPMessage* pSearchResult;
PCHAR pMyFilter = "(objectClass=*)";
errorCode = ldap_search_s(
pLdapConnection, // Session handle
(PWCHAR)pMyDN, // DN to start search
LDAP_SCOPE_BASE, // Scope
(PWCHAR)pMyFilter, // Filter
NULL, // Retrieve list of attributes
0, // Get both attributes and values
&pSearchResult); // [out] Search results
if (errorCode != LDAP_SUCCESS)
{
printf("ldap_search_s failed with 0x%0lx n",errorCode);
ldap_unbind_s(pLdapConnection);
if(pSearchResult != NULL)
ldap_msgfree(pSearchResult);
return -1;
}
else
printf("ldap_search succeeded n");
// Normal cleanup and exit.
ldap_unbind(pLdapConnection);
return 0;
// On error cleanup and exit.
error_exit:
ldap_unbind(pLdapConnection);
return -1;
};
问题取决于ANSII字符集。我将项目设置为多字节集,并将代码重组为该选择。
相关文章:
- 这对"With a stackless coroutine, only the top-level routine may be suspended."意味着什么
- Doees the 'this' 指针参与虚函数的多态行为
- QTableview Search
- LeetCode 1011.Binary Search,C++和Python的想法相同,但输出不同
- 为什么我会收到"Run-Time Check Failure #2 - Stack around the variable 'pr' was corrupted"错误?
- 您将如何连接"on the fly"文本+整数并将其传递给函数?
- 二叉搜索树 - 实现"search"函数
- 如何理解"Temporary objs are destroyed as the last step in evaluating the full-expression"?谁能用一些简单的例子来说明这
- 如何解决C++中声纳库贝静态代码分析错误"Explicitly define the missing copy constructor, move constructor .."
- Configuring the MPEG4MediaSink
- 这在C++ "It does not own the underlying data, and so is cheap to copy or assign"中意味着什么
- What is the std::chrono::time_point equivalent of std::numer
- 如何在数据库程序中添加"reduce the quantity of an item"功能?
- 使用自定义 nodejs 插件时的"The specified module could not be found"
- Boost.Python 和导入 dll,"The specified module could not be found"
- "Build succeeded"但"The breakpoint will not currently be hit"
- 尝试应用肖恩父母谈话"inheritance is the base class of evil"中的模式
- 为什么'allocate in one library and free in the other'是错误的
- 如何修复此错误"the value of 'x1' is not usable in a constant expression"?static_assert
- 来自 MSDN C++ 示例的 LDAP "The search filter is bad."错误