|
ENUMVAR.CPP (BROWSEH OLE Sample)
/*************************************************************************
-
- OLE Automation TypeLibrary Browse Helper Sample
-
- enumvar.cpp
-
- CEnumVariant implementation
-
- Written by Microsoft Product Support Services, Windows Developer Support
- (c) Copyright Microsoft Corp. 1994 All Rights Reserved
-
- ***********************************************************************/
#include <windows.h>
#include <windowsx.h>
#ifdef WIN16
#include <ole2.h>
#include <compobj.h>
#include <dispatch.h>
#include <variant.h>
#include <olenls.h>
#endif
#include "browseh.h"
/*
* CEnumVariant::Create
*
* Purpose:
* Creates an instance of the IEnumVARIANT enumerator object and initializes
it.
*
* Parameters:
* psa Safe array containing items to be enumerated.
* cElements Number of items to be enumerated.
* ppenumvariant Returns enumerator object.
*
* Return Value:
* HRESULT
*
*/
HRESULT
CEnumVariant::Create(SAFEARRAY FAR* psa, ULONG cElements, CEnumVariant FAR*
FAR* ppenumvariant)
{
HRESULT hr;
CEnumVariant FAR* penumvariant = NULL;
long lLBound;
*ppenumvariant = NULL;
penumvariant = new CEnumVariant();
if (penumvariant == NULL)
goto error;
penumvariant->m_cRef = 0;
// Copy elements into safe array that is used in enumerator
implemenatation and
// initialize state of enumerator.
hr = SafeArrayGetLBound(psa, 1, &lLBound);
if (FAILED(hr))
goto error;
penumvariant->m_cElements = cElements;
penumvariant->m_lLBound = lLBound;
penumvariant->m_lCurrent = lLBound;
hr = SafeArrayCopy(psa, &penumvariant->m_psa);
if (FAILED(hr))
goto error;
*ppenumvariant = penumvariant;
return NOERROR;
error:
if (penumvariant == NULL)
return ResultFromScode(E_OUTOFMEMORY);
if (penumvariant->m_psa)
SafeArrayDestroy(penumvariant->m_psa);
penumvariant->m_psa = NULL;
delete penumvariant;
return hr;
}
/*
* CEnumVariant::CEnumVariant
*
* Purpose:
* Constructor for CEnumVariant object. Initializes members to NULL.
*
*/
CEnumVariant::CEnumVariant()
{
m_psa = NULL;
}
/*
* CEnumVariant::~CEnumVariant
*
* Purpose:
* Destructor for CEnumVariant object.
*
*/
CEnumVariant::~CEnumVariant()
{
if (m_psa) SafeArrayDestroy(m_psa);
}
/*
* CEnumVariant::QueryInterface, AddRef, Release
*
* Purpose:
* Implements IUnknown::QueryInterface, AddRef, Release
*
*/
STDMETHODIMP
CEnumVariant::QueryInterface(REFIID iid, void FAR* FAR* ppv)
{
*ppv = NULL;
if (iid == IID_IUnknown || iid == IID_IEnumVARIANT)
*ppv = this;
else return ResultFromScode(E_NOINTERFACE);
AddRef();
return NOERROR;
}
STDMETHODIMP_(ULONG)
CEnumVariant::AddRef(void)
{
#ifdef _DEBUG
TCHAR ach[50];
wsprintf(ach, TEXT("Ref = %ld, Enum\r\n"), m_cRef+1);
OutputDebugString(ach);
#endif
return ++m_cRef; // AddRef Application Object if enumerator will outlive
application object
}
STDMETHODIMP_(ULONG)
CEnumVariant::Release(void)
{
#ifdef _DEBUG
TCHAR ach[50];
wsprintf(ach, TEXT("Ref = %ld, Enum\r\n"), m_cRef-1);
OutputDebugString(ach);
#endif
if(--m_cRef == 0)
{
delete this;
return 0;
}
return m_cRef;
}
/*
* CEnumVariant::Next
*
* Purpose:
* Retrieves the next cElements elements. Implements IEnumVARIANT::Next.
*
*/
STDMETHODIMP
CEnumVariant::Next(ULONG cElements, VARIANT FAR* pvar, ULONG FAR*
pcElementFetched)
{
HRESULT hr;
ULONG l;
long l1;
ULONG l2;
if (pcElementFetched != NULL)
*pcElementFetched = 0;
for (l=0; l<cElements; l++)
VariantInit(&pvar[l]);
// Retrieve the next cElements elements.
for (l1=m_lCurrent, l2=0; l1<(long)(m_lLBound+m_cElements) &&
l2<cElements; l1++, l2++)
{
hr = SafeArrayGetElement(m_psa, &l1, &pvar[l2]);
if (FAILED(hr))
goto error;
}
// Set count of elements retrieved
if (pcElementFetched != NULL)
*pcElementFetched = l2;
m_lCurrent = l1;
return (l2 < cElements) ? ResultFromScode(S_FALSE) : NOERROR;
error:
for (l=0; l<cElements; l++)
VariantClear(&pvar[l]);
return hr;
}
/*
* CEnumVariant::Skip
*
* Purpose:
* Skips the next cElements elements. Implements IEnumVARIANT::Skip.
*
*/
STDMETHODIMP
CEnumVariant::Skip(ULONG cElements)
{
m_lCurrent += cElements;
if (m_lCurrent > (long)(m_lLBound+m_cElements))
{
m_lCurrent = m_lLBound+m_cElements;
return ResultFromScode(S_FALSE);
}
else return NOERROR;
}
/*
* CEnumVariant::Reset
*
* Purpose:
* Resets the current element in the enumerator to the beginning. Implements
IEnumVARIANT::Reset.
*
*/
STDMETHODIMP
CEnumVariant::Reset()
{
m_lCurrent = m_lLBound;
return NOERROR;
}
/*
* CEnumVariant::Clone
*
* Purpose:
* Creates a copy of the current enumeration state. Implements
IEnumVARIANT::Clone.
*
*/
STDMETHODIMP
CEnumVariant::Clone(IEnumVARIANT FAR* FAR* ppenum)
{
CEnumVariant FAR* penum = NULL;
HRESULT hr;
*ppenum = NULL;
hr = CEnumVariant::Create(m_psa, m_cElements, &penum);
if (FAILED(hr))
goto error;
penum->AddRef();
penum->m_lCurrent = m_lCurrent;
*ppenum = penum;
return NOERROR;
error:
if (penum)
penum->Release();
return hr;
}
| Last news from Greatis Software |
 |
|
Nostalgia .Net |
|
.Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes. More » |
| Recommended software for developers |
 |
|
Ultimate Pack |
|
Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price. More » |
 |
|
Form Designer .Net |
|
Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro! More » |
 |
|
Print Suite .Net |
|
Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available More » |
 |
|
Gradient Controls .Net |
|
Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available More » |
 |
|
Greatis iGrid |
|
iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors. More » |
All the contacts and projectsDmitry Vasiliev (just.dmitry)
Related LinksSoftware for Visual Studio .NET developers Software for Delphi and C++ Builder developers Software for Visual Basic 6 developers Delphi Tips&Tricks MegaDetailed.NET More Online Helps Win32 Programmer's Reference Win32 Multimedia Programmer's Reference OLE Programmer's Reference Microsoft Windows Pen API Programmer's Reference Microsoft Windows Sockets 2 Reference Microsoft Windows Telephony API (TAPI) Programmer's Reference Unix Manual Pages
|