home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / ADSDK.ZIP / Samples / ActiveDir / GetDomainMode / vc / getdomainmode.cpp next >
Encoding:
C/C++ Source or Header  |  1999-04-05  |  5.5 KB  |  222 lines

  1. #include <wchar.h>
  2. #include <activeds.h>
  3.  
  4. HRESULT CheckDomainModeOfObject(IDirectoryObject *pDirObject, BOOL *bIsMixed);
  5. WCHAR * GetDirectoryObjectAttrib(IDirectoryObject *pDirObject,LPWSTR pAttrName);
  6.  
  7. HRESULT GetDomainMode(IADs *pDomain, BOOL *bIsMixed);
  8.  
  9.  
  10. int main(int argc, char* argv[])
  11. {
  12.  
  13. wprintf(L"This program checks whether the current domain is in mixed or native mode.\n");
  14.  
  15. //Intialize COM
  16. CoInitialize(NULL);
  17. HRESULT hr = S_OK;
  18. //Get rootDSE and the domain container's DN.
  19. IADs *pObject = NULL;
  20. LPOLESTR szPath = new OLECHAR[MAX_PATH];
  21. VARIANT var;
  22. BOOL bIsMixed;
  23.  
  24. hr = ADsOpenObject(L"LDAP://rootDSE",
  25.                  NULL,
  26.                  NULL,
  27.                  ADS_SECURE_AUTHENTICATION, //Use Secure Authentication
  28.                  IID_IADs,
  29.                  (void**)&pObject);
  30. if (FAILED(hr))
  31. {
  32.    wprintf(L"Not Found. Could not bind to the domain.\n");
  33.    if (pObject)
  34.      pObject->Release();
  35.    return TRUE;
  36. }
  37.  
  38. hr = pObject->Get(L"defaultNamingContext",&var);
  39. if (SUCCEEDED(hr))
  40. {
  41.     wcscpy(szPath,L"LDAP://"); //For NT 4.0 and Win 9.x, you must add the server name, e.g LDAP://myServer
  42.     wcscat(szPath,var.bstrVal);
  43.     VariantClear(&var);
  44.     if (pObject)
  45.     {
  46.        pObject->Release();
  47.        pObject = NULL;
  48.     }
  49.     //Bind to the root of the current domain.
  50.     hr = ADsOpenObject(szPath,
  51.                      NULL,
  52.                      NULL,
  53.                      ADS_SECURE_AUTHENTICATION, //Use Secure Authentication
  54.                      IID_IADs,
  55.                      (void**)&pObject);
  56.     if (SUCCEEDED(hr))
  57.     {
  58.         hr = GetDomainMode(pObject, &bIsMixed);
  59.         if (SUCCEEDED(hr))
  60.         {
  61.             hr = pObject->Get(L"name",&var);
  62.             if (bIsMixed)
  63.               wprintf(L"Current domain %s is in mixed mode\n", var.bstrVal);
  64.             else
  65.               wprintf(L"Current domain %s is in native mode\n", var.bstrVal);
  66.         }
  67.         else
  68.             wprintf(L"GetDomainMode failed with hr: %x",hr);
  69.     }
  70.     else
  71.         wprintf(L"Bind to domain failed with hr: %x",hr);
  72. }
  73. VariantClear(&var);
  74. if (pObject)
  75.   pObject->Release();
  76.  
  77. return TRUE;
  78. }
  79.         
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87. HRESULT GetDomainMode(IADs *pDomain, BOOL *bIsMixed)
  88. {
  89. HRESULT hr = E_FAIL;
  90. VARIANT var;
  91. if (pDomain)
  92. {
  93.     VariantClear(&var);
  94.     //Get the ntMixedDomain attribute
  95.     LPOLESTR szAttribute = L"ntMixedDomain";
  96.     hr = pDomain->Get(szAttribute,&var);
  97.     if (SUCCEEDED(hr))
  98.     {
  99.         //Type should be VT_I4.
  100.         if (var.vt==VT_I4)
  101.         {
  102.             //Zero means native mode.
  103.             if (var.lVal == 0)
  104.                 *bIsMixed = FALSE;
  105.             //One means mixed mode.
  106.             else if (var.lVal == 1)
  107.                 *bIsMixed = TRUE;
  108.             else
  109.                 hr=E_FAIL;
  110.         }
  111.     }
  112.     VariantClear(&var);
  113. }
  114. return hr;
  115.  
  116. }    
  117.  
  118. //Given an object, this function finds out if the domain containing the object is in mixed mode.
  119.  
  120. HRESULT CheckDomainModeOfObject(IDirectoryObject *pDirObject, BOOL *bIsMixed)
  121. {
  122.     HRESULT hr = E_FAIL;
  123.     IADs *pDomain = NULL;
  124.     VARIANT VarTest;
  125.     WCHAR *pFound = NULL;
  126.     int iLen;
  127.     WCHAR *pDomainPath = new WCHAR[MAX_PATH*2];
  128.  
  129.     //Check whether the domain containing the container is in mixed mode
  130.     WCHAR *pVal = NULL;
  131.     pVal = GetDirectoryObjectAttrib(pDirObject,L"canonicalName");
  132.  
  133.     if (pVal)
  134.     {
  135.         //Parse the canonical name for the DNS name of the domain
  136.         pFound = wcschr(pVal,'/');
  137.         //Bind to the domain using the dns name, get defaultnamingcontext, 
  138.         if (pFound)
  139.         {
  140.             iLen = pFound - pVal;
  141.             wcscpy(pDomainPath, L"LDAP://");
  142.             wcsncat(pDomainPath, pVal,iLen);
  143.             wcscat(pDomainPath, L"/rootDSE");
  144.             wprintf(L"DNS Name: %s\n", pDomainPath);
  145.             VariantClear(&VarTest);
  146.             hr = ADsOpenObject(pDomainPath,
  147.                             NULL,
  148.                             NULL,
  149.                             ADS_SECURE_AUTHENTICATION, //Use Secure Authentication
  150.                             IID_IADs,
  151.                             (void**)&pDomain);
  152.             if (SUCCEEDED(hr))
  153.             {
  154.                 hr = pDomain->Get(L"defaultNamingContext",&VarTest);
  155.                 if (SUCCEEDED(hr))
  156.                 {
  157.                     wcscpy(pDomainPath, L"LDAP://");
  158.                      wcsncat(pDomainPath, pVal,iLen);
  159.                     wcscat(pDomainPath, L"/");
  160.                     wcscat(pDomainPath, VarTest.bstrVal);
  161.                     VariantClear(&VarTest);
  162.                     if (pDomain)
  163.                         pDomain->Release();
  164.                     if (SUCCEEDED(hr))
  165.                     {
  166.                         hr = ADsOpenObject(pDomainPath,
  167.                                         NULL,
  168.                                         NULL,
  169.                                         ADS_SECURE_AUTHENTICATION, //Use Secure Authentication
  170.                                         IID_IADs,
  171.                                         (void**)&pDomain);
  172.                         if (SUCCEEDED(hr))
  173.                         {
  174.                             hr = GetDomainMode(pDomain, bIsMixed);
  175.                         }
  176.                     }
  177.                 }
  178.             }
  179.             if (pDomain)
  180.                 pDomain->Release();
  181.         }
  182.     }
  183.     return hr;
  184. }
  185.  
  186. ////////////////////////////////////////////////////////////////////////////////////////////////////
  187. /*  
  188.     GetDirectoryObjectAttrib()      -   Returns the value of the attribute named in pAttrName
  189.                                         from the IDirectoryObject passed
  190.     Parameters
  191.     
  192.         IDirectoryObject *pDirObject    - Object from which to retrieve an attribute value
  193.         LPWSTR pAttrName                - Name of attribute to retrieve
  194. */
  195. WCHAR * GetDirectoryObjectAttrib(IDirectoryObject *pDirObject,LPWSTR pAttrName)
  196. {
  197.     HRESULT   hr;
  198.     ADS_ATTR_INFO   *pAttrInfo=NULL;
  199.     DWORD   dwReturn;
  200.     static WCHAR pwReturn[1024];
  201.  
  202.     pwReturn[0] = 0l;
  203.  
  204.     hr = pDirObject->GetObjectAttributes( &pAttrName, 
  205.                                             1, 
  206.                                             &pAttrInfo, 
  207.                                             &dwReturn ); 
  208.     if ( SUCCEEDED(hr) )
  209.     {
  210.         for(DWORD idx=0; idx < dwReturn;idx++, pAttrInfo++ )   
  211.         {
  212.             if ( _wcsicmp(pAttrInfo->pszAttrName,pAttrName) == 0 )       
  213.             {
  214.                 wcscpy(pwReturn,pAttrInfo->pADsValues->CaseIgnoreString);
  215.                 break;
  216.             }
  217.         }
  218.         FreeADsMem( pAttrInfo );
  219.     }    
  220.     return pwReturn;
  221. }
  222.