Article ID: 200126
Article Last Modified on 11/1/2006
/*++
ATL is used to simplify the COM interface usage
assert()'s are used instead of real error checking
for readability.
Build command line:<BR/>
cl main.cpp adsiid.lib activeds.lib
--*/
#define UNICODE
#define _UNICODE
#include <windows.h>
#include <activeds.h>
#include <atlbase.h>
#include <stdio.h>
#include <assert.h>
#include <atlimpl.cpp>
#define RTN_OK 0
#define RTN_USAGE 1
#define RTN_ERROR 13
extern "C" int wmain (int argc, wchar_t *argv[])
{
DWORD dwRes;
CComPtr<IADsUser> pUser;
CComPtr<IADs> pObject;
CComBSTR strDomainPath, strUser, strDomain;
CComBSTR strClass;
HRESULT hr;
// initialize COM
//
hr = CoInitialize (NULL);
assert (SUCCEEDED(hr));
// check command line
//
if (argc != 2) {
printf ("Usage: %S <ADS path to domain user>\n", argv[0]);
return (RTN_USAGE);
}
// get the user object
//
hr = ADsGetObject (
argv[1],
IID_IADsUser,
reinterpret_cast<void**>(&pUser)
);
assert (SUCCEEDED(hr));
// get the user name
//
hr = pUser->get_Name (&strUser);
assert (SUCCEEDED(hr));
// get the parent of the object
//
hr = pUser->get_Parent (&strDomainPath);
assert (SUCCEEDED (hr));
hr = ADsGetObject (
strDomainPath,
IID_IADs,
reinterpret_cast<void**>(&pObject)
);
assert (SUCCEEDED(hr));
// verify that it's a domain - this could have been done by
// getting an IADsDomain interface in the ADsGetObject call...
//
hr = pObject->get_Class (&strClass);
assert (SUCCEEDED(hr));
assert (!wcscmp (TEXT("Domain"), strClass));
// get the domain name
//
hr = pObject->get_Name (&strDomain);
assert (SUCCEEDED(hr));
printf ("Domain of user %S is %S\n", strUser, strDomain);
return (RTN_OK);
}
Keywords: kbhowto KB200126