📄 impienumopcitemattributes.cpp
字号:
// ImpIEnumOPCItemAttributes.cpp
//
// This file contains an implementation of an IEnumOPCItemAttributes interface.
//
// Since the list of elements is buffered in the object
// It is appropriate for enumerations with a 'reasonable' number of elements
//
// Note that new/delete is used for local buffer storage
// while the global allocator is used for returned storage.
//
// See IEnumXXXX::Next in Win32SDK\OLE\Reference\Interfaces\IEnumXXXX
// for general guidelines for enumerators
//
// (c) COPYRIGHT 1996-1998, INTELLUTION INC.
// ALL RIGHTS RESERVED
//
//
// Functions defined in this module:
//
// CImpIEnumOPCItemAttributes::CImpIEnumOPCItemAttributes()
// CImpIEnumOPCItemAttributes::~CImpIEnumOPCItemAttributes()
// CImpIEnumOPCItemAttributes::AddRef()
// CImpIEnumOPCItemAttributes::Release()
// CImpIEnumOPCItemAttributes::QueryInterface()
// CImpIEnumOPCItemAttributes::Next()
// CImpIEnumOPCItemAttributes::Skip()
// CImpIEnumOPCItemAttributes::Reset()
// CImpIEnumOPCItemAttributes::Clone()
//
//
//
// Modification Log:
// Vers Date By Notes
// ---- -------- --- -----
// 1.00 08/26/97 jra Created
// 1.3 03/10/98 jra Modified to be wizard generated and driver specific.
//
#define WIN32_LEAN_AND_MEAN
#include "OpcStdAfx.h"
#include "OPC.h"
////////////////////////////////////////////////////////////////
// CImpIEnumOPCItemAttributes::CImpIEnumOPCItemAttributes
//
// Parameters:
// pUnkRef - LPUNKNOWN to use for reference counting.
// cstr - ULONG number of OPCITEMATTRIBTESs in pIA
// pIA - ptr to IAs
// pmem - IMalloc memory allocator to use for returned data
//
////////////////////////////////////////////////////////////////
CImpIEnumOPCItemAttributes::CImpIEnumOPCItemAttributes(LPUNKNOWN pUnkRef,
ULONG cIA,
OPCITEMATTRIBUTES *pIA,
IMalloc *pmem)
{
UINT i;
m_cRef = 0;
m_pUnkRef = pUnkRef;
m_iCur = 0;
m_cIA = cIA;
m_pOIA = new OPCITEMATTRIBUTES[cIA]; // use local memory
m_pmem = pmem;
if (NULL != m_pOIA)
{
// Make local copies of all the IAs
//
for (i = 0; i < m_cIA; i++)
{
IAClone(&m_pOIA[i], &pIA[i], NULL); // use local memory
}
}
return;
}
////////////////////////////////////////////////////////////////
// CImpIEnumOPCItemAttributes::~CImpIEnumOPCItemAttributes(void)
//
////////////////////////////////////////////////////////////////
CImpIEnumOPCItemAttributes::~CImpIEnumOPCItemAttributes(void)
{
unsigned int i;
if (NULL != m_pOIA)
{
// Delete the local copies of all the IAs
//
for (i = 0; i < m_cIA; i++)
{
IAFree(&m_pOIA[i], NULL);
}
delete [] m_pOIA;
}
return;
}
////////////////////////////////////////////////////////////////
// QueryInterface()
//
////////////////////////////////////////////////////////////////
STDMETHODIMP CImpIEnumOPCItemAttributes::QueryInterface(REFIID riid,
LPVOID *ppv)
{
*ppv = NULL;
if (IID_IUnknown == riid || IID_IEnumOPCItemAttributes == riid)
{
*ppv=(LPVOID)this;
}
if (NULL != *ppv)
{
((LPUNKNOWN)*ppv)->AddRef();
return S_OK;
}
return E_NOINTERFACE;
}
////////////////////////////////////////////////////////////////
// AddRef()
//
////////////////////////////////////////////////////////////////
STDMETHODIMP_(ULONG) CImpIEnumOPCItemAttributes::AddRef(void)
{
// Addref this object and also the 'parent' if any
//
InterlockedIncrement(&m_cRef);
if(m_pUnkRef != NULL)
{
m_pUnkRef->AddRef();
}
return m_cRef;
}
////////////////////////////////////////////////////////////////
// Release()
//
////////////////////////////////////////////////////////////////
STDMETHODIMP_(ULONG) CImpIEnumOPCItemAttributes::Release(void)
{
// Release this object and also the 'parent' if any
//
if(m_pUnkRef != NULL)
{
m_pUnkRef->Release();
}
if (0L != InterlockedDecrement(&m_cRef))
{
return m_cRef;
}
delete this;
return 0;
}
////////////////////////////////////////////////////////////////
// CImpIEnumOPCItemAttributes::Next()
//
// Purpose:
// Returns the next element in the enumeration.
//
// Parameters:
// cIA - ULONG max number of OPCITEMATTRIBTESs to return.
// pIA - OPCITEMATTRIBTES(s) in which to store the returned
// structures.
// pulstr ULONG * in which to return how many we
// actually returned.
//
// Return Value:
// HRESULT - S_OK if successful, S_FALSE otherwise,
//
////////////////////////////////////////////////////////////////
STDMETHODIMP CImpIEnumOPCItemAttributes::Next(ULONG cIA,
OPCITEMATTRIBUTES **pIA,
ULONG *pActual)
{
ULONG cReturn = 0L,
maxcount = cIA;
OPCITEMATTRIBUTES *temp;
*pActual = 0L; // default return count
*pIA = NULL;
// If this enumerator is empty - return FALSE (should never happen)
//
if (NULL == m_pOIA)
{
return S_FALSE;
}
// If user passed null for count of items returned
// Then he is only allowed to ask for 1 item
//
if (NULL == pActual)
{
if (1L != cIA)
{
return E_POINTER;
}
}
// If we are at end of list return FALSE
//
if (m_iCur >= m_cIA)
{
return S_FALSE;
}
// Allocate the items to be returned
//
temp = (OPCITEMATTRIBUTES*)pIMalloc->Alloc(cIA * sizeof(OPCITEMATTRIBUTES));
if(NULL == temp)
{
return S_FALSE;
}
// Return as many as we have left in list up to request count
//
while (m_iCur < m_cIA && cIA > 0)
{
// Fill in an IA to return to caller
IAClone(&temp[cReturn], &m_pOIA[m_iCur], m_pmem); // Use global memory for strings
// And move on to the next one
//
m_iCur++;
cReturn++;
cIA--;
}
// Shorten the block to the size actually used
// and return the final pointer to the caller
//
*pIA = (OPCITEMATTRIBUTES*)pIMalloc->Realloc(temp,
cReturn * sizeof(OPCITEMATTRIBUTES));
if (NULL != pActual)
{
*pActual = cReturn;
}
if (cReturn == maxcount)
{
return S_OK;
}
return S_FALSE;
}
////////////////////////////////////////////////////////////////
// CImpIEnumOPCItemAttributes::Skip()
//
// Purpose:
// Skips the next n elements in the enumeration.
//
// Parameters:
// cSkip - ULONG number of elements to skip.
//
// Return Value:
// HRESULT - S_OK if successful, S_FALSE if we could not
// skip the requested number.
//
////////////////////////////////////////////////////////////////
STDMETHODIMP CImpIEnumOPCItemAttributes::Skip(ULONG cSkip)
{
if (((m_iCur+cSkip) >= m_cIA) || NULL==m_pOIA)
{
return S_FALSE;
}
m_iCur+=cSkip;
return S_OK;
}
////////////////////////////////////////////////////////////////
// CImpIEnumOPCItemAttributes::Reset()
//
// Purpose:
// Resets the current element index in the enumeration to zero.
//
// Parameters:
// None
//
////////////////////////////////////////////////////////////////
STDMETHODIMP CImpIEnumOPCItemAttributes::Reset(void)
{
m_iCur=0;
return S_OK;
}
////////////////////////////////////////////////////////////////
// CImpIEnumOPCItemAttributes::Clone()
//
// Purpose:
// Returns another IEnumIA with the same state as ourselves.
//
// Parameters:
// ppEnum - CImpIEnumOPCItemAttributes * in which to return the
// new object.
//
////////////////////////////////////////////////////////////////
STDMETHODIMP CImpIEnumOPCItemAttributes::Clone(IEnumOPCItemAttributes **ppEnum)
{
CImpIEnumOPCItemAttributes *pNew;
*ppEnum = NULL;
//Create the clone
//
pNew = new CImpIEnumOPCItemAttributes(m_pUnkRef, m_cIA, m_pOIA, m_pmem);
if (NULL == pNew)
{
return E_OUTOFMEMORY;
}
pNew->AddRef();
// Set the 'state' of the clone to match the state if this
//
pNew->m_iCur = m_iCur;
*ppEnum = pNew;
return S_OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -