I'm trying to code a C++ program to print the distinguishedName of an user in active directory. The program compiles without errors, however instantly crashes on start up. I admit that I don't know c++ very well, and I believe that the problem is somewhere there.
#include <iostream>
#include <jni.h>
#include<string.h>
#include "com_library_ldap_JNIHelper.h"
#include <iads.h>
#include <adshlp.h>
#include "activeds.h"
#include <windows.h>
#include <stdlib.h>
#include <atlbase.h>
using namespace std;
int main()
{
string username = "Admin10";
string password = "password#10";
wchar_t wName[20];
mbstowcs(wName, username.c_str(), username.length());
wchar_t wpwd[20];
mbstowcs(wpwd, password.c_str(), password.length());
//HRESULT hr = S_OK; // COM result variable
HRESULT hr;
ADS_SEARCH_COLUMN col; // COL for iterations
LPWSTR szUsername = wName; // user name
LPWSTR szPassword = wpwd; // password
// Interface Pointers.
IDirectorySearch* pDSSearch=NULL;
// Initialize COM.
//CoInitialize(0);
::CoInitialize(NULL);
// Add code to securely retrieve the user name and password or
// leave both as NULL to use the default security context.
// Open a connection with server.
hr = ADsOpenObject(L"LDAP://10.0.14.74/OU=Vimal,DC=test,DC=local",
szUsername,
szPassword,
ADS_SECURE_AUTHENTICATION,
IID_IDirectorySearch,
(void**)&pDSSearch);
string des = "description";
string name = "Name";
string dn = "distinguishedname";
string propertycheck = "(&(objectClass=user)(cn=Admin10))";
wchar_t wdes[20];
mbstowcs(wdes, des.c_str(), des.length());
LPWSTR szDes = wdes;
wchar_t wname[20];
mbstowcs(wname, name.c_str(), name.length());
LPWSTR szName = wname;
wchar_t wdn[20];
mbstowcs(wdn, dn.c_str(), dn.length());
LPWSTR szDN = wdn;
wchar_t wpc[20];
mbstowcs(wpc, propertycheck.c_str(), propertycheck.length());
LPWSTR szPC = wpc;
LPWSTR pszAttr[] = { szDes, szName, szDN };
ADS_SEARCH_HANDLE hSearch;
DWORD dwCount = 0;
DWORD dwAttrNameSize = sizeof(pszAttr) / sizeof(LPWSTR);
// Search for all objects with the 'cn' property that start with c.
hr = pDSSearch->ExecuteSearch(szPC, pszAttr, dwAttrNameSize, &hSearch);
LPWSTR pszColumn;
while (pDSSearch->GetNextRow(hSearch) != S_ADS_NOMORE_ROWS)
{
// Get the property.
hr = pDSSearch->GetColumn(hSearch, szDN, &col); // want user distinguishedname
// If this object supports this attribute, display it.
if (SUCCEEDED(hr))
{
if (col.dwADsType == ADSTYPE_CASE_IGNORE_STRING)
wprintf(L"The description property:%s\r\n", col.pADsValues->CaseIgnoreString);
pDSSearch->FreeColumn(&col);
}
else
puts("description property NOT available");
puts("------------------------------------------------");
dwCount++;
}
pDSSearch->CloseSearchHandle(hSearch);
pDSSearch->Release();
CoUninitialize();
return 0;
}
Below I add the screenshot of an error displaying in visual studio: enter image description here
Exception thrown: read access violation. pDSSearch was nullptr.
The above is the error message I got, but I have no idea how to trace it. Thanks in advance.
Now I find the solution to my problem. I would changed the datatype of username and password variable which is passed to ADsOpenObject function and some more changes... Below I add the complete code for the solution.
}