OPC数据读写之订阅方式(Subscription)与COM可连接对象的应用分析

 HRESULT hr;
 //建立连接过程
 IConnectionPointContainer * pIConnectPointContainer = NULL;
 IConnectionPoint * pIConnectPoint = NULL;
 //第一步:
 hr = pIItemMgt->QueryInterface(IID_IConnectionPointContainer,
  (void**)&pIConnectPointContainer);
 if( FAILED(hr) ) //如果调用不成功,则该对象不支持出接口
 {
  //interface IConnectionPointContainer non disponible
  TCHAR tchTemp[128] = "";
  wsprintf(tchTemp, "查找 IID_IConnectionPointContainer 接口失败,错误代码:0x%0x", hr);
  MessageBox(NULL, tchTemp, tchTemp, MB_OK); 
  return FALSE;
 }
 else if( SUCCEEDED(hr) )
 {
  TCHAR tchTemp[128] = "";
  wsprintf(tchTemp, "查找 IID_IConnectionPointContainer 可连接对象接口成功");
  MessageBox(NULL, tchTemp,
   "Error SOPCC_SetDataChangeNotify()",
   MB_OK|MB_ICONINFORMATION); 
 }
 
 //第二步:
 // demande d'un pointeur sur l'interface IConnectionPoint
 hr = pIConnectPointContainer->FindConnectionPoint(IID_IOPCDataCallback,
  &pIConnectPoint);
 if( FAILED(hr) ) //表明对象不支持出接口 IOPCDataCallback 
 {
  TCHAR tchTemp[128] = "";
  wsprintf(tchTemp,
   "查找服务器对象的连接点 IConnectionPoint 接口失败/n错误代码:0x%0xpIConnectPoint: 0x%0x",
   hr, pIConnectPoint);
  MessageBox(NULL, tchTemp, "Error SOPCC_SetDataChangeNotify()", MB_OK|MB_ICONERROR); 
 }
 else if( SUCCEEDED(hr) )
 {
  TCHAR tchTemp[128] = "";
  wsprintf(tchTemp, "查找服务器对象的连接点 IConnectionPoint 接口成功");
  MessageBox(NULL, tchTemp, "Error SOPCC_SetDataChangeNotify()", MB_OK|MB_ICONINFORMATION);
 }
 //不管调用是否成功,都执行下面的语句,我们以后不再使用它
 pIConnectPointContainer->Release();
 if( NULL == pIConnectPoint )
 {
  TCHAR tchTemp[128] = "";
  wsprintf(tchTemp, "服务器不支持 IOPCDataCallback 接口,错误代码:0x%0x", hr);
  MessageBox(NULL, tchTemp, "Error SOPCC_SetDataChangeNotify()", MB_OK|MB_ICONERROR); 
  return FALSE;
 }

 //第三步: 服务器连接点对象建立与接收器对象的连接
 /*客户需要保存Advise的第二个参数的返回值(该连接的标识),
 断开连接时会用到该值*/
 CCallback  * callback;
 hr = pIConnectPoint->Advise(callback, 0);
 if( FAILED(hr) )
 {
  TCHAR tchTemp[128] = "";
  wsprintf(tchTemp, "接收器与连接对象建立连接失败,错误代码:0x%0x", hr);
  MessageBox(NULL, tchTemp, "Error SOPCC_SetDataChangeNotify()", MB_OK|MB_ICONERROR);   
 }
 else if( SUCCEEDED(hr) )
 {
  TCHAR tchTemp[128] = "";
  wsprintf(tchTemp, "接收器与连接对象建立连接成功");
  MessageBox(NULL, tchTemp, "Error SOPCC_SetDataChangeNotify()", MB_OK|MB_ICONINFORMATION);  
 }

 

Notice:
IID_IOPCDataCallback is a callback interface for the OPCGroup object
IID_IOPCShutdown is a callback interface for the OPCServer object

 

 

你可能感兴趣的:(object,服务器,null,callback,interface)