Exception thrown: read access violation. **pDSSearch** was nullptr

146 views Asked by At

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.

1

There are 1 answers

2
Vimal A On

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.

#include <iostream>
#include <jni.h>
#include<string.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 = "username";
string password = "password";
string resDN="";

std::wstring stemp = std::wstring(username.begin(), username.end());
LPCWSTR lpszUserName = stemp.c_str();
std::wstring stemp1 = std::wstring(password.begin(), password.end());
LPCWSTR lpszPassword = stemp1.c_str();
ADS_SEARCH_COLUMN col;  // COL for iterations


HRESULT hr;
string uType;
string check;
bool res = false;
// Interface Pointers.
IDirectorySearch* pDSSearch = NULL;

::CoInitialize(NULL);
hr = ADsOpenObject(L"LDAP://10.0.124.714/OU=Vimal,DC=test,DC=local",
    lpszUserName,
    lpszPassword,
    ADS_SECURE_AUTHENTICATION,
    IID_IDirectorySearch,
    (void**)&pDSSearch);
std::cout << hr;
if (!SUCCEEDED(hr))
{
    std::cout << "Not working!!!";
    return 0;
}
if (SUCCEEDED(hr))
{
    wstring des = L"description";
    wstring name = L"Name";
    wstring dn = L"distinguishedname";
    wstring propertycheck = L"(&(objectClass=user)(cn=Vimaluser1))";

    std::wstring sdes = std::wstring(des.begin(), des.end());
    LPCWSTR lpszDes = sdes.c_str();
    std::wstring sname = std::wstring(name.begin(), name.end());
    LPCWSTR lpszName = sname.c_str();
    std::wstring sdn = std::wstring(dn.begin(), dn.end());
    LPCWSTR lpszDN = sdn.c_str();
    std::wstring sprop = std::wstring(propertycheck.begin(), propertycheck.end());
    LPCWSTR lpszProp = sprop.c_str();

    LPCWSTR pszAttr[] = { lpszDes, lpszName, lpszDN };
    ADS_SEARCH_HANDLE hSearch;
    DWORD dwCount = 0;
    DWORD dwAttrNameSize = sizeof(pszAttr) / sizeof(LPCWSTR);

    hr = pDSSearch->ExecuteSearch((LPWSTR)lpszProp, (LPWSTR*)pszAttr, dwAttrNameSize, &hSearch);
    LPCWSTR pszColumn;
    while (pDSSearch->GetNextRow(hSearch) != S_ADS_NOMORE_ROWS)
    {
        // Get the property.
        hr = pDSSearch->GetColumn(hSearch, (LPWSTR)lpszDN, &col); // want user distinguishedname

        // If this object supports this attribute, display it.
        if (SUCCEEDED(hr))
        {
            if (col.dwADsType == ADSTYPE_DN_STRING) {
                wprintf(L"The distinguishedName property:%s\r\n", col.pADsValues->DNString);
                //resDN += col.pADsValues->DNString;
            }
            else
                std::cout << "dn not found";
            pDSSearch->FreeColumn(&col);
        }
        else
            puts("distinguishedName property NOT available");
        dwCount++;
    }

    uType = "Success";
    res = true;

    pDSSearch->CloseSearchHandle(hSearch);
    pDSSearch->Release();
    CoUninitialize();
}


if (res == false)
    uType = "Login details not found...";

std::cout << uType;

return 0;

}