Есть приложение на Qt.
имя пользователя на локальной машине (login): dimon
Домен: guzpc.local
Сервер: dc02.guzpc.local
Нужно получить из Active Directory полноре имя пользователя, ну и еще разные параметры.
Соединение устанавливается, дерево строится, а вот поиск никак не работает. Видимо я где то не вкуриваю что то.
Код: Выделить всё
// Add Wldap32.lib to your project.
// Add Wldap32.dll to your project.
#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include "stdafx.h"
#include "windows.h"
#include "winldap.h"
#include "stdio.h"
#include <QLibrary>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
//ui->setupUi(this);
PWCHAR hostName = NULL;
PWCHAR pMyDN = NULL;
LDAP* pLdapConnection = NULL;
ULONG version = LDAP_VERSION3;
ULONG connectSuccess = 0;
ULONG returnCode = 0;
ULONG searchSuccess = 0;
// Initialize a session. LDAP_PORT is the default port, 389.
char *hostName_2 = "dc02.guzpc.local";
QString Test = QString::fromLatin1(hostName_2);
hostName = (WCHAR*)(Test.utf16());
char *pMyDN_2 = "Ou=directory ,Dc=dimon, DC=guzpc";
QString test2 = QString::fromLatin1(pMyDN_2);
pMyDN = (WCHAR*)(test2.utf16());
pLdapConnection = ldap_init(hostName, LDAP_PORT);
if (pLdapConnection == NULL)
{
// Set the HRESULT based on the Windows error code.
char hr = HRESULT_FROM_WIN32(GetLastError());
qDebug() << "ldap_init failed with 0x" << hr;
ldap_unbind(pLdapConnection);
return ;
}
else
qDebug() << "ldap_init succeeded";
// 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)
qDebug() << "ldap_set_option succeeded - version set to 3";
else
{
qDebug() << "SetOption Error: " << returnCode;
ldap_unbind(pLdapConnection);
return ;
}
if(returnCode == LDAP_SUCCESS)
qDebug() << "ldap_set_option succeeded - version set to 3";
else
{
qDebug() << "SetOption Error: " << returnCode;
ldap_unbind(pLdapConnection);
return ;
}
// Connect to the server.
connectSuccess = ldap_connect(pLdapConnection, NULL);
if(connectSuccess == LDAP_SUCCESS)
qDebug() << "ldap_connect succeeded 0x" + QString::number(connectSuccess,16);
else
{
qDebug() << "ldap_connect failed with 0x" + QString::number(connectSuccess,16);
ldap_unbind(pLdapConnection);
return ;
}
// Bind with current credentials (login credentials). Be
// aware that the password itself is never sent over the
// network, and encryption is not used.
qDebug () << "Binding ...";
returnCode = ldap_bind_s(pLdapConnection, NULL, NULL,
LDAP_AUTH_NEGOTIATE);
if (returnCode == LDAP_SUCCESS)
qDebug() << "The bind was successful ";
else
{
ldap_unbind(pLdapConnection);
qDebug() << "The bind was unsuccessful with 0x" + QString::number(returnCode,16);
return ;
}
//Variables for Search Results
LDAPMessage* pSearchResult;
PWCHAR pMyFilter = NULL;
char *pMyFilter_2 = "(&(objectCategory=person)(objectClass=user))";
QString Test7 = QString::fromLatin1(pMyFilter_2);
pMyFilter = (WCHAR*)(Test7.utf16());
PWCHAR pMyAttributes[6];
pMyAttributes[0] = (WCHAR*)QString("cn").utf16();
pMyAttributes[1] = (WCHAR*)QString("company").utf16();
pMyAttributes[2] = (WCHAR*)QString("givenName").utf16();
pMyAttributes[3] = (WCHAR*)QString("sn").utf16();
pMyAttributes[4] = (WCHAR*)QString("memberOf").utf16();
pMyAttributes[5] = NULL;
searchSuccess = ldap_search_s(
pLdapConnection, // Session handle
pMyDN, // DN to start search
LDAP_SCOPE_SUBTREE, // Scope
pMyFilter, // Filter
pMyAttributes, // Retrieve list of attributes
0, // Get both attributes and values
&pSearchResult); // [out] Search results
if (searchSuccess != LDAP_SUCCESS)
{
qDebug() << "ldap_search_s failed with 0x0" + QString::number(searchSuccess,16);
ldap_unbind_s(pLdapConnection);
if(pSearchResult != NULL)
{
ldap_msgfree(pSearchResult);
}
}
else
qDebug() << "ldap_search succeeded ";
//*********************************************************************************************************
// Get the number of entries returned.
ULONG numberOfEntries;
numberOfEntries = ldap_count_entries(
pLdapConnection, // Session handle
pSearchResult); // Search result
/*if(numberOfEntries == NULL)
{
qDebug("ldap_count_entries failed with 0x%0lx \n",errorCode);
ldap_unbind_s(pLdapConnection);
if(pSearchResult != NULL)
ldap_msgfree(pSearchResult);
}
else
qDebug("ldap_count_entries succeeded \n");
*/
qDebug("The number of entries is: %d \n", numberOfEntries);
//----------------------------------------------------------
// Loop through the search entries, get, and output the
// requested list of attributes and values.
//----------------------------------------------------------
/* LDAPMessage* pEntry = NULL;
PWCHAR pEntryDN = NULL;
ULONG iCnt = 0;
char* sMsg;
BerElement* pBer = NULL;
PWCHAR pAttribute = NULL;
PWCHAR* ppValue = NULL;
ULONG iValue = 0;
for( iCnt=0; iCnt < numberOfEntries; iCnt++ )
{
// Get the first/next entry.
if( !iCnt )
pEntry = ldap_first_entry(pLdapConnection, pSearchResult);
else
pEntry = ldap_next_entry(pLdapConnection, pEntry);
// Output the entry number.
qDebug("ENTRY NUMBER %i \n", iCnt);
// Get the first attribute name.
pAttribute = ldap_first_attribute(
pLdapConnection, // Session handle
pEntry, // Current entry
&pBer); // [out] Current BerElement
// Output the attribute names for the current object
// and output values.
while(pAttribute != NULL)
{
// Output the attribute name.
QString abc = QString::fromWCharArray(pAttribute);
qDebug() << "abc" << abc;
// Get the string values.
ppValue = ldap_get_values(
pLdapConnection, // Session Handle
pEntry, // Current entry
pAttribute); // Current attribute
// Print status if no values are returned (NULL ptr)
if(ppValue == NULL)
{
qDebug(": [NO ATTRIBUTE VALUE RETURNED]");
}
// Output the attribute values
else
{
iValue = ldap_count_values(ppValue);
if(!iValue)
{
qDebug(": [BAD VALUE LIST]");
}
else
{
// Output the first attribute value
QString abc2 = QString::fromWCharArray(*ppValue);
qDebug()<< "abc2" << abc2;
qDebug()<< "iValue" << iValue;
// Output more values if available
ULONG z;
for(z=1; z<iValue; z++)
{
QString abc3 = QString::fromWCharArray(ppValue[z]);
qDebug() << "abc3" << abc3;
}
}
}
// Free memory.
if(ppValue != NULL)
ldap_value_free(ppValue);
ppValue = NULL;
ldap_memfree(pAttribute);
// Get next attribute name.
pAttribute = ldap_next_attribute(
pLdapConnection, // Session Handle
pEntry, // Current entry
pBer); // Current BerElement
qDebug("\n");
}
// if( pBer != NULL )
// ber_bvfree(pBer);
pBer = NULL;
}
// Normal cleanup and exit.
ldap_unbind(pLdapConnection);
return ;
// On error cleanup and exit.
*/
}
MainWindow::~MainWindow()
{
delete ui;
}