home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / ADSDK.ZIP / Samples / ActiveDir / Attributes / vc / attributes.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-04-05  |  13.2 KB  |  397 lines

  1. // attributes.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <wchar.h>
  5. #include <activeds.h>
  6. #ifndef ADS_SYSTEMFLAG_ATTR_NOT_REPLICATED
  7. #define ADS_SYSTEMFLAG_ATTR_NOT_REPLICATED         (0x00000001) // Non-replicated attribute
  8. #endif
  9.  
  10. #ifndef ADS_SYSTEMFLAG_ATTR_IS_CONSTRUCTED
  11. #define ADS_SYSTEMFLAG_ATTR_IS_CONSTRUCTED         (0x00000004) // Attribute is a constructed att
  12. #endif
  13.  
  14. HRESULT FindAttributesByType(IDirectorySearch *pSchemaNC, //IDirectorySearch pointer to schema naming context.
  15.          DWORD dwAttributeType, //Bit flags to search for in systemFlags
  16.          LPOLESTR pszAttributerNameType, //ldapDisplayName of the naming attribute you want to display for each returned attribute.
  17.                                          //NULL returns common name.
  18.          BOOL bIsExactMatch //TRUE to find attributes that have systemFlags exactly matching dwAttributeType
  19.                             //FALSE to find attributes that have the dwAttributeType bit set (and possibly others).
  20.         );
  21.  
  22. HRESULT FindIndexedAttributes(IDirectorySearch *pSchemaNC, //IDirectorySearch pointer to schema naming context.
  23.          LPOLESTR pszAttributeNameType, //ldapDisplayName of the naming attribute you want to display for each returned attribute.
  24.                                          //NULL returns common name.
  25.          BOOL bIsIndexed //TRUE to find indexed attributes.
  26.                          //FALSE to find non-indexed attributes.
  27.         );
  28.  
  29. HRESULT FindGCAttributes(IDirectorySearch *pSchemaNC, //IDirectorySearch pointer to schema naming context.
  30.          LPOLESTR pszAttributeNameType, //ldapDisplayName of the naming attribute you want to display for each returned attribute.
  31.                                          //NULL returns common name.
  32.          BOOL bInGC //TRUE to find indexed attributes.
  33.                          //FALSE to find non-indexed attributes.
  34.         );
  35.  
  36. int main(int argc, char* argv[])
  37. {
  38. LPOLESTR szPath = new OLECHAR[MAX_PATH];
  39. HRESULT hr = S_OK;
  40. IADs *pObject = NULL;
  41. IDirectorySearch *pSchemaNC = NULL;
  42. VARIANT var;
  43.  
  44. //Initialize COM
  45. CoInitialize(NULL);
  46.  
  47. wprintf(L"This program displays the following types of attributes in the schema:\n");
  48. wprintf(L"Non-Replicated, Indexed, Constructed, Global Catalog\n\n");
  49.  
  50. wcscpy(szPath, L"LDAP://");
  51. //Get rootDSE and the schema container's DN.
  52. //Bind to current user's domain using current user's security context.
  53. hr = ADsOpenObject(L"LDAP://rootDSE", //NOTE: If you're running NT 4.0, Win9x, you must add 
  54.                                       // the server name, e.g LDAP://myServer/rootDSE
  55.                  NULL,
  56.                  NULL,
  57.                  ADS_SECURE_AUTHENTICATION, //Use Secure Authentication
  58.                  IID_IADs,
  59.                  (void**)&pObject);
  60.  
  61. if (SUCCEEDED(hr))
  62. {
  63.     hr = pObject->Get(L"schemaNamingContext",&var);
  64.     if (SUCCEEDED(hr))
  65.     {
  66.         wcscpy(szPath,L"LDAP://");
  67.         wcscat(szPath,var.bstrVal);
  68.         hr = ADsOpenObject(szPath,
  69.                          NULL,
  70.                          NULL,
  71.                          ADS_SECURE_AUTHENTICATION, //Use Secure Authentication
  72.                          IID_IDirectorySearch,
  73.                          (void**)&pSchemaNC);
  74.  
  75.         if (SUCCEEDED(hr))
  76.         {
  77.             //Find non-replicated attributes
  78.             wprintf (L"----------------------------------------------\n");
  79.             wprintf(L"Non-Replicated attributes (stored on each domain controller but are not replicated elsewhere)\n");
  80.             wprintf(L"Find non-replicated attributes\n");
  81.             hr = FindAttributesByType(pSchemaNC, //IDirectorySearch pointer to schema naming context.
  82.                  ADS_SYSTEMFLAG_ATTR_NOT_REPLICATED, //Bit flags to search for in systemFlags
  83.                  L"ldapDisplayName",
  84.                  TRUE
  85.                  );
  86.  
  87.             //Find attributes included in the global catalog
  88.             wprintf (L"----------------------------------------------\n");
  89.             wprintf(L"Global Catalog attributes (replicated to the Global Catalog)\n");
  90.             wprintf(L"Find attributes included in the global catalog\n");
  91.             hr = FindGCAttributes(pSchemaNC,
  92.                  L"ldapDisplayName",
  93.                  TRUE
  94.                  );
  95.  
  96.             //Find constructed attributes
  97.             wprintf (L"----------------------------------------------\n");
  98.             wprintf(L"Constructed attributes (not stored in the directory but are calculated by the domain controller)\n");
  99.             wprintf(L"Find constructed attributes\n");
  100.             hr = FindAttributesByType(pSchemaNC, //IDirectorySearch pointer to schema naming context.
  101.                  ADS_SYSTEMFLAG_ATTR_IS_CONSTRUCTED, //Bit flags to search for in systemFlags
  102.                  L"ldapDisplayName",
  103.                  FALSE
  104.                  );
  105.  
  106.  
  107.             //Find indexed attributes
  108.             wprintf (L"----------------------------------------------\n");
  109.             wprintf(L"Indexed attributes (indexed for efficient search)\n");
  110.             wprintf(L"Find indexed attributes\n");
  111.             hr = FindIndexedAttributes(pSchemaNC, //IDirectorySearch pointer to schema naming context.
  112.                  L"ldapDisplayName",
  113.                  TRUE
  114.                 );
  115.  
  116.         }
  117.         if (pSchemaNC)
  118.            pSchemaNC->Release();
  119.     }
  120.     VariantClear(&var);
  121. }
  122. if (pObject)
  123.     pObject->Release();
  124.  
  125. // Uninitialize COM
  126. CoUninitialize();
  127. return 0;
  128. }
  129.  
  130. HRESULT FindAttributesByType(IDirectorySearch *pSchemaNC, //IDirectorySearch pointer to schema naming context.
  131.          DWORD dwAttributeType, //Bit flags to search for in systemFlags
  132.          LPOLESTR pszAttributeNameType, //ldapDisplayName of the naming attribute you want to display for each returned attribute.
  133.                                          //NULL returns common name.
  134.          BOOL bIsExactMatch //TRUE to find attributes that have systemFlags exactly matching dwAttributeType
  135.                             //FALSE to find attributes that have the dwAttributeType bit set (and possibly others).
  136.         )
  137. {
  138.     //Create search filter
  139.     LPOLESTR pszSearchFilter = new OLECHAR[MAX_PATH*2];
  140.     if (bIsExactMatch)
  141.        //Find attributes with systemFlags that exactly match dwAttributeType
  142.        wsprintf(pszSearchFilter, L"(&(objectCategory=attributeSchema)(systemFlags=%d))",dwAttributeType);
  143.     else
  144.        //Find attributes with systemFlags that contain dwAttributeType
  145.         wsprintf(pszSearchFilter, L"(&(objectCategory=attributeSchema)(systemFlags:1.2.840.113556.1.4.804:=%d))",dwAttributeType);
  146.  
  147.     //Attributes are one-level deep in the Schema container so only need to search one level.
  148.     ADS_SEARCHPREF_INFO SearchPrefs;
  149.     SearchPrefs.dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
  150.     SearchPrefs.vValue.dwType = ADSTYPE_INTEGER;
  151.     SearchPrefs.vValue.Integer = ADS_SCOPE_ONELEVEL;
  152.     DWORD dwNumPrefs = 1;
  153.  
  154.     // COL for iterations
  155.     ADS_SEARCH_COLUMN col;
  156.     HRESULT hr;
  157.     
  158.     // Interface Pointers
  159.     IADs    *pObj = NULL;
  160.     IADs    * pIADs = NULL;
  161.  
  162.     // Handle used for searching
  163.     ADS_SEARCH_HANDLE hSearch;
  164.     
  165.     // Set the search preference
  166.     hr = pSchemaNC->SetSearchPreference( &SearchPrefs, dwNumPrefs);
  167.     if (FAILED(hr))
  168.         return hr;
  169.  
  170.     CONST DWORD dwAttrNameSize = 1;
  171.     LPOLESTR pszAttribute[dwAttrNameSize];
  172.  
  173.     if (!pszAttributeNameType)
  174.         pszAttribute[0] = L"cn";
  175.     else
  176.         pszAttribute[0] = pszAttributeNameType;
  177.  
  178.  
  179.     // Execute the search
  180.     hr = pSchemaNC->ExecuteSearch(pszSearchFilter,
  181.                                   pszAttribute,
  182.                                   dwAttrNameSize,
  183.                                   &hSearch
  184.                                   );
  185.     if ( SUCCEEDED(hr) )
  186.     {    
  187.  
  188.     // Call IDirectorySearch::GetNextRow() to retrieve the next row 
  189.     //of data
  190.         while( pSchemaNC->GetNextRow( hSearch) != S_ADS_NOMORE_ROWS )
  191.         {
  192.  
  193.             // loop through the array of passed column names,
  194.             // print the data for each column
  195.             for (DWORD x = 0; x < dwAttrNameSize; x++)
  196.             {
  197.  
  198.                 // Get the data for this column
  199.                 hr = pSchemaNC->GetColumn( hSearch, pszAttribute[x], &col );
  200.  
  201.                 if ( SUCCEEDED(hr) )
  202.                 {
  203.  
  204.                     // Print the data for the column and free the column
  205.                     wprintf(L"%s: %s\r\n",pszAttribute[x],col.pADsValues->CaseIgnoreString); 
  206.                     pSchemaNC->FreeColumn( &col );
  207.                 }
  208.                 else
  209.                     wprintf(L"<%s property is not a string>",pszAttribute[x]);
  210.             }
  211.         }
  212.  
  213.         // Close the search handle to clean up
  214.         pSchemaNC->CloseSearchHandle(hSearch);
  215.     } 
  216.     return hr;
  217. }
  218.  
  219. HRESULT FindIndexedAttributes(IDirectorySearch *pSchemaNC, //IDirectorySearch pointer to schema naming context.
  220.          LPOLESTR pszAttributeNameType, //ldapDisplayName of the naming attribute you want to display for each returned attribute.
  221.                                          //NULL returns common name.
  222.          BOOL bIsIndexed //TRUE to find indexed attributes.
  223.                          //FALSE to find non-indexed attributes.
  224.         )
  225. {
  226.     //Create search filter
  227.     LPOLESTR pszSearchFilter = new OLECHAR[MAX_PATH*2];
  228.     DWORD dwIndexed;
  229.     if (bIsIndexed)
  230.        dwIndexed = 1;
  231.     else
  232.        dwIndexed = 0;
  233.     
  234.     wsprintf(pszSearchFilter, L"(&(objectCategory=attributeSchema)(searchFlags=%d))",dwIndexed);
  235.  
  236.     //Attributes are one-level deep in the Schema container so only need to search one level.
  237.     ADS_SEARCHPREF_INFO SearchPrefs;
  238.     SearchPrefs.dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
  239.     SearchPrefs.vValue.dwType = ADSTYPE_INTEGER;
  240.     SearchPrefs.vValue.Integer = ADS_SCOPE_ONELEVEL;
  241.     DWORD dwNumPrefs = 1;
  242.  
  243.     // COL for iterations
  244.     ADS_SEARCH_COLUMN col;
  245.     HRESULT hr;
  246.     
  247.     // Interface Pointers
  248.     IADs    *pObj = NULL;
  249.     IADs    * pIADs = NULL;
  250.  
  251.     // Handle used for searching
  252.     ADS_SEARCH_HANDLE hSearch;
  253.     
  254.     // Set the search preference
  255.     hr = pSchemaNC->SetSearchPreference( &SearchPrefs, dwNumPrefs);
  256.     if (FAILED(hr))
  257.         return hr;
  258.  
  259.     CONST DWORD dwAttrNameSize = 1;
  260.     LPOLESTR pszAttribute[dwAttrNameSize];
  261.  
  262.     if (!pszAttributeNameType)
  263.         pszAttribute[0] = L"cn";
  264.     else
  265.         pszAttribute[0] = pszAttributeNameType;
  266.  
  267.  
  268.     // Execute the search
  269.     hr = pSchemaNC->ExecuteSearch(pszSearchFilter,
  270.                                   pszAttribute,
  271.                                   dwAttrNameSize,
  272.                                   &hSearch
  273.                                   );
  274.     if ( SUCCEEDED(hr) )
  275.     {    
  276.  
  277.     // Call IDirectorySearch::GetNextRow() to retrieve the next row 
  278.     //of data
  279.         while( pSchemaNC->GetNextRow( hSearch) != S_ADS_NOMORE_ROWS )
  280.         {
  281.  
  282.             // loop through the array of passed column names,
  283.             // print the data for each column
  284.             for (DWORD x = 0; x < dwAttrNameSize; x++)
  285.             {
  286.  
  287.                 // Get the data for this column
  288.                 hr = pSchemaNC->GetColumn( hSearch, pszAttribute[x], &col );
  289.  
  290.                 if ( SUCCEEDED(hr) )
  291.                 {
  292.  
  293.                     // Print the data for the column and free the column
  294.                     wprintf(L"%s: %s\r\n",pszAttribute[x],col.pADsValues->CaseIgnoreString); 
  295.                     pSchemaNC->FreeColumn( &col );
  296.                 }
  297.                 else
  298.                     wprintf(L"<%s property is not a string>",pszAttribute[x]);
  299.             }
  300.         }
  301.  
  302.         // Close the search handle to clean up
  303.         pSchemaNC->CloseSearchHandle(hSearch);
  304.     } 
  305.     return hr;
  306. }
  307.  
  308. HRESULT FindGCAttributes(IDirectorySearch *pSchemaNC, //IDirectorySearch pointer to schema naming context.
  309.          LPOLESTR pszAttributeNameType, //ldapDisplayName of the naming attribute you want to display for each returned attribute.
  310.                                          //NULL returns common name.
  311.          BOOL bInGC //TRUE to find GC attributes.
  312.                          //FALSE to find non-GC attributes.
  313.         )
  314. {
  315.     //Create search filter
  316.     LPOLESTR pszSearchFilter = new OLECHAR[MAX_PATH*2];
  317.     LPOLESTR szBool = NULL;
  318.     if (bInGC)
  319.        szBool = L"TRUE";
  320.     else
  321.        szBool = L"FALSE";
  322.     
  323.     wsprintf(pszSearchFilter, L"(&(objectCategory=attributeSchema)(isMemberOfPartialAttributeSet=%s))",szBool);
  324.  
  325.     //Attributes are one-level deep in the Schema container so only need to search one level.
  326.     ADS_SEARCHPREF_INFO SearchPrefs;
  327.     SearchPrefs.dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
  328.     SearchPrefs.vValue.dwType = ADSTYPE_INTEGER;
  329.     SearchPrefs.vValue.Integer = ADS_SCOPE_ONELEVEL;
  330.     DWORD dwNumPrefs = 1;
  331.  
  332.     // COL for iterations
  333.     ADS_SEARCH_COLUMN col;
  334.     HRESULT hr;
  335.     
  336.     // Interface Pointers
  337.     IADs    *pObj = NULL;
  338.     IADs    * pIADs = NULL;
  339.  
  340.     // Handle used for searching
  341.     ADS_SEARCH_HANDLE hSearch;
  342.     
  343.     // Set the search preference
  344.     hr = pSchemaNC->SetSearchPreference( &SearchPrefs, dwNumPrefs);
  345.     if (FAILED(hr))
  346.         return hr;
  347.  
  348.     CONST DWORD dwAttrNameSize = 1;
  349.     LPOLESTR pszAttribute[dwAttrNameSize];
  350.  
  351.     if (!pszAttributeNameType)
  352.         pszAttribute[0] = L"cn";
  353.     else
  354.         pszAttribute[0] = pszAttributeNameType;
  355.  
  356.  
  357.     // Execute the search
  358.     hr = pSchemaNC->ExecuteSearch(pszSearchFilter,
  359.                                   pszAttribute,
  360.                                   dwAttrNameSize,
  361.                                   &hSearch
  362.                                   );
  363.     if ( SUCCEEDED(hr) )
  364.     {    
  365.  
  366.     // Call IDirectorySearch::GetNextRow() to retrieve the next row 
  367.     //of data
  368.         while( pSchemaNC->GetNextRow( hSearch) != S_ADS_NOMORE_ROWS )
  369.         {
  370.  
  371.             // loop through the array of passed column names,
  372.             // print the data for each column
  373.             for (DWORD x = 0; x < dwAttrNameSize; x++)
  374.             {
  375.  
  376.                 // Get the data for this column
  377.                 hr = pSchemaNC->GetColumn( hSearch, pszAttribute[x], &col );
  378.  
  379.                 if ( SUCCEEDED(hr) )
  380.                 {
  381.  
  382.                     // Print the data for the column and free the column
  383.                     wprintf(L"%s: %s\r\n",pszAttribute[x],col.pADsValues->CaseIgnoreString); 
  384.                     pSchemaNC->FreeColumn( &col );
  385.                 }
  386.                 else
  387.                     wprintf(L"<%s property is not a string>",pszAttribute[x]);
  388.             }
  389.         }
  390.  
  391.         // Close the search handle to clean up
  392.         pSchemaNC->CloseSearchHandle(hSearch);
  393.     } 
  394.     return hr;
  395. }
  396.  
  397.