home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / ADSDK.ZIP / Samples / ActiveDir / WKGUID / vc / wkguid.cpp < prev   
Encoding:
C/C++ Source or Header  |  1999-02-09  |  3.7 KB  |  137 lines

  1. // wkguid.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <wchar.h>
  5. #include <objbase.h>
  6.  
  7. //For ADSI
  8. #include <activeds.h>
  9.  
  10. //Make sure you define UNICODE
  11. //Need to define version 5 for Windows 2000
  12. #define _WIN32_WINNT 0x0500
  13. #include <ntdsapi.h>
  14.  
  15. HRESULT GetWKDomainObject(LPOLESTR szBindableWKGUID, //IN. Bindable string GUID of well-known object.
  16.                           IADs **ppObject //OUT. Return a pointer to the specified well-known object.
  17.                           );
  18.  
  19.  
  20. void wmain( int argc, wchar_t *argv[ ])
  21. {
  22. wprintf(L"This program finds the User's container in the current Window 2000 domain\n");
  23. wprintf(L"It uses the WKGUID binding string format.\n");
  24. //Intialize COM
  25. CoInitialize(NULL);
  26. HRESULT hr = S_OK;
  27. //Get rootDSE and the domain container's DN.
  28. IADs *pObject = NULL;
  29. hr = GetWKDomainObject(GUID_USERS_CONTAINER_W, //IN. Bindable string GUID of Users container.
  30.                           &pObject //OUT. Return a pointer to the specified well-known object.
  31.                           );
  32. if (FAILED(hr))
  33. {
  34.    wprintf(L"Not Found. Could not bind to the User container.\n");
  35.    if (pObject)
  36.      pObject->Release();
  37.    return;
  38. }
  39.  
  40. BSTR bstr;
  41. pObject->get_ADsPath(&bstr);
  42. wprintf (L"\nADsPath of Users Container (using WKGUID binding format):\n%s\n", bstr);
  43. FreeADsStr(bstr);
  44.  
  45. hr = pObject->get_Name(&bstr);
  46. if (SUCCEEDED(hr))
  47. {
  48.     wprintf (L"\nName of Users Container (using WKGUID binding format):\n%s\n", bstr);
  49.     FreeADsStr(bstr);
  50. }
  51.  
  52.  
  53. VARIANT var;
  54. pObject->Get(L"distinguishedName",&var);
  55. wprintf (L"\nName of Users Container: %s\n", var.bstrVal);
  56. VariantClear(&var);
  57.  
  58. if (pObject)
  59.     pObject->Release();
  60.  
  61.  
  62. //Uninitalize COM
  63. CoUninitialize();
  64. return;
  65.  
  66. }
  67.  
  68. // This function gets the specified well-known object for the current user's domain.
  69. /* Note that the following well-known GUIDs are defined for domainDNS in ntdsapi.h:
  70. #define GUID_USERS_CONTAINER_W              L"a9d1ca15768811d1aded00c04fd8d5cd"
  71. #define GUID_COMPUTRS_CONTAINER_W           L"aa312825768811d1aded00c04fd8d5cd"
  72. #define GUID_SYSTEMS_CONTAINER_W            L"ab1d30f3768811d1aded00c04fd8d5cd"
  73. #define GUID_DOMAIN_CONTROLLERS_CONTAINER_W L"a361b2ffffd211d1aa4b00c04fd7d83a"
  74. #define GUID_INFRASTRUCTURE_CONTAINER_W     L"2fbac1870ade11d297c400c04fd8d5cd"
  75. #define GUID_DELETED_OBJECTS_CONTAINER_W    L"18e2ea80684f11d2b9aa00c04f79f805"
  76. #define GUID_LOSTANDFOUND_CONTAINER_W       L"ab8153b7768811d1aded00c04fd8d5cd"
  77. */
  78.  
  79. HRESULT GetWKDomainObject(LPOLESTR szBindableWKGUID, //IN. Bindable string GUID of well-known object.
  80.                           IADs **ppObject //OUT. Return a pointer to the specified well-known object.
  81.                           )
  82. {
  83. HRESULT hr = E_FAIL;
  84. //Get rootDSE and the domain container's DN.
  85. IADs *pObject = NULL;
  86. LPOLESTR szPath = new OLECHAR[MAX_PATH];
  87. VARIANT var;
  88. hr = ADsOpenObject(L"LDAP://rootDSE",
  89.                  NULL,
  90.                  NULL,
  91.                  ADS_SECURE_AUTHENTICATION, //Use Secure Authentication
  92.                  IID_IADs,
  93.                  (void**)&pObject);
  94.  
  95. //Get current domain DN.
  96. if (SUCCEEDED(hr))
  97. {
  98.     hr = pObject->Get(L"defaultNamingContext",&var);
  99.     if (SUCCEEDED(hr))
  100.     {
  101.         //Build the WKGUID binding string.
  102.         wcscpy(szPath,L"LDAP://");
  103.         wcscat(szPath,L"<WKGUID=");
  104.         wcscat(szPath,szBindableWKGUID);
  105.         wcscat(szPath,L",");
  106.         wcscat(szPath,var.bstrVal);
  107.         wcscat(szPath,L">");
  108.         //Print the binding string.
  109.         //wprintf(L"WKGUID binding string: %s\n",szPath);
  110.         VariantClear(&var);
  111.         //Bind to the well-known object.
  112.         hr = ADsOpenObject(szPath,
  113.                          NULL,
  114.                          NULL,
  115.                          ADS_SECURE_AUTHENTICATION, //Use Secure Authentication
  116.                          IID_IADs,
  117.                          (void**)ppObject);
  118.         if (FAILED(hr))
  119.         {
  120.             if (*ppObject)
  121.             {
  122.               (*ppObject)->Release();
  123.               (*ppObject) = NULL;
  124.             }
  125.         }
  126.     }
  127. }
  128. if (pObject)
  129.   pObject->Release();
  130.  
  131. return hr;
  132. }
  133.     
  134.  
  135.                           
  136.  
  137.