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

  1.  
  2. #define INC_OLE2
  3. #define UNICODE 1
  4. #define _WIN32_DCOM
  5.  
  6.  
  7. #include "ADSIhelpers.h"
  8.  
  9.  
  10. #include <stdio.h>
  11. #include <assert.h>
  12. #include <iads.h>
  13. #include <adshlp.h>
  14. #include <adserr.h>
  15. #include <wchar.h>
  16. #include <objbase.h>
  17. #include <activeds.h>
  18. //Make sure you define UNICODE
  19. //Need to define version 5 for Windows 2000
  20. #define _WIN32_WINNT 0x0500
  21.  
  22.  
  23. #include <sddl.h>
  24.  
  25. //////////////////////////////////////////////////////////////////////
  26. //
  27. // PLEASE READ: You MUST run on Windows 2000 to run this sample
  28. //
  29. ////////////////////////////////////////////////////////////////////////
  30.  
  31.  
  32. // Calls all the methods on a IADs object
  33. // and prints them
  34. void PrintIADSObject(IADs * pIADs)
  35. {
  36.     assert(pIADs);
  37.  
  38.     BSTR bsResult;
  39.  
  40.     pIADs->get_Name(&bsResult); 
  41.     wprintf(L" NAME: %s\n",(LPOLESTR) bsResult);
  42.     SysFreeString(bsResult);
  43.     
  44.     pIADs->get_Class(&bsResult); 
  45.     wprintf(L" CLASS: %s\n",(LPOLESTR) bsResult);
  46.     SysFreeString(bsResult);
  47.     
  48.     pIADs->get_GUID(&bsResult); 
  49.     wprintf(L" GUID: %s\n",(LPOLESTR) bsResult);
  50.     SysFreeString(bsResult);
  51.     
  52.     pIADs->get_ADsPath(&bsResult); 
  53.     wprintf(L" ADSPATH: %s\n",(LPOLESTR) bsResult);
  54.     SysFreeString(bsResult);
  55.     
  56.     pIADs->get_Parent(&bsResult); 
  57.     wprintf(L" PARENT: %s\n",(LPOLESTR) bsResult);
  58.     SysFreeString(bsResult);
  59.     
  60.     pIADs->get_Schema(&bsResult); 
  61.     wprintf(L" SCHEMA: %s\n",(LPOLESTR) bsResult);
  62.     SysFreeString(bsResult);
  63.     
  64. }
  65.  
  66.  
  67. // Will display a message box for a BAD Hresult.
  68. // Will do nothing for a S_OK HRESULT
  69. // Pass in the HRESULT to be tested and a char * describing the
  70. // area in the code 
  71. void CheckHRESULT(HRESULT hr, const char *pszCause)
  72. {
  73.     if (FAILED(hr))
  74.     {
  75.         char sz[1024];
  76.         char szCaption[1024];
  77.         strcpy(szCaption, pszCause);
  78.         strcat(szCaption, " failed!");
  79.         if (!FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0, hr, 0, sz, 1024, 0))
  80.             strcpy(sz, "Unknown error");
  81.         MessageBoxA(0, sz, szCaption, MB_SETFOREGROUND);
  82.        // DebugBreak();
  83.     }
  84. }
  85.  
  86.  
  87. // When passed a VARIANT vt member, will return a string describing
  88. // all the bitmask settings...
  89. const char * GetVariantStyle(VARTYPE vt)
  90. {
  91.     static char pRet[200]; 
  92.  
  93.     pRet[0] = NULL;
  94.     
  95.     if (vt == VT_EMPTY) strcat(pRet,"VT_EMPTY ");
  96.  
  97.     if (HAS_BIT_STYLE(vt, VT_BYREF)) strcat(pRet,"VT_BYREF ");
  98.     if (HAS_BIT_STYLE(vt, VT_UI1)) strcat(pRet,"VT_UI1 ");
  99.     if (HAS_BIT_STYLE(vt, VT_I2)) strcat(pRet,"VT_I2 ");
  100.     if (HAS_BIT_STYLE(vt, VT_I4)) strcat(pRet,"VT_I4 ");
  101.     if (HAS_BIT_STYLE(vt, VT_R4)) strcat(pRet,"VT_R4 ");
  102.     if (HAS_BIT_STYLE(vt, VT_R8)) strcat(pRet,"VT_R8 ");
  103.     if (HAS_BIT_STYLE(vt, VT_CY)) strcat(pRet,"VT_CY ");
  104.     if (HAS_BIT_STYLE(vt, VT_BSTR)) strcat(pRet,"VT_BSTR ");
  105.     if (HAS_BIT_STYLE(vt, VT_NULL)) strcat(pRet,"VT_NULL ");
  106.     if (HAS_BIT_STYLE(vt, VT_ERROR)) strcat(pRet,"VT_ERROR ");
  107.     if (HAS_BIT_STYLE(vt, VT_BOOL)) strcat(pRet,"VT_BOOL ");
  108.     if (HAS_BIT_STYLE(vt, VT_DATE)) strcat(pRet,"VT_DATE ");
  109.     if (HAS_BIT_STYLE(vt, VT_DISPATCH)) strcat(pRet,"VT_DISPATCH ");
  110.     if (HAS_BIT_STYLE(vt, VT_VARIANT)) strcat(pRet,"VT_VARIANT ");
  111.     if (HAS_BIT_STYLE(vt, VT_UNKNOWN)) strcat(pRet,"VT_UNKNOWN ");
  112.     if (HAS_BIT_STYLE(vt, VT_ARRAY)) strcat(pRet,"VT_ARRAY ");
  113.  
  114.     return pRet;          
  115. }
  116.  
  117.  
  118.  
  119. // Pass in the interface ptr to the property value
  120. // will return a BSTR value of the data.
  121. // The IADsPropertyValue::get_ADsType()  is called to retrieve the 
  122. // ADSTYPE valued enum 
  123. // This enum is then used to determine which IADsPropertyValue method
  124. // to call to receive the actual data
  125.  
  126. // CALLER assumes responsibility for freeing returned BSTR
  127. HRESULT    GetIADsPropertyValueAsBSTR(BSTR * pbsRet,IADsPropertyEntry *pAdsEntry, IADsPropertyValue * pAdsPV)
  128. {
  129.     HRESULT hr = S_OK;
  130.  
  131.     long lAdsType;
  132.     hr = pAdsPV->get_ADsType(&lAdsType);
  133.     
  134.     if (FAILED(hr))
  135.         return hr;
  136.  
  137.     switch (lAdsType)
  138.     {
  139.         case ADSTYPE_INVALID :
  140.         {
  141.             *pbsRet = SysAllocString(L"<ADSTYPE_INVALID>");
  142.         }
  143.         break;
  144.  
  145.         case ADSTYPE_DN_STRING :
  146.         {
  147.             hr = pAdsPV->get_DNString(pbsRet);
  148.         }
  149.         break;
  150.         case ADSTYPE_CASE_EXACT_STRING :
  151.         {
  152.             hr = pAdsPV->get_CaseExactString(pbsRet);
  153.         }
  154.         break;
  155.         case ADSTYPE_CASE_IGNORE_STRING :
  156.         {
  157.             hr = pAdsPV->get_CaseIgnoreString(pbsRet);
  158.         }
  159.         break;
  160.         case ADSTYPE_PRINTABLE_STRING :
  161.         {
  162.             hr = pAdsPV->get_PrintableString(pbsRet);
  163.         }
  164.         break;
  165.         case ADSTYPE_NUMERIC_STRING :
  166.         {
  167.             hr = pAdsPV->get_NumericString(pbsRet);
  168.         }
  169.         break;
  170.         case ADSTYPE_BOOLEAN :
  171.         {
  172.             long b;
  173.             hr = pAdsPV->get_Boolean(&b);
  174.             if (SUCCEEDED(hr))
  175.             {
  176.                 if (b)
  177.                     *pbsRet = SysAllocString(L"<TRUE>");
  178.                 else
  179.                     *pbsRet = SysAllocString(L"<FALSE>");
  180.             }
  181.         }
  182.         break;
  183.         case ADSTYPE_INTEGER :
  184.         {
  185.             long lInt;
  186.             hr = pAdsPV->get_Integer(&lInt);
  187.             if (SUCCEEDED(hr))
  188.             {
  189.                 WCHAR wOut[100];
  190.                 wsprintf(wOut,L"%d",lInt);
  191.                 *pbsRet = SysAllocString(wOut);
  192.             }
  193.         }
  194.         break;
  195.         case ADSTYPE_OCTET_STRING :
  196.         {
  197.             *pbsRet = SysAllocString(L"<ADSTYPE_OCTET_STRING>");
  198.             BSTR bsName= NULL;
  199.             VARIANT vOctet;
  200.             DWORD dwSLBound;
  201.             DWORD dwSUBound;
  202.             void HUGEP *pArray;
  203.             VariantInit(&vOctet);
  204.     
  205.                 //Get the name of the property to handle
  206.                 //the properties we're interested in.
  207.                 pAdsEntry->get_Name(&bsName);
  208.                 hr = pAdsPV->get_OctetString(&vOctet);
  209.                 
  210.                 //Get a pointer to the bytes in the octet string.
  211.                 if (SUCCEEDED(hr))
  212.                 {
  213.                     hr = SafeArrayGetLBound( V_ARRAY(&vOctet),
  214.                                               1,
  215.                                               (long FAR *) &dwSLBound );
  216.                     hr = SafeArrayGetUBound( V_ARRAY(&vOctet),
  217.                                               1,
  218.                                               (long FAR *) &dwSUBound );
  219.                     if (SUCCEEDED(hr))
  220.                     {
  221.                         hr = SafeArrayAccessData( V_ARRAY(&vOctet),
  222.                                                   &pArray );
  223.                     }
  224.                     /* Since an Octet String has a specific meaning 
  225.                        depending on the attribute name, handle two 
  226.                        common ones here
  227.                     */
  228.                     if (0==wcscmp(L"objectGUID", bsName))
  229.                     {
  230.                         LPOLESTR szDSGUID = new WCHAR [39];
  231.  
  232.                         //Cast to LPGUID
  233.                         LPGUID pObjectGUID = (LPGUID)pArray;
  234.                         //Convert GUID to string.
  235.                         ::StringFromGUID2(*pObjectGUID, szDSGUID, 39); 
  236.                         *pbsRet = SysAllocString(szDSGUID);
  237.  
  238.                     }
  239.                     else if (0==wcscmp(L"objectSid", bsName))
  240.                     {
  241.                         PSID pObjectSID = (PSID)pArray;
  242.  
  243.                         //Convert SID to string.
  244.                         LPOLESTR szSID = NULL;
  245.                         ConvertSidToStringSid(pObjectSID, &szSID);
  246.                         *pbsRet = SysAllocString(szSID);
  247.                         LocalFree(szSID);
  248.                     }
  249.                     else
  250.                     {
  251.                         *pbsRet = SysAllocString(L"<Value of type Octet String. No Conversion>");
  252.  
  253.                     }
  254.                         SafeArrayUnaccessData( V_ARRAY(&vOctet) );
  255.                         VariantClear(&vOctet);
  256.                 }
  257.  
  258.                 SysFreeString(bsName);
  259.                 
  260.  
  261.         }
  262.         break;
  263.         case ADSTYPE_LARGE_INTEGER :
  264.         {
  265.             *pbsRet = SysAllocString(L"<ADSTYPE_LARGE_INTEGER>");
  266.         }
  267.         break;
  268.         case ADSTYPE_PROV_SPECIFIC :
  269.         {
  270.             *pbsRet = SysAllocString(L"<ADSTYPE_PROV_SPECIFIC>");
  271.         }
  272.         break;
  273.         case ADSTYPE_OBJECT_CLASS :
  274.         {
  275.             hr = pAdsPV->get_CaseIgnoreString(pbsRet);
  276.         }
  277.         break;
  278.         case ADSTYPE_PATH :
  279.         {
  280.             hr = pAdsPV->get_CaseIgnoreString(pbsRet);
  281.         }
  282.         break;
  283.         case ADSTYPE_NT_SECURITY_DESCRIPTOR :
  284.         {
  285.             *pbsRet = SysAllocString(L"<ADSTYPE_NT_SECURITY_DESCRIPTOR>");
  286.         }
  287.         break;
  288.     
  289.         default: 
  290.             *pbsRet = SysAllocString(L"<UNRECOGNIZED>");
  291.         break;
  292.             
  293.     }    
  294.     return hr;
  295. }
  296.  
  297.  
  298.