📄 impiopcserver.cpp
字号:
// ImpIOPCServer.cpp
//
// This file contains the implementation of
// the IOPCServer interface for the XXX server.
//
// (c) COPYRIGHT 1996-1998, INTELLUTION INC.
// ALL RIGHTS RESERVED
//
//
// Functions defined in this module:
//
// CImpIOPCServer::CImpIOPCServer()
// CImpIOPCServer::~CImpIOPCServer()
// CImpIOPCServer::AddRef()
// CImpIOPCServer::Release()
// CImpIOPCServer::QueryInterface()
// CImpIOPCSyncIO::GetStatus()
// CImpIOPCSyncIO::GetErrorString()
// CImpIOPCServer::AddGroup()
// CImpIOPCServer::GetGroupByName()
// CImpIOPCServer::RemoveGroup()
// CImpIOPCServer::CreateGroupEnumerator()
//
//
//
// 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 <afxconv.h> // used for ATL conversion functions
#include "OPC.h"
#include "OPCERROR.h"
#include "OPCVersionInfo.h"
/////////////////////////////////////////////////////////////////////////////
// External global variables
/////////////////////////////////////////////////////////////////////////////
extern CString g_strDefaultGeneratedGroupName;
extern DWORD g_dwWaitPeriod;
extern TCHAR g_tszAcronym[];
/////////////////////////////////////////////////////////////////////////////
// Vendor information
/////////////////////////////////////////////////////////////////////////////
static WCHAR g_pwcVendorInfo[100];
/////////////////////////////////////////////////////////////////////////////
// Constructor /Destructor functions
//
////////////////////////////////////////////////////////////////
// CImpIOPCServer()
//
// Constructor for this Interface
//
////////////////////////////////////////////////////////////////
CImpIOPCServer::CImpIOPCServer(LPUNKNOWN pUnkOuter)
{
m_pUnkOuter = pUnkOuter;
m_pParentServer = (COPCDrvServer *)pUnkOuter;
// Format the vendor info string show the version of the server.
//
CString szVendorInfo;
WCHAR *wcTemp;
szVendorInfo.Format("%sOPC - Intellution Inc. OPC Server v%d.%d.%d",
g_tszAcronym, MAJOR_VERSION, MINOR_VERSION, BUILD_NUMBER);
USES_CONVERSION;
wcTemp = A2W(szVendorInfo.GetBuffer(szVendorInfo.GetLength() + 1));
wcscpy(g_pwcVendorInfo, wcTemp);
szVendorInfo.ReleaseBuffer();
}
////////////////////////////////////////////////////////////////
// ~CImpIOPCServer()
//
// Destructor for this Interface
//
////////////////////////////////////////////////////////////////
CImpIOPCServer::~CImpIOPCServer(void)
{
m_pParentServer->m_pCImpIOPCServer = NULL;
}
/////////////////////////////////////////////////////////////////////////////
// IUnknown functions Delegate to Parent, but also perform internal reference
// checking and cleanup.
//
////////////////////////////////////////////////////////////////
// AddRef()
//
////////////////////////////////////////////////////////////////
STDMETHODIMP_(ULONG) CImpIOPCServer::AddRef(void)
{
return m_pUnkOuter->AddRef();
}
////////////////////////////////////////////////////////////////
// Release()
//
////////////////////////////////////////////////////////////////
STDMETHODIMP_(ULONG) CImpIOPCServer::Release(void)
{
return m_pUnkOuter->Release();
}
////////////////////////////////////////////////////////////////
// QueryInterface()
//
////////////////////////////////////////////////////////////////
STDMETHODIMP CImpIOPCServer::QueryInterface(REFIID iid,
LPVOID *ppInterface)
{
return m_pUnkOuter->QueryInterface(iid, ppInterface);
}
/////////////////////////////////////////////////////////////////////////////
// CImpIOPCServer (IOPCServer) interface functions
//
////////////////////////////////////////////////////////////////
// CImpIOPCServer::GetStatus()
//
// This function fills in the OPCSERVERSTATUS structure that
// was passed in.
//
// Returns:
// HRESULT - S_OK if the function succeeded.
// - E_FAIL if the function failed.
//
////////////////////////////////////////////////////////////////
STDMETHODIMP CImpIOPCServer::GetStatus(OPCSERVERSTATUS **ppServerStatus)
{
OPCSERVERSTATUS *pServerStatus = NULL;
// Make sure that we got a good pointer
//
if (NULL == ppServerStatus)
{
return E_FAIL;
}
// Allocate some memory for the struct
//
pServerStatus = (OPCSERVERSTATUS *)pIMalloc->Alloc(sizeof(OPCSERVERSTATUS));
if(NULL == pServerStatus)
{
*ppServerStatus = NULL;
return E_FAIL;
}
// Get the vendor info
//
pServerStatus->szVendorInfo = WSTRClone(g_pwcVendorInfo, pIMalloc);
if (NULL == pServerStatus->szVendorInfo)
{
pIMalloc->Free(pServerStatus);
*ppServerStatus = NULL;
return E_FAIL;
}
// Get the start and update times
//
pServerStatus->ftStartTime = m_pParentServer->m_ftServerStartTime;
pServerStatus->ftLastUpdateTime = m_pParentServer->m_ftLastUpdate;
CoFileTimeNow(&pServerStatus->ftCurrentTime);
// Get the general server state info
//
pServerStatus->dwServerState = m_pParentServer->m_ServerState;
pServerStatus->dwGroupCount = m_pParentServer->GetNumGroupHandles();
pServerStatus->dwBandWidth = m_pParentServer->m_dwBandwidth;
// Get the server build info
//
pServerStatus->wMajorVersion = MAJOR_VERSION;
pServerStatus->wMinorVersion = MINOR_VERSION;
pServerStatus->wBuildNumber = BUILD_NUMBER;
pServerStatus->wReserved = 42;
// return the result (if any) and the error
//
*ppServerStatus = pServerStatus;
return S_OK;
}
////////////////////////////////////////////////////////////////
// CImpIOPCServer::GetErrorString()
//
// For server specific error codes we need to return a user
// displayable string in the user's language.
// The easiest way to do this is to put the strings in the RC
// file and use LoadString.
//
// Currently, the LCID is ignored.
//
// Returns:
// HRESULT - S_OK if the function succeeded
// - E_FAIL if the function failed.
// - E_OUTOFMEMORY if WSTRClone() failed.
// - E_INVALIDARG if we got a bad pointer.
//
////////////////////////////////////////////////////////////////
STDMETHODIMP CImpIOPCServer::GetErrorString(HRESULT hr,
LCID locale,
LPWSTR *ppString)
{
static WCHAR *pwcErrorString = NULL;
// Make sure we didn't get any bad parameters.
//
if (NULL == ppString)
{
return E_INVALIDARG;
}
switch(hr)
{
// Standard COM HRESULT values
//
case S_OK:
pwcErrorString = L"Operation Successful";
break;
case E_INVALIDARG:
pwcErrorString = L"The value of one or more parameters was not valid.";
break;
case E_OUTOFMEMORY:
pwcErrorString = L"Not enough memory to complete the requested operation.";
break;
case E_FAIL:
pwcErrorString = L"Error. Operation failed.";
break;
case E_NOINTERFACE:
pwcErrorString = L"Interface not supported.";
break;
case E_POINTER:
pwcErrorString = L"Invalid or NULL pointer.";
break;
case E_NOTIMPL:
pwcErrorString = L"Operation not implemented.";
break;
// OPC specific HRESULT error values
//
case OPC_E_INVALIDITEMID:
pwcErrorString = L"Item ID was invalid and not found.";
break;
case OPC_E_INVALIDHANDLE:
pwcErrorString = L"The server handle is invalid.";
break;
case OPC_E_DUPLICATENAME:
pwcErrorString = L"A group is already defined with that name.";
break;
case OPC_E_BADTYPE:
pwcErrorString = L"Write operation failed due to a bad type.";
break;
case OPC_E_BADRIGHTS:
pwcErrorString = L"The Items AccessRights do not allow the operation.";
break;
case OPC_E_INVALIDFILTER:
pwcErrorString = L"The Filter string was not valid.";
break;
case OLE_E_NOCONNECTION:
pwcErrorString = L"No previous connection.";
break;
case OPC_E_PUBLIC:
pwcErrorString = L"The requested operation cannot be done on a public group.";
break;
case OPC_E_RANGE:
pwcErrorString = L"The value passed to WRITE was out of range.";
break;
// OPC specific HRESULT success values
//
case OPC_S_UNSUPPORTEDRATE:
pwcErrorString = L"The server does not support the requested data rate, but will use the closest available rate.";
break;
case OPC_S_CLAMP:
pwcErrorString = L"The value passed to WRITE was accepted, but was clamped.";
break;
// Miscellaneous HRESULT return codes.
//
case CONNECT_E_ADVISELIMIT:
pwcErrorString = L"Connection limit exceeded. Request cancelled.";
break;
case CONNECT_E_NOCONNECTION:
pwcErrorString = L"Connection not established.";
break;
case CONNECT_E_CANNOTCONNECT:
pwcErrorString = L"Cannot connect to interface.";
break;
case CLASS_E_NOAGGREGATION:
pwcErrorString = L"Aggregation is not supported.";
break;
case CLASS_E_CLASSNOTAVAILABLE:
pwcErrorString = L"Class not available.";
break;
case DV_E_FORMATETC:
pwcErrorString = L"Bad stream format.";
break;
default:
*ppString = NULL;
return E_FAIL;
break;
}
*ppString = WSTRClone(pwcErrorString, pIMalloc);
if (NULL == *ppString)
{
return E_OUTOFMEMORY;
}
return S_OK;
}
////////////////////////////////////////////////////////////////
// CImpIOPCServer::AddGroup()
//
// This function creates a new group of the specified name on
// this server.
// Note TimeBias, Deadband and LCID are not implemented
//
// Returns:
// HRESULT - S_OK if the function succeeded
// - E_FAIL if the function failed.
// - OPC_E_DUPLICATENAME if a group already contains
// the name passed to us.
// - E_INVALIDARG if a parameter passed is invalid,
// such as a bad pointer.
// - E_OUTOFMEMORY if a memory allocator failed.
//
////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -