public static List <OPCServer> BrowseServers(string hostname, NetworkCredential networkCredential = null, bool DAVersion3 = false) { List <OPCServer> servers = new List <OPCServer>(); Guid clsidcat; if (DAVersion3) { clsidcat = new Guid("{CC603642-66D7-48F1-B69A-B625E73652D7}"); //OPC Data Access Servers Version 3. } else { clsidcat = new Guid("{63D5F432-CFE4-11D1-B2C8-0060083BA1FB}"); //OPC Data Access Servers Version 2. } Guid clsidenum = new Guid("{13486D51-4821-11D2-A494-3CB306C10000}"); //OPCEnum.exe try { IOPCServerList2 serverList = (IOPCServerList2)Internal.Interop.CreateInstance(clsidenum, hostname, networkCredential); IOPCEnumGUID pIOPCEnumGuid; serverList.EnumClassesOfCategories(1, ref clsidcat, 0, ref clsidcat, out pIOPCEnumGuid); string pszProgID; string pszUserType; string pszVerIndProgID; Guid guid = new Guid(); int nServerCnt = 0; uint iRetSvr; pIOPCEnumGuid.Next(1, out guid, out iRetSvr); while (iRetSvr != 0) { nServerCnt++; serverList.GetClassDetails(ref guid, out pszProgID, out pszUserType, out pszVerIndProgID); OPCServer server = new OPCServer() { Guid = guid, ProgId = pszProgID, HostName = hostname, }; servers.Add(server); pIOPCEnumGuid.Next(1, out guid, out iRetSvr); } Internal.Interop.ReleaseServer(serverList); } catch (System.Exception ex) { Console.Out.WriteLine(ex.Message); } return(servers); }
public static OPCServer FindServerByProgID(string progID, string hostname) { foreach (OPCServer srv in OPCServer.BrowseServers(hostname)) { if (srv.ProgId.ToUpper().Contains(progID.ToUpper())) { return(srv); } } return(null); }
public void AdviseOnServer(OPCServer server, OPCItem item) { if (m_pItemMgt == null) { return; } try { IConnectionPointContainer pCPC; pCPC = (IConnectionPointContainer)m_pItemMgt; Guid riid = typeof(IOPCDataCallback).GUID; //IEnumConnectionPoints ppEnum; //pCPC.EnumConnectionPoints(out ppEnum); pCPC.FindConnectionPoint(ref riid, out m_pDataCallback); //Если ранее была активирована подписка, то отменить ее if (m_dwCookie != 0) { m_pDataCallback.Unadvise(m_dwCookie); } DataCallback m_pSink = new DataCallback(item); m_pDataCallback.Advise(m_pSink, out m_dwCookie); } catch (COMException ex) { string msg; //Запрашиваем у сервера текст ошибки, соответствующий текущему HRESULT server.Server.GetErrorString(ex.HResult, 2, out msg); Console.Out.WriteLine(msg, "Ошибка"); } catch (ServerException ex) { string msg; //Запрашиваем у сервера текст ошибки, соответствующий текущему HRESULT server.Server.GetErrorString(ex.HResult, 2, out msg); Console.Out.WriteLine(msg, "Ошибка"); } }