I'm trying to get info about users in Active Directory using C++. In particular, I'd like to retrieve the EmployeeID property, but it doesn't seem to work. I know for sure one user has a value for the EmployeeID.

If I look for FullName or LastLogin, I get the values, so I know it's probably not permissions or network connectivity issue. My server is running Windows Server 2003 and client is XP logged in as admin.

Here is the code:

#include <afxwin.h>
#include <iostream>

#include <comdef.h>
#include <Iads.h>
#include <Adshlp.h>

int main()
{
  HRESULT hr;
  IADsContainer *pCont = NULL;
  
  CoInitialize(NULL);

  hr = ADsGetObject(L"WinNT://Testhost.local/testsvr", IID_IADsContainer, (void**) &pCont );
  if (!SUCCEEDED(hr))
    return hr;

  _variant_t var;
  IEnumVARIANTPtr pEnum;
  ADsBuildEnumerator (pCont,&pEnum);
  int cnt=0;
  ULONG ulFetched = 0L;

  _variant_t vChild;
  while ((SUCCEEDED(ADsEnumerateNext(pEnum, 1, &vChild, &ulFetched)) && ulFetched==1)) {
    IADsUser* pADs;
    hr = V_DISPATCH(&vChild)->QueryInterface(IID_IADsUser, (void**)&pADs);
    if (hr!=S_OK)
      break;

    VARIANT varEmpID;
    BSTR attribName = SysAllocString(L"EmployeeID");
    HRESULT successVar = pADs->Get(attribName, &varEmpID);
    SysFreeString(attribName);
    if (SUCCEEDED(successVar))
      int test = 100;

    BSTR bstrName;
    pADs->get_Name(&bstrName);
    CString csName ( bstrName==NULL ? L"" : bstrName );
    SysFreeString(bstrName);

    printf("%s\n",csName);

    VariantClear(&varEmpID);

    pADs->Release();
    pADs = NULL;
  }

  // Cleanup
  if (pCont)
    pCont->Release();
  CoUninitialize();

  return 0;
}

I tried getting the info with GetInfoEx, but that didn't seem to work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.