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 / util.cxx < prev   
Encoding:
C/C++ Source or Header  |  1997-07-29  |  6.3 KB  |  313 lines

  1. //---------------------------------------------------------------------------
  2. //
  3. //  Microsoft Active Directory 1.0 Sample Code
  4. //
  5. //  Copyright (C) Microsoft Corporation, 1992 - 1995
  6. //
  7. //  File:  util.cxx
  8. //
  9. //  Contents:  Ansi to Unicode conversions and misc helper functions
  10. //
  11. //----------------------------------------------------------------------------
  12.  
  13. #include "main.hxx"
  14.  
  15. //
  16. // Local functions
  17. //
  18.  
  19.  
  20. int
  21. AnsiToUnicodeString(
  22.     LPSTR pAnsi,
  23.     LPWSTR pUnicode,
  24.     DWORD StringLength
  25.     )
  26. {
  27.     int iReturn;
  28.  
  29.     if( StringLength == 0 )
  30.         StringLength = strlen( pAnsi );
  31.  
  32.     iReturn = MultiByteToWideChar(CP_ACP,
  33.                                   MB_PRECOMPOSED,
  34.                                   pAnsi,
  35.                                   StringLength + 1,
  36.                                   pUnicode,
  37.                                   StringLength + 1 );
  38.  
  39.     //
  40.     // Ensure NULL termination.
  41.     //
  42.     pUnicode[StringLength] = 0;
  43.  
  44.     return iReturn;
  45. }
  46.  
  47.  
  48. int
  49. UnicodeToAnsiString(
  50.     LPWSTR pUnicode,
  51.     LPSTR pAnsi,
  52.     DWORD StringLength
  53.     )
  54. {
  55.     LPSTR pTempBuf = NULL;
  56.     INT   rc = 0;
  57.  
  58.     if( StringLength == 0 ) {
  59.  
  60.         //
  61.         // StringLength is just the
  62.         // number of characters in the string
  63.         //
  64.         StringLength = wcslen( pUnicode );
  65.     }
  66.  
  67.     //
  68.     // WideCharToMultiByte doesn't NULL terminate if we're copying
  69.     // just part of the string, so terminate here.
  70.     //
  71.  
  72.     pUnicode[StringLength] = 0;
  73.  
  74.     //
  75.     // Include one for the NULL
  76.     //
  77.     StringLength++;
  78.  
  79.     //
  80.     // Unfortunately, WideCharToMultiByte doesn't do conversion in place,
  81.     // so allocate a temporary buffer, which we can then copy:
  82.     //
  83.  
  84.     if( pAnsi == (LPSTR)pUnicode )
  85.     {
  86.         pTempBuf = (LPSTR)LocalAlloc( LPTR, StringLength );
  87.         pAnsi = pTempBuf;
  88.     }
  89.  
  90.     if( pAnsi )
  91.     {
  92.         rc = WideCharToMultiByte( CP_ACP,
  93.                                   0,
  94.                                   pUnicode,
  95.                                   StringLength,
  96.                                   pAnsi,
  97.                                   StringLength,
  98.                                   NULL,
  99.                                   NULL );
  100.     }
  101.  
  102.     /* If pTempBuf is non-null, we must copy the resulting string
  103.      * so that it looks as if we did it in place:
  104.      */
  105.     if( pTempBuf && ( rc > 0 ) )
  106.     {
  107.         pAnsi = (LPSTR)pUnicode;
  108.         strcpy( pAnsi, pTempBuf );
  109.         LocalFree( pTempBuf );
  110.     }
  111.  
  112.     return rc;
  113. }
  114.  
  115.  
  116. LPWSTR
  117. AllocateUnicodeString(
  118.     LPSTR  pAnsiString
  119.     )
  120. {
  121.     LPWSTR  pUnicodeString = NULL;
  122.  
  123.     if (!pAnsiString)
  124.         return NULL;
  125.  
  126.     pUnicodeString = (LPWSTR)LocalAlloc(
  127.                         LPTR,
  128.                         strlen(pAnsiString)*sizeof(WCHAR) + sizeof(WCHAR)
  129.                         );
  130.  
  131.     if (pUnicodeString) {
  132.  
  133.         AnsiToUnicodeString(
  134.             pAnsiString,
  135.             pUnicodeString,
  136.             0
  137.             );
  138.     }
  139.  
  140.     return pUnicodeString;
  141. }
  142.  
  143.  
  144. void
  145. FreeUnicodeString(
  146.     LPWSTR  pUnicodeString
  147.     )
  148. {
  149.  
  150.     LocalFree(pUnicodeString);
  151.  
  152.     return;
  153. }
  154.  
  155.  
  156. //
  157. // Misc helper functions for displaying data.
  158. //
  159.  
  160.  
  161. HRESULT
  162. PrintVariant(
  163.     VARIANT varPropData
  164.     )
  165. {
  166.     HRESULT hr;
  167.     BSTR bstrValue;
  168.  
  169.     switch (varPropData.vt) {
  170.     case VT_I4:
  171.         printf("%d", varPropData.lVal);
  172.         break;
  173.     case VT_BSTR:
  174.         printf("%S", varPropData.bstrVal);
  175.         break;
  176.  
  177.     case VT_BOOL:
  178.         printf("%d", V_BOOL(&varPropData));
  179.         break;
  180.  
  181.     case (VT_ARRAY | VT_VARIANT):
  182.         PrintVariantArray(varPropData);
  183.         break;
  184.  
  185.     case VT_DATE:
  186.         hr = VarBstrFromDate(
  187.                  varPropData.date,
  188.                  LOCALE_SYSTEM_DEFAULT,
  189.                  LOCALE_NOUSEROVERRIDE,
  190.                  &bstrValue
  191.                  );
  192.         printf("%S", bstrValue);
  193.         break;
  194.  
  195.     default:
  196.         printf("Data type is %d\n", varPropData.vt);
  197.         break;
  198.  
  199.     }
  200.     printf("\n");
  201.     return(S_OK);
  202. }
  203.  
  204.  
  205. HRESULT
  206. PrintVariantArray(
  207.     VARIANT var
  208.     )
  209. {
  210.     LONG dwSLBound = 0;
  211.     LONG dwSUBound = 0;
  212.     VARIANT v;
  213.     LONG i;
  214.     HRESULT hr = S_OK;
  215.  
  216.     if(!((V_VT(&var) &  VT_VARIANT) &&  V_ISARRAY(&var))) {
  217.         return(E_FAIL);
  218.     }
  219.  
  220.     //
  221.     // Check that there is only one dimension in this array
  222.     //
  223.  
  224.     if ((V_ARRAY(&var))->cDims != 1) {
  225.         hr = E_FAIL;
  226.         BAIL_ON_FAILURE(hr);
  227.     }
  228.     //
  229.     // Check that there is atleast one element in this array
  230.     //
  231.  
  232.     if ((V_ARRAY(&var))->rgsabound[0].cElements == 0){
  233.         hr = E_FAIL;
  234.         BAIL_ON_FAILURE(hr);
  235.     }
  236.  
  237.     //
  238.     // We know that this is a valid single dimension array
  239.     //
  240.  
  241.     hr = SafeArrayGetLBound(V_ARRAY(&var),
  242.                             1,
  243.                             (long FAR *)&dwSLBound
  244.                             );
  245.     BAIL_ON_FAILURE(hr);
  246.  
  247.     hr = SafeArrayGetUBound(V_ARRAY(&var),
  248.                             1,
  249.                             (long FAR *)&dwSUBound
  250.                             );
  251.     BAIL_ON_FAILURE(hr);
  252.  
  253.     for (i = dwSLBound; i <= dwSUBound; i++) {
  254.         VariantInit(&v);
  255.         hr = SafeArrayGetElement(V_ARRAY(&var),
  256.                                 (long FAR *)&i,
  257.                                 &v
  258.                                 );
  259.         if (FAILED(hr)) {
  260.             continue;
  261.         }
  262.         if (i < dwSUBound) {
  263.             printf("%S, ", v.bstrVal);
  264.         } else {
  265.             printf("%S", v.bstrVal);
  266.         }
  267.     }
  268.     return(S_OK);
  269.  
  270. error:
  271.     return(hr);
  272. }
  273.  
  274.  
  275. HRESULT
  276. PrintProperty(
  277.     BSTR bstrPropName,
  278.     HRESULT hRetVal,
  279.     VARIANT varPropData
  280.     )
  281. {
  282.     HRESULT hr = S_OK;
  283.  
  284.     switch (hRetVal) {
  285.  
  286.     case 0:
  287.         printf("%-32S: ", bstrPropName);
  288.         PrintVariant(varPropData);
  289.         break;
  290.  
  291.     case E_ADS_CANT_CONVERT_DATATYPE:
  292.         printf("%-32S: ", bstrPropName);
  293.         printf("<Data could not be converted for display>\n");
  294.         break;
  295.  
  296.     default:
  297.         printf("%-32S: ", bstrPropName);
  298.         printf("<Data not available>\n");
  299.         break;
  300.  
  301.     }
  302.     return(hr);
  303. }
  304.  
  305. void
  306. PrintUsage(
  307.     void
  308.     )
  309. {
  310.     printf("usage: adscmd [list|dump] <ADsPath>\n") ;
  311. }
  312.  
  313.