Thanks Timovjl! I can use the functions to get the AD user properties now. This is the source code for reference:
#include <wchar.h>
#include <stdio.h>
#include <windows.h>
#include <iads.h>
#include <adshlp.h>
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")
#pragma comment(lib, "Activeds.lib")
const IID iadsuser={0x3e37e320,0x17e2,0x11cf,{0xab,0xc4,0x02,0x60,0x8c,0x9e,0x75,0x53}};
int main(int argc,char *argv[])
{
IADsUser *pObject;
HRESULT hr,hr1;
BSTR bstr;
VARIANT var;
// Initialize COM.
CoInitialize(NULL);
hr = ADsGetObject(L"LDAP://CN=Full Name,OU=XX,OU=YY,DC=ABC,DC=COM",&iadsuser,(void**)&pObject);
//hr = ADsGetObject(L"WinNT://ABC.COM/Shortname,user",&iadsuser,(void**)&pObject);
if(hr==S_OK)
{
// Use the object.
printf("Success -> ADsGetObject()\n");
char text[100];
memset(text,0,sizeof(text));
hr1=pObject->lpVtbl->get_FullName(pObject,&bstr);
if(hr1==S_OK){
printf("Success -> get_FullName()\n");
wcstombs(text,bstr,wcslen((wchar_t *)bstr));
printf("%s\n",text);
SysFreeString(bstr);
}
else printf("Fail code=%lx -> get_FullName()\n",hr1);
memset(text,0,sizeof(text));
hr1=pObject->lpVtbl->get_EmailAddress(pObject,&bstr);
if(hr1==S_OK){
printf("Success -> get_EmailAddress()\n");
wcstombs(text,bstr,wcslen((wchar_t *)bstr));
printf("%s\n",text);
SysFreeString(bstr);
}
else printf("Fail code=%lx -> get_EmailAddress()\n",hr1);
memset(text,0,sizeof(text));
hr1=pObject->lpVtbl->get_TelephonePager(pObject,&var);
if(hr1==S_OK){
printf("Success -> get_TelephonePager()\n");
wcstombs(text,V_BSTR(&var),wcslen((wchar_t *)V_BSTR(&var)));
printf("%s\n",text);
VariantClear(&var);
}
else printf("Fail code=%lx -> get_TelephonePager()\n",hr1);
// Release the object.
// pObject->Release();
pObject->lpVtbl->Release(pObject);
}
else printf("Fail code=%lx -> Release()\n",hr);
// Uninitialize COM.
CoUninitialize();
return 0;
}
For the ADsGetObject(), if I use WinNT format string, then many properties can't be got (this is mentioned in Win SDK document); if I use LDAP format string, I have to use an 'exact' path and full name string to get the object. Then I have further questions now:
1) If I only know the AD user's login name (shortname), I don't know how to find it out or convert it to the full LDAP string;
2) I don't know how to enum or set filter to search a mass of AD users, although I know it's possible technically.