home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / ADSDK.ZIP / Samples / General / ADQI / DirectoryObject.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-04-08  |  7.5 KB  |  328 lines

  1. //----------------------------------------------------------------------------
  2. //
  3. //  Microsoft Active Directory 2.5 Sample Code
  4. //
  5. //  Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. //  File:       IDirectoryObject.cpp
  8. //
  9. //  Contents:   IDirectoryObject usage
  10. //
  11. //
  12. //----------------------------------------------------------------------------
  13.  
  14. #include "stdafx.h"
  15. #include "ADQI.h"
  16. #include "DirectoryObject.h"
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[]=__FILE__;
  21. #define new DEBUG_NEW
  22. #endif
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CDlgIDirectoryObject dialog
  26.  
  27.  
  28. CDlgIDirectoryObject::CDlgIDirectoryObject(LPUNKNOWN pUnk, CWnd* pParent /*=NULL*/)
  29.     : CDialog(CDlgIDirectoryObject::IDD, pParent)
  30. {
  31.     //{{AFX_DATA_INIT(CDlgIDirectoryObject)
  32.     m_sDN = _T("");
  33.     m_sRDN = _T("");
  34.     m_sSchema = _T("");
  35.     m_sClass = _T("");
  36.     m_sParent = _T("");
  37.     m_sAttributes = _T("");
  38.     //}}AFX_DATA_INIT
  39.  
  40.     HRESULT hr;
  41.     m_pDirObject = NULL;
  42.     hr = pUnk->QueryInterface( IID_IDirectoryObject, (void **) &m_pDirObject );
  43.     if ( !SUCCEEDED(hr) )
  44.     {
  45.         AfxMessageBox(_T("Fatal Error! QI for IDirectoryObject failed"));
  46.         return;
  47.     }
  48.     pUnk->Release();
  49.  
  50.     ShowAttributes();
  51.  
  52. }
  53.  
  54.  
  55. CDlgIDirectoryObject::~CDlgIDirectoryObject()
  56. {
  57.     if ( m_pDirObject )
  58.     {
  59.         m_pDirObject->Release();
  60.     }
  61. }
  62.  
  63. void CDlgIDirectoryObject::DoDataExchange(CDataExchange* pDX)
  64. {
  65.     CDialog::DoDataExchange(pDX);
  66.     //{{AFX_DATA_MAP(CDlgIDirectoryObject)
  67.     DDX_Control(pDX, IDC_ATTRIBUTE, m_cAttributes);
  68.     DDX_Control(pDX, IDC_VALUELIST, m_cValueList);
  69.     DDX_Text(pDX, IDC_DN, m_sDN);
  70.     DDX_Text(pDX, IDC_RDN, m_sRDN);
  71.     DDX_Text(pDX, IDC_SCHEMA, m_sSchema);
  72.     DDX_Text(pDX, IDC_CLASS, m_sClass);
  73.     DDX_Text(pDX, IDC_PARENT, m_sParent);
  74.     DDX_Text(pDX, IDC_ATTRIBUTE, m_sAttributes);
  75.     //}}AFX_DATA_MAP
  76. }
  77.  
  78.  
  79. BEGIN_MESSAGE_MAP(CDlgIDirectoryObject, CDialog)
  80.     //{{AFX_MSG_MAP(CDlgIDirectoryObject)
  81.     ON_BN_CLICKED(IDC_DELETE, OnDelete)
  82.     ON_BN_CLICKED(IDC_GET, OnGet)
  83.     ON_EN_CHANGE(IDC_ATTRIBUTE, OnChangeAttribute)
  84.     //}}AFX_MSG_MAP
  85. END_MESSAGE_MAP()
  86.  
  87. /////////////////////////////////////////////////////////////////////////////
  88. // CDlgIDirectoryObject message handlers
  89. // Demonstrate:
  90. // IDirectoryObject::GetObjectInformation()
  91. //
  92. /////////////////////////////////////////////////////////////////////////////
  93.  
  94. void CDlgIDirectoryObject::ShowAttributes()
  95. {
  96.     ADS_OBJECT_INFO *pInfo;
  97.     HRESULT hr;
  98.  
  99.     hr = m_pDirObject->GetObjectInformation(&pInfo);
  100.     if (!SUCCEEDED(hr) )
  101.     {
  102.         return;
  103.     }
  104.     
  105.     /////////////////////////////////////////////
  106.     // Show the attributes in the UI
  107.     /////////////////////////////////////////////
  108.     USES_CONVERSION;
  109.  
  110.     m_sRDN        = OLE2T(pInfo->pszRDN);
  111.     m_sDN        = OLE2T(pInfo->pszObjectDN);
  112.     m_sParent    = OLE2T(pInfo->pszParentDN);
  113.     m_sClass    = OLE2T(pInfo->pszClassName);
  114.     m_sSchema    = OLE2T(pInfo->pszSchemaDN);
  115.     
  116.     ///////////////////////////////////////////////////////////
  117.     // Do not forget to clean-up the memory using FreeADsMem!
  118.     //////////////////////////////////////////////////////////
  119.     FreeADsMem( pInfo );
  120.  
  121. }
  122.  
  123. // 
  124. //  IDirectoryObject::DeleteDSObject();
  125. //
  126. //
  127. void CDlgIDirectoryObject::OnDelete() 
  128. {
  129.     CDeleteObjectDlg dlg;
  130.     CString s, sMsg;
  131.     HRESULT hr;
  132.  
  133.     if ( dlg.DoModal() == IDOK )
  134.     {
  135.          USES_CONVERSION;
  136.          // Example: CN=jsmith
  137.          s    = dlg.GetObjectName();
  138.          hr = m_pDirObject->DeleteDSObject( T2OLE(s) );
  139.     }
  140.  
  141.     if ( SUCCEEDED(hr) )
  142.     {
  143.         sMsg.Format(_T("Object '%s' was sucessfully deleted"), s); 
  144.         AfxMessageBox( sMsg );
  145.     }
  146.     else
  147.     {
  148.         AfxMessageBox(GetErrorMessage(hr));
  149.         return;
  150.     }
  151.     
  152. }
  153.  
  154. ///////////////////////////////////////////////////
  155. //
  156. // Demonstrate:
  157. // IDirectoryObject::GetObjectAttributes
  158. //
  159. //////////////////////////////////////////////////
  160.  
  161.  
  162. void CDlgIDirectoryObject::OnGet() 
  163. {
  164.    CStringList sList;
  165.    UINT nCount;
  166.    HRESULT hr;
  167.    
  168.  
  169.    UpdateData(TRUE);
  170.  
  171.    StringToStringList(m_sAttributes, sList );
  172.    nCount = sList.GetCount();
  173.    if ( nCount == 0 )
  174.    {
  175.        return;
  176.    }
  177.  
  178.    
  179.    
  180.    ///////////////////////////////////////
  181.    // Now build the Attribute Names List
  182.    ///////////////////////////////////////
  183.    POSITION            pos;
  184.    DWORD            dwNumAttr;
  185.    LPWSTR            *pAttrNames=NULL;
  186.    ADS_ATTR_INFO    *pAttrInfo;
  187.    DWORD            dwReturn;
  188.    
  189.  
  190.    USES_CONVERSION;
  191.  
  192.  
  193.    pAttrNames = (LPWSTR*) AllocADsMem( sizeof(LPWSTR) * nCount );
  194.    pos = sList.GetHeadPosition();
  195.    dwNumAttr = 0;
  196.    while( pos != NULL )
  197.    {
  198.        pAttrNames[dwNumAttr] = T2OLE(sList.GetAt(pos));
  199.        dwNumAttr++;
  200.        sList.GetNext(pos);
  201.    }
  202.  
  203.    
  204.  
  205.    
  206.    /////////////////////////////////////////
  207.    // Get attributes value requested
  208.    // Note: The order is not necessarily the same as
  209.    //       requested via pAttrNames.
  210.    ///////////////////////////////////////////
  211.    hr = m_pDirObject->GetObjectAttributes( pAttrNames, dwNumAttr, &pAttrInfo, &dwReturn );
  212.  
  213.    
  214.  
  215.    if ( SUCCEEDED(hr) )
  216.    {
  217.       DWORD idx;
  218.       CString sDisplay;
  219.       CStringList sValueList;
  220.       UINT nCount;
  221.       POSITION pos;
  222.  
  223.       m_cValueList.ResetContent();
  224.  
  225.       for( idx=0; idx < dwReturn; idx++) 
  226.       {          
  227.           ADsToStringList( pAttrInfo[idx].pADsValues, pAttrInfo[idx].dwNumValues, sValueList );
  228.           sDisplay = OLE2T(pAttrInfo[idx].pszAttrName);
  229.           sDisplay += _T(":");
  230.           m_cValueList.AddString( sDisplay );
  231.           nCount = sValueList.GetCount();
  232.           if ( nCount = 0 ) // can not find/convert the value
  233.           {
  234.               m_cValueList.AddString(_T(" > [No Value]"));
  235.               continue;
  236.           }
  237.           else
  238.           {
  239.               pos = sValueList.GetHeadPosition();
  240.               while( pos != NULL )
  241.               {
  242.                   sDisplay = _T(" > ");
  243.                   sDisplay += sValueList.GetAt(pos);
  244.                   m_cValueList.AddString( sDisplay );
  245.                   sValueList.GetNext(pos);
  246.               }
  247.  
  248.           }
  249.           
  250.       }
  251.  
  252.       sValueList.RemoveAll();
  253.    }
  254.  
  255.    
  256.  
  257.    ///////////////////////////////////////////////////////////
  258.    // Use FreADsMem for all memory obtained from ADSI call 
  259.    /////////////////////////////////////////////////////////////
  260.    FreeADsMem( pAttrInfo );
  261.    FreeADsMem( pAttrNames );
  262.  
  263.     
  264. }
  265.  
  266. void CDlgIDirectoryObject::OnChangeAttribute() 
  267. {
  268.     BOOL bEnabled;
  269.     bEnabled = m_cAttributes.GetWindowTextLength() > 0 ? TRUE : FALSE;
  270.     GetDlgItem( IDC_GET )->EnableWindow( bEnabled );
  271.     
  272. }
  273.  
  274. BOOL CDlgIDirectoryObject::OnInitDialog() 
  275. {
  276.     CDialog::OnInitDialog();
  277.     
  278.     OnChangeAttribute();
  279.     return TRUE;  // return TRUE unless you set the focus to a control
  280.                   // EXCEPTION: OCX Property Pages should return FALSE
  281. }
  282. /////////////////////////////////////////////////////////////////////////////
  283. // CDeleteObjectDlg dialog
  284.  
  285.  
  286. CDeleteObjectDlg::CDeleteObjectDlg(CWnd* pParent /*=NULL*/)
  287.     : CDialog(CDeleteObjectDlg::IDD, pParent)
  288. {
  289.     //{{AFX_DATA_INIT(CDeleteObjectDlg)
  290.     m_sDelete = _T("");
  291.     //}}AFX_DATA_INIT
  292. }
  293.  
  294.  
  295. void CDeleteObjectDlg::DoDataExchange(CDataExchange* pDX)
  296. {
  297.     CDialog::DoDataExchange(pDX);
  298.     //{{AFX_DATA_MAP(CDeleteObjectDlg)
  299.     DDX_Text(pDX, IDC_DELETE, m_sDelete);
  300.     //}}AFX_DATA_MAP
  301. }
  302.  
  303.  
  304. BEGIN_MESSAGE_MAP(CDeleteObjectDlg, CDialog)
  305.     //{{AFX_MSG_MAP(CDeleteObjectDlg)
  306.     ON_EN_CHANGE(IDC_DELETE, OnChangeDelete)
  307.     //}}AFX_MSG_MAP
  308. END_MESSAGE_MAP()
  309.  
  310. /////////////////////////////////////////////////////////////////////////////
  311. // CDeleteObjectDlg message handlers
  312.  
  313. void CDeleteObjectDlg::OnOK() 
  314. {
  315.     UpdateData( TRUE );
  316.     m_sRDN = m_sDelete;
  317.     
  318.     CDialog::OnOK();
  319. }
  320.  
  321. void CDeleteObjectDlg::OnChangeDelete() 
  322. {
  323.     BOOL bEnabled;
  324.     bEnabled = GetDlgItem( IDC_DELETE )->GetWindowTextLength() > 0 ? TRUE : FALSE;
  325.     GetDlgItem( IDOK )->EnableWindow( bEnabled );
  326.     
  327. }
  328.