4

How do I set up the incoming parameter pUser to pass into this function CheckUserGroups(IADsUser *pUser)? I saw function on another post but they did not explain it in detail: Need to retrieve all groups a user belongs to... in C++

Please advise.

HRESULT CheckUserGroups(IADsUser *pUser)
{
  IADsMembers *pGroups;
  HRESULT hr = S_OK;

  hr = pUser->Groups(&pGroups);
  pUser->Release();
  if (FAILED(hr)) return hr;

  IUnknown *pUnk;
  hr = pGroups->get__NewEnum(&pUnk);
  if (FAILED(hr)) return hr;
  pGroups->Release();

  IEnumVARIANT *pEnum;
  hr = pUnk->QueryInterface(IID_IEnumVARIANT,(void**)&pEnum);
  if (FAILED(hr)) return hr;

  pUnk->Release();

  // Enumerate.
  BSTR bstr;
  VARIANT var;
  IADs *pADs;
  ULONG lFetch;
  IDispatch *pDisp;

  VariantInit(&var);
  hr = pEnum->Next(1, &var, &lFetch);
  while(hr == S_OK)
  {
    if (lFetch == 1)
    {
      pDisp = V_DISPATCH(&var);
      pDisp->QueryInterface(IID_IADs, (void**)&pADs);
      pADs->get_Name(&bstr);
      printf("Group belonged: %S\n",bstr);
      SysFreeString(bstr);
      pADs->Release();
    }
    VariantClear(&var);
    pDisp=NULL;
    hr = pEnum->Next(1, &var, &lFetch);
  };
  hr = pEnum->Release();
  return S_OK;
}
Peter Ruderman
  • 12,241
  • 1
  • 36
  • 58

1 Answers1

5

Here is an exmaple to create and set up IADsUser *pUser(change "Administrator" to your username):

    HRESULT hr = S_OK;
    IADsUser *pUser;
    _bstr_t bstr;
    DWORD ll_len = 255;
    char lbBuffer[255];
    ::GetComputerName(lbBuffer, &ll_len);

    bstr = "WinNT://" + _bstr_t(lbBuffer) + "/" + _bstr_t("Administrator") + ",user";

    hr = CoInitialize(NULL);
    hr = ADsGetObject(bstr, IID_IADsUser, (void **)&pUser);

    if (SUCCEEDED(hr))
    {
        hr = CheckUserGroups(pUser);
        /*
        hr = pUser->SetPassword(_bstr_t("123456"));

        if (SUCCEEDED(hr))
        {
            CoUninitialize();
            return TRUE;
        }
        */
    }
    CoUninitialize();

For LDAP, you may get help from the example in this document, change the first parameter of ADsGetObject.

Drake Wu
  • 6,927
  • 1
  • 7
  • 30
  • Thank you for your comment. Will your example only work for Local users? How would I do it for LDAP? – DestryGleason Nov 20 '18 at 20:13
  • @DestryGleason You may get help from the example in [this document](https://learn.microsoft.com/zh-cn/windows/desktop/ADSI/binding-with-getobject-and-adsgetobject) – Drake Wu Nov 21 '18 at 01:43