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

72 Views Asked by At

I want to check whether a current user is belongs to a particular group in active directory or not using c++ code. I'm using ADSI documentation code, below I add the code which i'm getting an error. Unhandled exception thrown: read access violation. pGroup was nullptr. This is the error i'm getting. Can anyone please help me to solve this error. Thanks in Advance.

//#include "stdafx.h"
#include <iostream>
#include <string>
#include <Windows.h>
#include <WinBase.h>
#include <Iads.h>
#include <Adshlp.h>
#include <atlbase.h>


int main()
{
IADsGroup* pGroup = NULL;
HRESULT hr = S_OK;

std::string path = "WinNT://10.0.14.74/Administrators";
std::wstring spath = std::wstring(path.begin(), path.end());
LPCWSTR lpszPath = spath.c_str();

LPCWSTR adsPath = lpszPath;
BSTR bstr = NULL;
::CoInitialize(NULL);
hr = ADsGetObject(adsPath, IID_IADs, (void**)&pGroup);
//std::cout << hr;
if (FAILED(hr))
{
    std::cout << hr;
}

hr = pGroup->get_Description(&bstr);
if (FAILED(hr))
{
    std::cout << hr;
}

printf("Description: %S\n", bstr);
SysFreeString(bstr);

VARIANT_BOOL inG = false;
hr = pGroup->IsMember(CComBSTR("WinNT://Microsoft/SecUser"), &inG);

if (inG)
{
    printf("already in the group.\n");
}
else
{
    printf("User not in that group.\n");
}
CoUninitialize();

return 0;
}

In this line hr = pGroup->IsMember(CComBSTR("WinNT://Microsoft/SecUser"), &inG); I don't know what this Microsoft/SecUser means? Can anyone please help me to solve this error. Thanks in Advance.

0

There are 0 best solutions below