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

📄 opcdrvutility.cpp

📁 基于Intellution开发包的开发的OPC服务器
💻 CPP
字号:
// OPCDrvUtility.cpp
//
//  This file contains some string utility functions
//  for the OPC Sample server.
//
//	(c) COPYRIGHT 1996-1998, INTELLUTION INC.
// ALL RIGHTS RESERVED
//
//
//	Functions defined in this module:
//
//			BSTRFromWSTR()
//			WSTRFromBSTR()
//			BSTRFree()
//			WSTRClone()
//			WSTRFree()
//			IAClone()
//			IAFree()
//			LogEventMessage()
//			RegisterEventService()
//			DeRegisterEventService()
//
//
//
// 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.
//
//


#define WIN32_LEAN_AND_MEAN

#include "OpcStdAfx.h"

#include <String.h>

#include "OPCDrv.h"

HANDLE	g_hEventSource = NULL;

HRESULT NewArray(unsigned long lElements, SAFEARRAY ** ppsaArray);

////////////////////////////////////////////////////////////////
// BSTRFromWSTR()
//
// Utility function to malloc a BSTR 
// and copy a WSTR into it
//
////////////////////////////////////////////////////////////////
BSTR BSTRFromWSTR(const WCHAR	*s, 
				  IMalloc		*pmem)
{
	BSTR	bstr;
	int		length;


	length = wcslen(s) + 1;		// number of WCHARS plus NUL
	
	// get space (add 4 more bytes for the BSTR size DWORD)
	//
	if(pmem)
	{
		bstr = (BSTR) pmem->Alloc(length + sizeof(DWORD));
	}
	else
	{
		bstr = new WCHAR[ length + 2];
	}
	
	if(bstr == NULL)
	{
		return NULL;
	}

	bstr[0] = length ;		// includes NUL but not DWORD
	bstr[1] = 0;
	wcscpy(&bstr[2] , s);
	return &bstr[2];
}


////////////////////////////////////////////////////////////////
// WSTRFromBSTR()
//
// Utility function to malloc a WSTR 
// and copy a BSTR into it
//
////////////////////////////////////////////////////////////////
WCHAR * WSTRFromBSTR(BSTR		bstr, 
					 IMalloc	*pmem)
{
	WCHAR *wstr;


	bstr--;
	bstr--;
	if(pmem)
	{
		wstr = (WCHAR*) pmem->Alloc( bstr[0] );
	}
	else
	{
		wstr = new WCHAR[bstr[0]];
	}
	
	if(wstr == NULL)
	{
		return NULL;
	}

	wcscpy(wstr, &bstr[2]);

	return wstr;
}


////////////////////////////////////////////////////////////////
// BSTRFree()
//
// Utility function to Free a BSTR 
// which was allocated above
//
////////////////////////////////////////////////////////////////
void BSTRFree(BSTR			bstr, 
			  IMalloc		*pmem)
{
	if(bstr == NULL)
	{
		return;
	}

	bstr--;
	bstr--;

	if(pmem)
	{
		pmem->Free(bstr);
	}
	else delete [] bstr;
}


////////////////////////////////////////////////////////////////
// WSTRClone()
//
// Clone a Wide String 
//
////////////////////////////////////////////////////////////////
WCHAR * WSTRClone(const WCHAR	*oldstr, 
				  IMalloc		*pmem)
{
	WCHAR *newstr;

	if(pmem) 
	{
		newstr = (WCHAR*)pmem->Alloc(sizeof(WCHAR) * (wcslen(oldstr) + 1));
	}
	else 
	{
		newstr = new WCHAR[wcslen(oldstr) + 1];
	}

	if(newstr)
	{
		wcscpy(newstr, oldstr);
	}

	return newstr;
}


////////////////////////////////////////////////////////////////
// WSTRFree()
//
// Free a Wide String 
//
////////////////////////////////////////////////////////////////
void WSTRFree(WCHAR		*c, 
			  IMalloc	*pmem)
{
	if(c == NULL)
	{
		return;
	}

	if(pmem)
	{
		pmem->Free(c);
	}
	else
	{
		delete c;
	}
}


