有专门的本地连接与远程连接的代码,我有C++BUILDER的代码,DELPHI很相近,但有不太同,
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);
}
}