home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / Native / JView / CRESPROP.CPP next >
Encoding:
C/C++ Source or Header  |  2000-05-04  |  3.7 KB  |  181 lines

  1. /*++
  2.  
  3. (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  4.  
  5. Module Name:
  6.  
  7.     cresprop.cpp
  8.  
  9. Abstract:
  10.  
  11.     Implements the CEnumResourceJAVAPROPERTY class.
  12.  
  13. --*/
  14.  
  15. #pragma hdrstop
  16.  
  17. #include "jview.h"
  18. #include "cresprop.hpp"
  19.  
  20. CEnumResourceJAVAPROPERTY::CEnumResourceJAVAPROPERTY(
  21.     LPRESOURCEJAVAPROPERTY pProperties, ULONG cProperties)
  22. {
  23.     m_cRef = 1;
  24.  
  25.     m_pProperties = pProperties;
  26.     m_cProperties = cProperties;
  27.     m_current = 0;
  28. }
  29.  
  30. STDMETHODIMP
  31. CEnumResourceJAVAPROPERTY::QueryInterface(REFIID riid, LPVOID *ppvObj)
  32. {
  33.     HRESULT hr;
  34.  
  35.     if (riid == IID_IEnumJAVAPROPERTY || riid == IID_IUnknown) {
  36.         *ppvObj = (LPENUMJAVAPROPERTY) this;
  37.         m_cRef++;
  38.         hr = S_OK;
  39.     } else {
  40.         *ppvObj = NULL;
  41.         hr = E_NOINTERFACE;
  42.     }
  43.  
  44.     return hr;
  45. }
  46.  
  47. STDMETHODIMP_(ULONG)
  48. CEnumResourceJAVAPROPERTY::AddRef()
  49. {
  50.     m_cRef++;
  51.  
  52.     return m_cRef;
  53. }
  54.  
  55. STDMETHODIMP_(ULONG)
  56. CEnumResourceJAVAPROPERTY::Release()
  57. {
  58.     m_cRef--;
  59.  
  60.     if (m_cRef > 0)
  61.         return m_cRef;
  62.  
  63.     delete this;
  64.     return 0;
  65. }
  66.  
  67. STDMETHODIMP
  68. CEnumResourceJAVAPROPERTY::Next(ULONG celt, LPJAVAPROPERTY rgelt, ULONG
  69.     *pceltFetched)
  70. {
  71.     HRESULT hr;
  72.     ULONG celtFetched;
  73.     char buffer[256];
  74.     int cchUnicode;
  75.  
  76.     hr = S_OK;
  77.     celtFetched = 0;
  78.  
  79.     while (hr == S_OK && celtFetched < celt) {
  80.  
  81.         if (m_current >= m_cProperties) {
  82.             hr = S_FALSE;
  83.             break;
  84.         }
  85.  
  86.         if (LoadString(GetModuleHandle(NULL), m_pProperties[m_current].ValueID,
  87.             buffer, sizeof(buffer)) > 0) {
  88.  
  89.             cchUnicode = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, buffer, -1,
  90.                 NULL, 0);
  91.  
  92.             if (cchUnicode > 0) {
  93.  
  94.                 rgelt->pszValue = (LPOLESTR) CoTaskMemAlloc(cchUnicode *
  95.                     sizeof(OLECHAR));
  96.  
  97.                 if (rgelt->pszValue != NULL) {
  98.                     MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, buffer, -1,
  99.                         rgelt->pszValue, cchUnicode);
  100.                 } else {
  101.                     hr = E_OUTOFMEMORY;
  102.                 }
  103.  
  104.             } else {
  105.                 hr = E_UNEXPECTED;
  106.             }
  107.  
  108.         } else {
  109.             hr = E_FAIL;
  110.         }
  111.  
  112.         if (hr == S_OK) {
  113.  
  114.             rgelt->pszKey = (LPOLESTR)
  115.                 CoTaskMemAlloc((lstrlenW(m_pProperties[m_current].pszKey) + 1) *
  116.                 sizeof(OLECHAR));
  117.  
  118.             if (rgelt->pszKey != NULL) {
  119.  
  120.                 wcscpy(rgelt->pszKey, m_pProperties[m_current].pszKey);
  121.  
  122.                 rgelt++;
  123.                 m_current++;
  124.                 celtFetched++;
  125.  
  126.             } else {
  127.                 CoTaskMemFree(rgelt->pszValue);
  128.                 hr = E_OUTOFMEMORY;
  129.             }
  130.         }
  131.     }
  132.  
  133.     if (pceltFetched != NULL)
  134.         *pceltFetched = celtFetched;
  135.  
  136.     return hr;
  137. }
  138.  
  139. STDMETHODIMP
  140. CEnumResourceJAVAPROPERTY::Skip(ULONG celt)
  141. {
  142.     HRESULT hr;
  143.  
  144.     if (m_current + celt <= m_cProperties) {
  145.         m_current += celt;
  146.         hr = S_OK;
  147.     } else {
  148.         m_current = m_cProperties;
  149.         hr = S_FALSE;
  150.     }
  151.  
  152.     return hr;
  153. }
  154.  
  155. STDMETHODIMP
  156. CEnumResourceJAVAPROPERTY::Reset()
  157. {
  158.     m_current = 0;
  159.     return S_OK;
  160. }
  161.  
  162. STDMETHODIMP
  163. CEnumResourceJAVAPROPERTY::Clone(LPENUMJAVAPROPERTY *ppenum)
  164. {
  165.     HRESULT hr;
  166.     CEnumResourceJAVAPROPERTY *pClone;
  167.  
  168.     pClone = new CEnumResourceJAVAPROPERTY(m_pProperties, m_cProperties);
  169.  
  170.     if (pClone != NULL) {
  171.         pClone->m_current = m_current;
  172.         *ppenum = (LPENUMJAVAPROPERTY) pClone;
  173.         hr = S_OK;
  174.     } else {
  175.         *ppenum = NULL;
  176.         hr = E_OUTOFMEMORY;
  177.     }
  178.  
  179.     return hr;
  180. }
  181.