static public void ListHDAServers(String node) { IDiscovery discovery = new OpcCom.ServerEnumerator(); Opc.Server[] servers; try { if (string.IsNullOrEmpty(node)) { servers = discovery.GetAvailableServers(Specification.COM_HDA_10); } else { servers = discovery.GetAvailableServers(Specification.COM_HDA_10, node, null); } foreach (Opc.Server s in servers) { Console.WriteLine(s.Name); } } catch (System.Runtime.InteropServices.ExternalException e) { Console.WriteLine("Error getting available servers: {0}", e.Message); } }
public static object Connect(URL url, ConnectData connectData) { string text = url.Path; string text2 = null; int num = url.Path.LastIndexOf('/'); if (num >= 0) { text = url.Path.Substring(0, num); text2 = url.Path.Substring(num + 1); } Guid guid; if (text2 == null) { guid = new ServerEnumerator().CLSIDFromProgID(text, url.HostName, connectData); if (guid == Guid.Empty) { try { guid = new Guid(text); } catch { throw new ConnectFailedException(text); } } } else { try { guid = new Guid(text2); } catch { throw new ConnectFailedException(text2); } } NetworkCredential credential = connectData?.GetCredential(null, null); if (connectData == null || connectData.LicenseKey == null) { try { return(Interop.CreateInstance(guid, url.HostName, credential)); } catch (Exception e) { throw new ConnectFailedException(e); } } try { return(Interop.CreateInstanceWithLicenseKey(guid, url.HostName, credential, connectData.LicenseKey)); } catch (Exception e2) { throw new ConnectFailedException(e2); } }
/// <summary> /// Connects to the specified COM server server. /// </summary> public static object Connect(Opc.URL url, Opc.ConnectData connectData) { // parse path to find prog id and clsid. string progID = url.Path; string clsid = null; int index = url.Path.LastIndexOf('/'); if (index >= 0) { progID = url.Path.Substring(0, index); clsid = url.Path.Substring(index + 1); } // look up prog id if clsid not specified in the url. Guid guid; if (clsid == null) { // use OpcEnum to lookup the prog id. guid = new ServerEnumerator().CLSIDFromProgID(progID, url.HostName, connectData); // check if prog id is actually a clsid string. if (guid == Guid.Empty) { try { guid = new Guid(progID); } catch { throw new Opc.ConnectFailedException(progID); } } } // convert clsid string to a guid. else { try { guid = new Guid(clsid); } catch { throw new Opc.ConnectFailedException(clsid); } } // get the credentials. NetworkCredential credentials = (connectData != null)?connectData.GetCredential(null, null):null; // instantiate the server using CoCreateInstanceEx. if (connectData == null || connectData.LicenseKey == null) { try { return(OpcCom.Interop.CreateInstance(guid, url.HostName, credentials)); } catch (Exception e) { throw new Opc.ConnectFailedException(e); } } // instantiate the server using IClassFactory2. else { try { return(OpcCom.Interop.CreateInstanceWithLicenseKey(guid, url.HostName, credentials, connectData.LicenseKey)); } catch (Exception e) { throw new Opc.ConnectFailedException(e); } } }