/*---------------------------------------------------*
* OPC客户端BCB6调用子程序 OPCClient.h V1.5 *
* 2009.5.9 *
*---------------------------------------------------*/
#ifndef OPC_CLIENT_CLASS
#define OPC_CLIENT_CLASS
#include <ComObj.hpp> // OleCheck
#include <atlbase.h> // CComPtr
#include <opcda_i.c>
#include <OPCDA.h>
#include <ComCtrls.hpp>
bool Check_OPCID(WideString id)
{
bool valid=true;
try
{
GUID opc_guid=ProgIDToClassID(id);
}
catch(...)
{
valid=false;
}
return(valid);
}
bool ConnectOPCServer(WideString id, // OPC Server ID
WideString ip, // IP,空为本地
IUnknown * & opcServer)
{ // 支持远程联接OPCServer
try
{
CLSID clsid;
OleCheck(CLSIDFromProgID(id,&clsid));
if (ip.Length()!=0)
{ // 远程OPC
COSERVERINFO ServerInfo={0,ip.c_bstr(),0,0};
MULTI_QI MQI={&IID_IOPCServer,NULL,0};
OleCheck(CoCreateInstanceEx(clsid,
NULL,
CLSCTX_REMOTE_SERVER,
&ServerInfo,
1,
&MQI));
opcServer=MQI.pItf;
}
else
{ // 本地OPC
OleCheck(CoCreateInstance(clsid,
NULL,
CLSCTX_LOCAL_SERVER,
IID_IOPCServer,
(void **)&opcServer));
}
return(true);
}
catch(...)
{
opcServer=NULL;
return(false);
}
}
// 部分片断