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

📄 opcvariantpack.cpp

📁 基于Intellution开发包的开发的OPC服务器
💻 CPP
字号:
// OPCVariantPack.cpp
//
//  This file contains utility functions which
//  the server and client can use to 
//  'pack' or 'Marshall' VARIANTS into the HGLOBAL 
//  used by the IDataObject.
//
//  (c) COPYRIGHT 1996-1998, INTELLUTION INC.
//  ALL RIGHTS RESERVED
//
//
//	Functions defined in this module:
//
//			OPCVariantPack()
//			OPCVariantSize()
//			OPCVariantUnPack()
//
//
//
// Note - this logic currently supports only numeric and VT_BSTR 
// data types which are those most commonly used.
// Arrays are not currently supported (although they should be).
//
// 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 "OPC10.h"
#include "OPCVariantPack.h"


////////////////////////////////////////////////////////////////
// OPCVariantPack()
//
// Used by Server
// 'Marshalls' the Variant into the 'HGLOBAL'.
//
// Returns the size of the marshalled data
//
////////////////////////////////////////////////////////////////
int	OPCVariantPack(char		*dest, 
				   VARIANT	*vp)
{
	int				extra = 0;
	unsigned short	*bp;
	int				len;


	// This version has limited functionality
	// It handles all numeric types (which have no 'extra')
	// plus BSTR. It does NOT handle arrays at this time.
	//

	// Copy the 'core' of the variant
	// Note that for anything with a 'pointer'
	// the pointer in the 'dest' memory will be meaningless
	// (See Unpack below)
	//
	memcpy(dest, vp, sizeof(VARIANT));

	// determine where extra (if any) will go
	//
	dest += sizeof(VARIANT);

	switch(vp->vt)
	{
	case VT_BSTR:
		bp = vp->bstrVal;
		bp -= 2;
		len = *bp;			// byte count of BSTR (incl NUL)
		extra = len + 4;	// Total bytes includes DWORD
		memcpy(dest, bp, extra);
		break;

	default:
		extra = 0;
		break;
	}

	return sizeof(VARIANT) + extra;
}


////////////////////////////////////////////////////////////////
// OPCVariantSize()
//
// Return the number of bytes needed to marshall a variant.
// (Must be the same as that returned by OPCVariantPack)
//
////////////////////////////////////////////////////////////////
int	OPCVariantSize(VARIANT *vp)
{
	int				extra = 0;
	unsigned short	*bp;
	int				len;


	// This version has limited functionality
	// It handles all numeric types (which have no 'extra')
	// plus BSTR. It does NOT handle arrays at this time.
	//
	switch(vp->vt)
	{
	case VT_BSTR:
		bp = vp->bstrVal;
		bp -= 2;
		len = *bp;			// byte count of BSTR (incl NUL)
		extra = len + 4;	// Total Bytes includes DWORD
		break;

	default:
		extra = 0;
		break;
	}

	return sizeof(VARIANT) + extra;
}


////////////////////////////////////////////////////////////////
// OPCVariantUnPack()
//
// Used by Client.
//
// 'Marshalls' the Variant OUT of the 'HGLOBAL'. The passed 
// variant is assumed to be EMPTY.
// (see VariantInit or VariantClear)
//
////////////////////////////////////////////////////////////////
void OPCVariantUnpack(VARIANT	*vp, 
					  char		*src)
{
	unsigned short *bp;

	
	// copy the 'body' of the variant
	// This works OK for any of the numeric values 
	// within the union itself
	//
	memcpy(vp, src, sizeof(VARIANT));		
	
	// Check for special cases (things with pointers)
	//
	switch(vp->vt)
	{
	case VT_BSTR:
		DWORD strlen;
		// invalidate the pointer 
		// which is from the server's context
		//
		vp->bstrVal = NULL;

		src += sizeof(VARIANT);		// skip past the Variant 'core'
		bp = (unsigned short *) src;
		bp += 2;		// ptr to string itself (which is WCHAR)
		strlen = SysStringLen(((unsigned short*) bp) );
		vp->bstrVal = SysAllocStringLen( (unsigned short*) bp, strlen);
		break;

	default:
		break;
	}
}

⌨️ 快捷键说明

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