⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 impiopcitemmgt.cpp

📁 基于Intellution开发包的开发的OPC服务器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ImpIOPCItemMgt.cpp
//
//  This file contains the implementation of 
//  the IOPCItemMgt interface for groups in the XXX server.
//
//
//	(c) COPYRIGHT 1996-1998, INTELLUTION INC.
// ALL RIGHTS RESERVED
//
//
//	Functions defined in this module:
//
//			CImpIOPCItemMgt::CImpIDataObject()
//			CImpIOPCItemMgt::~CImpIDataObject()
//			CImpIOPCItemMgt::AddRef()
//			CImpIOPCItemMgt::Release()
//			CImpIOPCItemMgt::QueryInterface()
//			CImpIOPCItemMgt::AddItems()
//			CImpIOPCItemMgt::ValidateItems()
//			CImpIOPCItemMgt::RemoveItems()
//			CImpIOPCItemMgt::SetActiveState()
//			CImpIOPCItemMgt::SetClientHandles()
//			CImpIOPCItemMgt::SetDatatypes()
//			CImpIOPCItemMgt::CreateEnumerator()
//
//
//
// Modification Log:
//	Vers	Date     By		Notes
//	----	-------- ---	-----
//	1.0		08/26/97 jra	Created
//	1.3		03/10/98 jra	Modified to be wizard generated and driver specific.
//	7.11	09/10/98 jra	Changed AddItems() to zero out allocated memory.
//
//

#define WIN32_LEAN_AND_MEAN

#include "OpcStdAfx.h"
#include "OPC.h"
#include "OPCError.h"


/////////////////////////////////////////////////////////////////////////////
// Constructor /Destructor functions
//

////////////////////////////////////////////////////////////////
// CImpIOPCItemMgt()
//   Constructor for this Interface
//
////////////////////////////////////////////////////////////////
CImpIOPCItemMgt::CImpIOPCItemMgt(LPUNKNOWN pUnkOuter)
{
	m_pUnkOuter		= pUnkOuter;
	m_pParentGroup	= (COPCDrvGroup *)pUnkOuter;
}


////////////////////////////////////////////////////////////////
// ~CImpIOPCItemMgt()
//   Destructor for this Interface
//
////////////////////////////////////////////////////////////////
CImpIOPCItemMgt::~CImpIOPCItemMgt(void)
{
	m_pParentGroup->m_pCImpIItemMgt = NULL;
}


/////////////////////////////////////////////////////////////////////////////
// IUnknown functions Delegate to Parent
//

////////////////////////////////////////////////////////////////
// AddRef()
//
////////////////////////////////////////////////////////////////
STDMETHODIMP_(ULONG) CImpIOPCItemMgt::AddRef(void)
{
	return m_pUnkOuter->AddRef();
}


////////////////////////////////////////////////////////////////
// Release()
//
////////////////////////////////////////////////////////////////
STDMETHODIMP_(ULONG) CImpIOPCItemMgt::Release(void)
{
	return m_pUnkOuter->Release(); 
}


////////////////////////////////////////////////////////////////
// QueryInterface()
//
////////////////////////////////////////////////////////////////
STDMETHODIMP CImpIOPCItemMgt::QueryInterface(REFIID		iid, 
											 LPVOID		*ppInterface)
{
	return m_pUnkOuter->QueryInterface(iid, ppInterface);
}



/////////////////////////////////////////////////////////////////////////////
// CImpIOPCItemMgt (IOPCItemMgt) interface functions
//


