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

  1. // bindtoparent.cpp : Defines the entry point for the console application.
  2. //
  3. #include <wchar.h>
  4. #include <objbase.h>
  5. #include <activeds.h>
  6.  
  7. HRESULT GetParentObject(IADs *pObject, //Pointer the object whose parent to bind to.
  8.                         IADs **ppParent //Return a pointer to the parent object.
  9.                         );
  10.  
  11. HRESULT FindUserByName(IDirectorySearch *pSearchBase, //Container to search
  12.                        LPOLESTR szFindUser, //Name of user to find.
  13.                        IADs **ppUser); //Return a pointer to the user
  14.  
  15.  
  16. void wmain( int argc, wchar_t *argv[ ])
  17. {
  18.  
  19. //Handle the command line arguments.
  20. LPOLESTR pszBuffer = new OLECHAR[MAX_PATH*2];
  21. if (argv[1] == NULL)
  22. {
  23.     wprintf(L"This program finds a user in the current Window 2000 domain\n");
  24.     wprintf(L"and displays its parent container's ADsPath and binds to that container.\n");
  25. //    wprintf(L"This program demonstrates reading a property of type octet string.\n\n");
  26.     
  27.     wprintf(L"Enter Common Name of the user to find:");
  28.     _getws(pszBuffer);
  29. }
  30. else
  31.    wcscpy(pszBuffer, argv[1]);
  32. //if empty string, exit.
  33. if (0==wcscmp(L"", pszBuffer))
  34.    return;
  35.     
  36. wprintf(L"\nFinding user: %s...\n",pszBuffer);
  37.     
  38. //Intialize COM
  39. CoInitialize(NULL);
  40. HRESULT hr = S_OK;
  41. //Get rootDSE and the domain container's DN.
  42. IADs *pObject = NULL;
  43. IADs *pParent = NULL;
  44. IDirectorySearch *pDS = NULL;
  45. LPOLESTR szPath = new OLECHAR[MAX_PATH];
  46. VARIANT var;
  47. hr = ADsOpenObject(L"LDAP://rootDSE",
  48.                  NULL,
  49.                  NULL,
  50.                  ADS_SECURE_AUTHENTICATION, //Use Secure Authentication
  51.                  IID_IADs,
  52.                  (void**)&pObject);
  53. if (FAILED(hr))
  54. {
  55.    wprintf(L"Not Found. Could not bind to the domain.\n");
  56.    if (pObject)
  57.      pObject->Release();
  58.    return;
  59. }
  60.  
  61. hr = pObject->Get(L"defaultNamingContext",&var);
  62. if (SUCCEEDED(hr))
  63. {
  64.     wcscpy(szPath,L"LDAP://"); // If you're running on NT 4.0 or Win9.x machine, you need to 
  65.                                // add the server name e.g L"LDAP://myServer"
  66.     wcscat(szPath,var.bstrVal);
  67.     VariantClear(&var);
  68.     if (pObject)
  69.     {
  70.        pObject->Release();
  71.        pObject = NULL;
  72.     }
  73.     //Bind to the root of the current domain.
  74.     hr = ADsOpenObject(szPath,
  75.                      NULL,
  76.                      NULL,
  77.                      ADS_SECURE_AUTHENTICATION, //Use Secure Authentication
  78.                      IID_IDirectorySearch,
  79.                      (void**)&pDS);
  80.     if (SUCCEEDED(hr))
  81.     {
  82.         hr =  FindUserByName(pDS, //Container to search
  83.                            pszBuffer, //Name of user to find.
  84.                            &pObject); //Return a pointer to the user
  85.         if (SUCCEEDED(hr))
  86.         {
  87.             BSTR bstr, bstrC;
  88.             hr = GetParentObject(pObject, //Pointer the object whose parent to bind to.
  89.                         &pParent //Return a pointer to the parent object.
  90.                         );
  91.             if (SUCCEEDED(hr))
  92.             {
  93.                    wprintf(L"Successfully bound to parent container\n");
  94.                    //Get ADsPath
  95.                    hr = pParent->get_ADsPath(&bstr);
  96.                       //Get the distinguishedName property
  97.                    hr = pParent->Get(L"distinguishedName", &var);
  98.                    //Get class
  99.                    hr = pParent->get_Class(&bstrC);
  100.                    if (SUCCEEDED(hr))
  101.                    {
  102.                       wprintf(L"ADsPath: %s\n",bstr);
  103.                       wprintf(L"DN: %s\n",var.bstrVal);
  104.                       wprintf(L"Class: %s\n",bstrC);
  105.                    }
  106.                    else
  107.                         wprintf(L"Get method failed with hr: %x\n",hr);
  108.                    VariantClear(&var);
  109.                    //Free the BSTR.
  110.                    FreeADsStr(bstr);
  111.                    FreeADsStr(bstrC);
  112.             }
  113.             else
  114.                 wprintf(L"GetParentObject failed with hr: %x\n",hr);
  115.             if (pParent)
  116.                 pParent->Release();
  117.         }
  118.         else
  119.         {
  120.             wprintf(L"User \"%s\" not Found.\n",pszBuffer);
  121.             wprintf (L"FindUserByName failed with the following HR: %x\n", hr);
  122.         }
  123.         if (pObject)
  124.             pObject->Release();
  125.     }
  126.  
  127.     if (pDS)
  128.        pDS->Release();
  129. }
  130.  
  131. //Uninitalize COM
  132. CoUninitialize();
  133.  
  134.     return;
  135. }
  136.  
  137. HRESULT GetParentObject(IADs *pObject, //Pointer the object whose parent to bind to.
  138.                         IADs **ppParent //Return a pointer to the parent object.
  139.                         )
  140. {
  141.     if ((!pObject)||(!ppParent))
  142.         return E_INVALIDARG;
  143.  
  144.     HRESULT hr = E_FAIL;
  145.     BSTR bstr;
  146.     hr = pObject->get_Parent(&bstr);
  147.     if (SUCCEEDED(hr))
  148.     {
  149.         //Bind to the parent container.
  150.         *ppParent = NULL;
  151.         hr = ADsOpenObject(bstr,
  152.              NULL,
  153.              NULL,
  154.              ADS_SECURE_AUTHENTICATION, //Use Secure Authentication
  155.              IID_IADs,
  156.              (void**)ppParent);
  157.         if(FAILED(hr))
  158.         {
  159.             if (!(*ppParent))
  160.             {
  161.               (*ppParent)->Release();
  162.               (*ppParent) = NULL;
  163.             }
  164.         }
  165.     }
  166.     FreeADsStr(bstr);
  167.     return hr;
  168. }
  169.  
  170.  
  171.  
  172.  
  173.  
  174. HRESULT FindUserByName(IDirectorySearch *pSearchBase, //Container to search
  175.                        LPOLESTR szFindUser, //Name of user to find.
  176.                        IADs **ppUser) //Return a pointer to the user
  177. {
  178.     HRESULT hrObj = E_FAIL;
  179.     HRESULT hr = E_FAIL;
  180.     if ((!pSearchBase)||(!szFindUser))
  181.         return E_INVALIDARG;
  182.     //Create search filter
  183.     LPOLESTR pszSearchFilter = new OLECHAR[MAX_PATH];
  184.     LPOLESTR szADsPath = new OLECHAR[MAX_PATH];
  185.     wcscpy(pszSearchFilter, L"(&(objectCategory=person)(objectClass=user)(cn=");
  186.     wcscat(pszSearchFilter, szFindUser);
  187.     wcscat(pszSearchFilter,    L"))");
  188.     //Search entire subtree from root.
  189.     ADS_SEARCHPREF_INFO SearchPrefs;
  190.     SearchPrefs.dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
  191.     SearchPrefs.vValue.dwType = ADSTYPE_INTEGER;
  192.     SearchPrefs.vValue.Integer = ADS_SCOPE_SUBTREE;
  193.     DWORD dwNumPrefs = 1;
  194.     // COL for iterations
  195.     ADS_SEARCH_COLUMN col;
  196.     // Handle used for searching
  197.     ADS_SEARCH_HANDLE hSearch;
  198.     // Set the search preference
  199.     hr = pSearchBase->SetSearchPreference( &SearchPrefs, dwNumPrefs);
  200.     if (FAILED(hr))
  201.         return hr;
  202.     // Set attributes to return
  203.     CONST DWORD dwAttrNameSize = 1;
  204.     LPOLESTR pszAttribute[dwAttrNameSize] = {L"ADsPath"};
  205.  
  206.     // Execute the search
  207.     hr = pSearchBase->ExecuteSearch(pszSearchFilter,
  208.                                   pszAttribute,
  209.                                   dwAttrNameSize,
  210.                                   &hSearch
  211.                                   );
  212.     if (SUCCEEDED(hr))
  213.     {    
  214.  
  215.     // Call IDirectorySearch::GetNextRow() to retrieve the next row 
  216.     //of data
  217.         while( pSearchBase->GetNextRow( hSearch) != S_ADS_NOMORE_ROWS )
  218.         {
  219.             // loop through the array of passed column names,
  220.             // print the data for each column
  221.             for (DWORD x = 0; x < dwAttrNameSize; x++)
  222.             {
  223.                 // Get the data for this column
  224.                 hr = pSearchBase->GetColumn( hSearch, pszAttribute[x], &col );
  225.                 if ( SUCCEEDED(hr) )
  226.                 {
  227.                     // Print the data for the column and free the column
  228.                     // Note the attribute we asked for is type CaseIgnoreString.
  229.                     wcscpy(szADsPath, col.pADsValues->CaseIgnoreString); 
  230.                     wprintf(L"Found User %s.\n",szFindUser); 
  231.                     wprintf(L"%s: %s\r\n",pszAttribute[x],col.pADsValues->CaseIgnoreString); 
  232.                     //If multiple users returned, just use the first one.
  233.                     if (SUCCEEDED(hrObj))
  234.                     {
  235.                        hrObj = S_FALSE;
  236.                        wprintf(L"\n");
  237.                     }
  238.                     else
  239.                     {
  240.                        hr = ADsOpenObject(szADsPath,
  241.                                      NULL,
  242.                                      NULL,
  243.                                      ADS_SECURE_AUTHENTICATION, //Use Secure Authentication
  244.                                      IID_IADs,
  245.                                      (void**)ppUser);
  246.                        if (SUCCEEDED(hr))
  247.                          hrObj = S_OK;
  248.                     }
  249.                     pSearchBase->FreeColumn( &col );
  250.                 }
  251.                 else
  252.                     hr = E_FAIL;
  253.             }
  254.         }
  255.         // Close the search handle to clean up
  256.         pSearchBase->CloseSearchHandle(hSearch);
  257.     }
  258.     if (FAILED(hrObj))
  259.         hr = hrObj;
  260.     else if (S_FALSE == hrObj)
  261.     {
  262.         VARIANT var;
  263.         wprintf(L"---------------------------------------------------\n"); 
  264.         wprintf(L"More than one user with CN %s was found.\n",szFindUser); 
  265.         (*ppUser)->Get(L"distinguishedName", &var);
  266.         wprintf(L"Returning pointer to User (DN): %s\n",var.bstrVal); 
  267.         wprintf(L"---------------------------------------------------\n"); 
  268.         VariantClear(&var);
  269.     }
  270.     return hr;
  271. }
  272.