home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / netds / adsi / sampapp / adscmd / dump.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-13  |  2.9 KB  |  163 lines

  1. //----------------------------------------------------------------------
  2. //
  3. //  Microsoft Active Directory 1.0 Sample Code
  4. //
  5. //  Copyright (C) Microsoft Corporation, 1996
  6. //
  7. //  File:       dump.cxx
  8. //
  9. //  Contents:   Functions for dumping the properties for an object.
  10. //
  11. //
  12. //----------------------------------------------------------------------
  13.  
  14. #include "main.hxx"
  15.  
  16. //
  17. // Given an ADsPath, bind to the object and call the DumpObject routine.
  18. //
  19.  
  20. int
  21. DoDump(char *AnsiADsPath)
  22. {
  23.  HRESULT hr = E_OUTOFMEMORY ;
  24.  LPWSTR pszADsPath = NULL;
  25.  IADs * pADs = NULL;
  26.  
  27.  //
  28.  // Convert path to unicode and then bind to the object.
  29.  //
  30.  
  31.  BAIL_ON_NULL(pszADsPath = AllocateUnicodeString(AnsiADsPath));
  32.  
  33.  hr = ADsGetObject(
  34.              pszADsPath,
  35.              IID_IADs,
  36.              (void **)&pADs
  37.              );
  38.  
  39.  if (FAILED(hr)) {
  40.  
  41.      printf("Failed to bind to object: %S\n", pszADsPath) ;
  42.  }
  43.  else {
  44.  
  45.      //
  46.      // Dump the object
  47.      //
  48.  
  49.      hr = DumpObject(pADs);
  50.  
  51.      if (FAILED(hr)) {
  52.  
  53.          printf("Unable to read properties of: %S\n", pszADsPath) ;
  54.      }
  55.  
  56.      pADs->Release();
  57.  }
  58.  
  59. error:
  60.  
  61.  FreeUnicodeString(pszADsPath);
  62.  
  63.  return (FAILED(hr) ? 1 : 0) ;
  64. }
  65.  
  66. //
  67. // Given an ADs pointer, dump the contents of the object
  68. //
  69.  
  70. HRESULT
  71. DumpObject(
  72.  IADs * pADs
  73.  )
  74. {
  75.  HRESULT hr;
  76. HRESULT hrSA;
  77.  IADs * pADsProp = NULL;
  78.  VARIANT var;
  79. ZeroMemory(&var,sizeof(var));
  80. VARIANT *   pvarPropName = NULL;
  81.  DWORD i = 0;
  82. VARIANT varProperty;
  83.  IDispatch * pDispatch = NULL;
  84.  
  85.  //
  86.  // Access the schema for the object
  87.  //
  88.  
  89.  hr = GetPropertyList(
  90.              pADs,
  91.              &var);
  92.  BAIL_ON_FAILURE(hr);
  93.  
  94.  //
  95.  // List the Properties
  96. //
  97. hr = SafeArrayAccessData(var.parray, (void **) &pvarPropName);
  98. BAIL_ON_FAILURE(hr);
  99.  
  100. for (i = 0; i < var.parray->rgsabound[0].cElements; i++){
  101.  
  102.    //
  103.      // Get a property and print it out. The HRESULT is passed to
  104.      // PrintProperty.
  105.      //
  106.  
  107.      hr = pADs->Get(
  108.              pvarPropName[i].bstrVal,
  109.              &varProperty
  110.              );
  111.    PrintProperty(
  112.          pvarPropName[i].bstrVal,
  113.          hr,
  114.          varProperty
  115.          );
  116.  
  117. }
  118.  
  119. hr = SafeArrayUnaccessData(var.parray);
  120.  
  121. error:
  122. // Don't destroy hr in case we're here from BAIL_ON_FAILURE
  123. if(var.parray) hrSA = SafeArrayDestroy(var.parray);
  124.  
  125. return(hr);
  126. }
  127.  
  128.  
  129. HRESULT
  130. GetPropertyList(
  131.  IADs * pADs,
  132.  VARIANT * pvar )
  133. {
  134.  HRESULT hr= S_OK;
  135.  BSTR bstrSchemaPath = NULL;
  136. IADsClass * pADsClass = NULL;
  137.  
  138.  hr = pADs->get_Schema(&bstrSchemaPath);
  139.  BAIL_ON_FAILURE(hr);
  140.  
  141.  hr = ADsGetObject(
  142.              bstrSchemaPath,
  143.              IID_IADsClass,
  144.              (void **)&pADsClass);
  145.  BAIL_ON_FAILURE(hr);
  146.  
  147. //Put SafeArray of bstr's into input variant struct
  148. hr = pADsClass->get_MandatoryProperties(pvar);
  149. BAIL_ON_FAILURE(hr);
  150.  
  151. error:
  152.  if (bstrSchemaPath) {
  153.      SysFreeString(bstrSchemaPath);
  154.  }
  155.  
  156.  if (pADsClass) {
  157.      pADsClass->Release();
  158.  }
  159.  
  160.  return(hr);
  161. }
  162.  
  163.