////////////////////////////////////////////////////////////////
// IAClone()
//
// Clone an OPCITEMATTRIBUTES 
// This is similar to IAGet() in item.cpp
//
////////////////////////////////////////////////////////////////
void IAClone(OPCITEMATTRIBUTES			*newIA, 
			 const OPCITEMATTRIBUTES	*oldIA, 
			 IMalloc					*pmem)
{
	// Note Blob and EUInfo not supported at present

	// the easy stuff...
	//
	newIA->bActive				= oldIA->bActive;
	newIA->hServer				= oldIA->hServer;
	newIA->hClient				= oldIA->hClient;
	newIA->dwAccessRights		= oldIA->dwAccessRights;
	newIA->dwBlobSize			= 0;	// not supported
	newIA->pBlob				= NULL;
	newIA->vtRequestedDataType	= oldIA->vtRequestedDataType;
	newIA->vtCanonicalDataType	= oldIA->vtCanonicalDataType;
	newIA->dwEUType				= OPC_NOENUM;		// not supported
	newIA->vEUInfo.vt			= VT_EMPTY;

	// strings...
	//
	newIA->szAccessPath = WSTRClone(oldIA->szAccessPath, pmem);
	if (NULL == newIA->szAccessPath)
	{
		// return some kind of error?
	}
	newIA->szItemID = WSTRClone(oldIA->szItemID, pmem);
	if (NULL == newIA->szItemID)
	{
		// return some kind of error?
	}
}


////////////////////////////////////////////////////////////////
// IAFree()
//
// Free the contents of an OPCITEMATTRIBUTES 
// (Caller must Free IA itself)
//
////////////////////////////////////////////////////////////////
void IAFree(OPCITEMATTRIBUTES	*pIA, 
			IMalloc				*pmem)
{
	if(NULL == pIA)
	{
		return;
	}

	// Note Blob and EUInfo not supported at present
	WSTRFree(pIA->szAccessPath, pmem);
	WSTRFree(pIA->szItemID, pmem);
}


////////////////////////////////////////////////////////////////
// LogEventMessage()
//
// Used to send a message to Event Viewer
//
////////////////////////////////////////////////////////////////
BOOL LogEventMessage(LPCTSTR		ptcMessage, 
					 LPVOID			pData,			/* = NULL */
					 DWORD			dwDataSize,		/* = 0 */
					 DWORD			dwEventID,		/* = 0 */
					 WORD			wType)			/* = EVENTLOG_INFORMATION_TYPE */
					 
{
	BOOL	bRetVal;


	if (NULL == ptcMessage)
	{
		return FALSE;
	}

	// Get the handle for Win32 Event Logging
	//
	if (NULL == g_hEventSource)
	{
		RegisterEventService();

		if (NULL == g_hEventSource)
		{
			return FALSE;
		}
	}

	bRetVal = ReportEvent(g_hEventSource,
						  EVENTLOG_INFORMATION_TYPE,
						  wType,
						  dwEventID,
						  NULL,
						  1,
						  dwDataSize,
						  &ptcMessage,
						  pData);

	return bRetVal;
}


////////////////////////////////////////////////////////////////
// RegisterEventService()
//
// Used to send a message to Event Viewer
//
////////////////////////////////////////////////////////////////
void RegisterEventService()
{
	// Get the handle for Win32 Event Logging
	//
	if (NULL == g_hEventSource)
	{
		g_hEventSource = RegisterEventSource(NULL, "OPCNIO");
	}
}


////////////////////////////////////////////////////////////////
// DeResterEventService()
//
// Used to send a message to Event Viewer
//
////////////////////////////////////////////////////////////////
void DeRegisterEventService()
{
	if (NULL == g_hEventSource)
	{
		return;
	}

	DeregisterEventSource(g_hEventSource);
}


//---(FUNCTION)---------------------------------------------------------------
//
// @func	HRESULT | NewArray | Internal utility function to create an array
//			of VARIANTs.
//
// @parm	unsigned long | lElements | Number of elements in array
//
// @parm	SAFEARRAY** | ppsaArray | Pointer to a pointer to the SAFEARRAY
//
// @rdesc	Returns one of the following:
//
// @flag	S_OK | Success
// @flag	E_OUTOFMEMORY | Memory could not be allocated for an operation.
//
HRESULT NewArray(unsigned long lElements, SAFEARRAY ** ppsaArray)
{
	SAFEARRAY		*psaArrayLocal;
	SAFEARRAYBOUND	rgsabound[1];	// Bounds for array (one dimension)


	rgsabound[0].lLbound = 0;			// Lower bound
	rgsabound[0].cElements = lElements;	// Number of elements

	psaArrayLocal = SafeArrayCreate(VT_VARIANT, 1, rgsabound);
	if(psaArrayLocal == NULL)
	{
		return(E_OUTOFMEMORY);
	}

	*ppsaArray = psaArrayLocal;

	return(S_OK);
}

⌨️ 快捷键说明

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