用VC开发的客户端,现在只能在rslink程序运行的时候才能对OPC服务器进行访问,想改成不用启动RSlink就能直接对OPC服务器进行访问的方式,就好像直接能访问服务器的IP或者计算机名字一样。
下面是小弟参考网上写的程序,但是程序执行到hRet=CLSIDFromProgID(ProgID,&OPCCLSID);处就出错了,在调用
hRet=ConnectToServer(L"RSLinx OPC Server",TRUE,&ppUnknown);//RSLinx OPC Server时也是失败的,各位大侠给小弟指点一下哪里错了吧,十分感谢啊!!!
HRESULT CHookDetectDlg::ConnectToServer(/*in */LPOLESTR ProgID,/*in*/ BOOL IsRemote,/*out */ IUnknown **ppUnknown)
{
CLSID OPCCLSID;
hRet=CLSIDFromProgID(ProgID,&OPCCLSID);
if(hRet!=S_OK)
{
fp2=fopen("log.txt","at+");
str5="OPC convert failed";
fprintf(fp2,"%s,%s\n",str4,str5);
fclose(fp2);
}
//++++++++++++++++++++++++++++++++++++++++++//
if(IsRemote)
{
//opc和服务器不在同一天计算机上T2OLE("ServerComouter");
COSERVERINFO ServerInfo;
ServerInfo.pwszName=L"192.168.1.30";
memset(&ServerInfo,0,sizeof(ServerInfo));
MULTI_QI qi[1];
memset(qi,0,sizeof(qi));
qi[0].pIID=&IID_IUnknown;
hRet=CoCreateInstanceEx(OPCCLSID,NULL,CLSCTX_REMOTE_SERVER,
&ServerInfo,1,qi);
*ppUnknown=qi[0].pItf;
// MessageBox("2");
}
else //opc服务器和opc客户在同一台计算机上
{
hRet=CoCreateInstance(OPCCLSID,NULL,CLSCTX_LOCAL_SERVER,IID_IUnknown,
(void **)ppUnknown);
}
return hRet;
} |