DOCUMENT:Q200126 04-OCT-1999 [winnt]
TITLE :HOWTO: Determining the Domain of A User Through ADSI
PRODUCT :Microsoft Windows NT
PROD/VER:winnt:2.0,2.5,4.0
OPER/SYS:
KEYWORDS:kbADSI kbGrpDSPlatform kbDSupport
======================================================================
-------------------------------------------------------------------------------
The information in this article applies to:
- Microsoft Windows NT Workstation version 4.0
- Microsoft Active Directory Service Interfaces, versions 2.0, 2.5
-------------------------------------------------------------------------------
SUMMARY
=======
The Active Directory Services Interface (ADSI) is the preferred method for
working with Microsoft Windows NT user accounts. However, there may be times
when the Windows NT provider in ADSI does not highlight a method or property
needed for a specific task. In that case, determine the domain and name of the
user being referenced by an ADsPath, so that other calls can be made.
NOTE: This method only applies to Windows NT 3.0 or 4.0 domains.
MORE INFORMATION
================
To determine the domain of a user given its ADsPath complete the following
steps:
1. Get the user object.
2. Get the ADsPath of the object's parent.
3. Verify the parent's class is "Domain".
4. Get the name of the parent object.
Sample Code
-----------
/*++
ATL is used to simplify the COM interface usage
assert()'s are used instead of real error checking
for readability.
Build command line:
cl main.cpp adsiid.lib activeds.lib
--*/
#define UNICODE
#define _UNICODE
#include
#include
#include
#include
#include
#include
#define RTN_OK 0
#define RTN_USAGE 1
#define RTN_ERROR 13
extern "C" int wmain (int argc, wchar_t *argv[])
{
DWORD dwRes;
CComPtr pUser;
CComPtr 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 \n", argv[0]);
return (RTN_USAGE);
}
// get the user object
//
hr = ADsGetObject (
argv[1],
IID_IADsUser,
reinterpret_cast(&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(&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);
}
Additional query words:
======================================================================
Keywords : kbADSI kbGrpDSPlatform kbDSupport
Technology : kbWinNTsearch kbWinNTWsearch kbWinNTW400 kbWinNTW400search kbWinNT400xsearch kbAudDeveloper kbADSISearch kbADSI200 kbADSI250
Version : winnt:2.0,2.5,4.0
Issue type : kbhowto
=============================================================================
THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS
PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS
ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO
EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR
ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL,
CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF
MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION
OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES
SO THE FOREGOING LIMITATION MAY NOT APPLY.
Copyright Microsoft Corporation 1999.