////////////////////////////////////////////////////////////////
// AddItems()
//
// This function adds a list of items to a specific group.
//
// Returns:
//	HRESULT	-	S_OK if all Items added with no errors.
//			-	S_FALSE if some Items added with no errors, but
//				other Items added with errors. The caller will
//				then want to look at the ppErrors array.
//			-	E_OUTOFMEMORY if a memory allocator failed.
//			-	E_INVALIDARG if a parameter passed into the
//				function is invalid, such as a NULL pointer.
//			-	E_FAIL if the function failed altogether.
//			-	OPC_E_PUBLIC is a valid return value, but we
//				don't support public groups at all, so the client
//				can't try and add items to a public group because
//				we won't let them add a public group.
////////////////////////////////////////////////////////////////
STDMETHODIMP CImpIOPCItemMgt::AddItems(DWORD			dwNumItems, 
									   OPCITEMDEF		*pItemArray,
									   OPCITEMRESULT	**ppAddResults, 
									   HRESULT			**ppErrors)
{
	OPCHANDLE		OPCHandle;
	OPCITEMRESULT	*pItemResult;
	HRESULT			*pHResultList,
					hr = S_OK;
	COPCDrvItem		*pNewItem;


	// Make sure we were passed good pointers
	//
	if((NULL == pItemArray)   ||
	   (NULL == ppAddResults) ||
	   (NULL == ppErrors))
	{
		return E_INVALIDARG;
	}

	// Defaults in case of error
	//
	*ppAddResults = pItemResult = NULL;
	*ppErrors = pHResultList = NULL;

	// First - allocate memory for the result array(s)
	//
	*ppAddResults = pItemResult = 
		(OPCITEMRESULT *)pIMalloc->Alloc(sizeof(OPCITEMRESULT) * dwNumItems);
	if(NULL == pItemResult)
	{
		*ppErrors = NULL;
		*ppAddResults = NULL;
		return E_OUTOFMEMORY;
	}

	// Now allocate the memory for the error array(s)
	//
	*ppErrors = pHResultList = 
		(HRESULT *)pIMalloc->Alloc(sizeof(HRESULT) * dwNumItems);
	if(NULL == pHResultList)
	{
		if(pItemResult)
		{
			pIMalloc->Free(pItemResult);
		}

		*ppErrors = NULL;
		*ppAddResults = NULL;
		return E_OUTOFMEMORY;
	}

	// jra 091998
	// Initialize the allocated memory to 0
	memset(pItemResult, 0, sizeof(OPCITEMRESULT) * dwNumItems);
	memset(pHResultList, 0, sizeof(HRESULT) * dwNumItems);

	// Now for each item... 
	//
	for (DWORD i = 0; i < dwNumItems; i++)
	{
		// Get the memory and a handle for this Item
		//
		pHResultList[i] = m_pParentGroup->ItemAlloc(&OPCHandle, &pNewItem);
		if (FAILED(pHResultList[i]))
		{
			pHResultList[i] = OPC_E_INVALIDITEMID;
			hr = S_FALSE;
			continue;
		}

		// Make sure the ItemID is syntatically correct and initialize it
		//
		pHResultList[i] = pNewItem->Init(OPCHandle,			// Server handle
										 &pItemArray[i],	// Item definition 
										 &pItemResult[i]);	// Item result
		if(FAILED(pHResultList[i]))
		{
			// Determine from the return value of Init() what the return
			// value for this HRESULT should be per the OPC spec.
			//
			switch(pHResultList[i])
			{
			case E_FAIL:
				pHResultList[i] = OPC_E_BADTYPE;
				break;
			default:
				pHResultList[i] = OPC_E_INVALIDITEMID;
				break;
			}
			delete pNewItem;
			hr = S_FALSE;
			continue;
		}

		// Everything is fine, so add the new item to the map and
		// record the fact the the group has a pointer to it
		//
		m_pParentGroup->ItemSet(pNewItem);
		pNewItem->AddRef();
	}

	return hr;
}


////////////////////////////////////////////////////////////////
// ValidateItems()
//
// This function determines if an ItemID would be added to the
// group without error. It does not affect the group in any way.
// It also returns informtion to the caller regarding canonical
// datatype, etc.
//
// Returns:
//	HRESULT	-	S_OK if everything was successful.
//			-	S_FALSE if the call was partially successful. This
//				tells the caller to see the ppErrors array for
//				more information.
//			-	E_FAIL if the function failed.
//
////////////////////////////////////////////////////////////////
STDMETHODIMP CImpIOPCItemMgt::ValidateItems(DWORD			dwNumItems, 
											OPCITEMDEF		*pItemArray,
											BOOL			bBlobUpdate, 
											OPCITEMRESULT	**ppValidationResults, 
											HRESULT			**ppErrors)
{
	OPCITEMRESULT	*pItemResult;
	HRESULT			*pHResultList,
					hr = S_OK;


	// Make sure we were passed good pointers
	//
	if((NULL == pItemArray)			 ||
	   (NULL == ppValidationResults) ||
	   (NULL == ppErrors))
	{
		return E_INVALIDARG;
	}

	// Defaults in case of error
	//
	*ppValidationResults = pItemResult = NULL;
	*ppErrors = pHResultList = NULL;

	// First - allocate memory for the result array(s)
	//
	*ppValidationResults = pItemResult = 
		(OPCITEMRESULT *)pIMalloc->Alloc(sizeof(OPCITEMRESULT) * dwNumItems);
	if(NULL == pItemResult)
	{
		*ppErrors = NULL;
		*ppValidationResults = NULL;
		return E_FAIL;
	}

	// Now allocate the memory for the error array(s)
	//
	*ppErrors = pHResultList = 
		(HRESULT *)pIMalloc->Alloc(sizeof(HRESULT) * dwNumItems);
	if(NULL == pHResultList)
	{
		if(pItemResult)
		{
			pIMalloc->Free(pItemResult);
		}

		*ppErrors = NULL;
		*ppValidationResults = NULL;
		return E_FAIL;
	}

	// Now for each item... 
	//
	COPCDrvItem	*pTempItem = new COPCDrvItem(this->m_pParentGroup);
	pTempItem->AddRef();
	for(DWORD i = 0; i < dwNumItems; i++)
	{
		// We call Init() on each item definition. This will verify the
		// driver name, I/O address, signal conditioning, etc through a
		// call to the NIO functions for that driver.
		//
		pHResultList[i] = pTempItem->Init((OPCHANDLE)i,		// dummy handle
										  &pItemArray[i],	// OPCITEMDEF
										  &pItemResult[i]);	// OPCITEMRESULT
		
		// Modify the error returned
		if(FAILED(pHResultList[i]))
		{
			switch(pHResultList[i])
			{
			case E_FAIL:
				pHResultList[i] = OPC_E_BADTYPE;
				break;
			default:
				pHResultList[i] = OPC_E_INVALIDITEMID;
				break;
			}
			hr = S_FALSE;
		}
	}
	// Release the temp item. This will effectively delete it
	//
	pTempItem->Release();

	return hr;
}


////////////////////////////////////////////////////////////////
// RemoveItems()
//
// This function deletes a list of items from a specific group.
//
// Returns:
//	HRESULT	-	S_OK if everything was successful

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -