/// <summary> /// Reads the guids from the enumerator. /// </summary> private static Guid[] ReadClasses(IOPCEnumGUID enumerator) { ArrayList guids = new ArrayList(); int fetched = 0; Guid[] buffer = new Guid[10]; //IntPtr rgelt = new IntPtr(); do { try { //enumerator.Next(buffer.Length, rgelt, out fetched); enumerator.Next(buffer.Length, buffer, out fetched); for (int ii = 0; ii < fetched; ii++) { guids.Add(buffer[ii]); } } catch { break; } }while (fetched > 0); return((Guid[])guids.ToArray(typeof(Guid))); }
private void Dispose(bool disposing) { if (ComServer != null) { Marshal.ReleaseComObject(ComServer); ComServer = null; } }
internal OpcEnumGuid(IOPCEnumGUID comServer) { if (comServer == null) { throw new ArgumentNullException("comServer"); } ComServer = comServer; }
public List <Guid> GetClsIds() { var categories = new[] { new Guid("63D5F432-CFE4-11d1-B2C8-0060083BA1FB"), // DA 2.xxx new Guid("CC603642-66D7-48f1-B69A-B625E73652D7"), // DA 3.0 }; IOPCEnumGUID enumerator = null; _server.EnumClassesOfCategories(2, categories, 0, null, out enumerator); var guids = new List <Guid>(); try { var buffer = new Uri[10]; var guidSize = Marshal.SizeOf <Guid>(); int fetched = 0; do { var ptr = Marshal.AllocCoTaskMem(guidSize * buffer.Length); try { enumerator.Next(buffer.Length, ptr, out fetched); if (fetched > 0) { var p = ptr; for (int i = 0; i < fetched; i++, p += guidSize) { var v = Marshal.PtrToStructure <Guid>(p); Marshal.DestroyStructure <Guid>(p); guids.Add(v); } } } finally { Marshal.FreeCoTaskMem(ptr); } }while (fetched > 0); } catch { } finally { ComUtils.ReleaseServer(enumerator); } return(guids); }
/// <summary> /// Returns a list of servers that support the specified specification. /// </summary> public Uri[] GetAvailableServers(params Specification[] specifications) { // enumerate servers on specified machine. try { // convert the interface version to a guid. Guid[] catids = new Guid[specifications.Length]; for (int ii = 0; ii < catids.Length; ii++) { catids[ii] = new Guid(specifications[ii].Id); } // get list of servers in the specified specification. IOPCEnumGUID enumerator = null; m_server.EnumClassesOfCategories( catids.Length, catids, 0, null, out enumerator); // read clsids. List <Guid> clsids = ReadClasses(enumerator); // release enumerator object. ComUtils.ReleaseServer(enumerator); enumerator = null; // fetch class descriptions. Uri[] uris = new Uri[clsids.Count]; for (int ii = 0; ii < uris.Length; ii++) { uris[ii] = CreateUri(clsids[ii]); } return(uris); } catch (Exception e) { throw ServiceResultException.Create(StatusCodes.BadCommunicationError, e, "Could not enumerate COM servers."); } }
/// <summary> /// Reads the guids from the enumerator. /// </summary> private List <Guid> ReadClasses(IOPCEnumGUID enumerator) { List <Guid> guids = new List <Guid>(); int fetched = 0; Guid[] buffer = new Guid[10]; do { try { IntPtr pGuids = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Guid)) * buffer.Length); try { enumerator.Next(buffer.Length, pGuids, out fetched); if (fetched > 0) { IntPtr pos = pGuids; for (int ii = 0; ii < fetched; ii++) { buffer[ii] = (Guid)Marshal.PtrToStructure(pos, typeof(Guid)); pos = (IntPtr)(pos.ToInt64() + Marshal.SizeOf(typeof(Guid))); guids.Add(buffer[ii]); } } } finally { Marshal.FreeCoTaskMem(pGuids); } } catch { break; } }while (fetched > 0); return(guids); }
//====================================================================== // Private Methods /// <summary> /// Reads the guids from the enumerator. /// </summary> private Guid[] ReadClasses(IOPCEnumGUID enumerator) { ArrayList guids = new ArrayList(); int fetched = 0; int count = 10; // create buffer. IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Guid)) * count); try { do { try { enumerator.Next(count, buffer, out fetched); IntPtr pPos = buffer; for (int ii = 0; ii < fetched; ii++) { Guid guid = (Guid)Marshal.PtrToStructure(pPos, typeof(Guid)); guids.Add(guid); pPos = (IntPtr)(pPos.ToInt64() + Marshal.SizeOf(typeof(Guid))); } } catch { break; } }while (fetched > 0); return((Guid[])guids.ToArray(typeof(Guid))); } finally { Marshal.FreeCoTaskMem(buffer); } }
// Token: 0x06000168 RID: 360 RVA: 0x00002F8C File Offset: 0x0000118C public ServerDescription[] GetAvailableServers(params Guid[] catids) { ServerDescription[] result; try { IOPCEnumGUID iopcenumGUID = null; this.m_server.EnumClassesOfCategories(catids.Length, catids, 0, null, out iopcenumGUID); List <Guid> list = this.ReadClasses(iopcenumGUID); Utils.ReleaseServer(iopcenumGUID); iopcenumGUID = null; ServerDescription[] array = new ServerDescription[list.Count]; for (int i = 0; i < array.Length; i++) { array[i] = this.ReadServerDetails(list[i]); } result = array; } catch (Exception e) { throw Utils.CreateComException(e, -2147467259, "Could not enumerate COM servers.", new object[0]); } return(result); }
private Guid[] ReadClasses(IOPCEnumGUID enumerator) { ArrayList list = new ArrayList(); int pceltFetched = 0; Guid[] rgelt = new Guid[10]; do { try { enumerator.Next(rgelt.Length, rgelt, out pceltFetched); for (int i = 0; i < pceltFetched; i++) { list.Add(rgelt[i]); } } catch { break; } }while (pceltFetched > 0); return((Guid[])list.ToArray(typeof(Guid))); }
// Token: 0x0600016A RID: 362 RVA: 0x00003054 File Offset: 0x00001254 private List <Guid> ReadClasses(IOPCEnumGUID enumerator) { List <Guid> list = new List <Guid>(); int num = 0; Guid[] array = new Guid[10]; do { try { IntPtr intPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Guid)) * array.Length); try { enumerator.Next(array.Length, intPtr, out num); if (num > 0) { IntPtr ptr = intPtr; for (int i = 0; i < num; i++) { array[i] = (Guid)Marshal.PtrToStructure(ptr, typeof(Guid)); ptr = (IntPtr)(ptr.ToInt64() + (long)Marshal.SizeOf(typeof(Guid))); list.Add(array[i]); } } } finally { Marshal.FreeCoTaskMem(intPtr); } } catch { break; } }while (num > 0); return(list); }
// Token: 0x06000006 RID: 6 RVA: 0x0000236C File Offset: 0x0000136C private Guid[] ReadClasses(IOPCEnumGUID enumerator) { ArrayList arrayList = new ArrayList(); int num = 0; int num2 = 10; IntPtr intPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Guid)) * num2); Guid[] result; try { do { try { enumerator.Next(num2, intPtr, out num); IntPtr ptr = intPtr; for (int i = 0; i < num; i++) { Guid guid = (Guid)Marshal.PtrToStructure(ptr, typeof(Guid)); arrayList.Add(guid); ptr = (IntPtr)(ptr.ToInt64() + (long)Marshal.SizeOf(typeof(Guid))); } } catch { break; } }while (num > 0); result = (Guid[])arrayList.ToArray(typeof(Guid)); } finally { Marshal.FreeCoTaskMem(intPtr); } return(result); }
/// <summary> /// Returns an enumerator to allow to determine available OPC servers. /// </summary> /// <param name="categories">OPC Server categories (OPCDA/OPCHDA etc.)</param> /// <returns>Enumerator to allow to determine available OPC servers.</returns> public IEnumerable <ServerDescription> GetEnumerator(params Guid[] categories) { IEnumGUID enumerator = null; IOPCEnumGUID opcEnumerator = null; try { var tmp = Guid.Empty; if (serverList2 != null) { serverList2.EnumClassesOfCategories( (uint)categories.Length, categories, 0, ref tmp, out opcEnumerator); } else { serverList.EnumClassesOfCategories( (uint)categories.Length, categories, 0, ref tmp, out enumerator); } while (true) { var res = 0; uint fetched = 0; var ids = new Guid[1]; if (opcEnumerator != null) { res = opcEnumerator.Next((uint)ids.Length, ids, out fetched); } else if (enumerator != null) { res = enumerator.Next(1, ids, out fetched); } if (res > 1) { Marshal.ThrowExceptionForHR(res); } if (fetched == 0) { break; } var id = ids[0]; ServerDescription serverDescription; try { string name; string programId; var versionIndependentProgramId = string.Empty; if (serverList2 != null) { serverList2.GetClassDetails(ref id, out programId, out name, out versionIndependentProgramId); } else { serverList.GetClassDetails(ref id, out programId, out name); } serverDescription = new ServerDescription(id, programId, versionIndependentProgramId, name); } catch (Exception e) { serverDescription = new ServerDescription(id, e); } yield return(serverDescription); } } finally { if (enumerator != null) { Marshal.ReleaseComObject(enumerator); } if (opcEnumerator != null) { Marshal.ReleaseComObject(opcEnumerator); } } }
/// <summary> /// Returns a list of servers that support the specified specification on the specified host. /// </summary> public Opc.Server[] GetAvailableServers(Specification specification, string host, ConnectData connectData) { lock (this) { NetworkCredential credentials = (connectData != null)?connectData.GetCredential(null, null):null; // connect to the server. m_server = (IOPCServerList2)OpcCom.Interop.CreateInstance(CLSID, host, credentials); m_host = host; try { ArrayList servers = new ArrayList(); // convert the interface version to a guid. Guid catid = new Guid(specification.ID); // get list of servers in the specified specification. IOPCEnumGUID enumerator = null; m_server.EnumClassesOfCategories( 1, new Guid[] { catid }, 0, null, out enumerator); // read clsids. Guid[] clsids = ReadClasses(enumerator); // release enumerator object. OpcCom.Interop.ReleaseServer(enumerator); enumerator = null; // fetch class descriptions. foreach (Guid clsid in clsids) { Factory factory = new OpcCom.Factory(); try { URL url = CreateUrl(specification, clsid); Opc.Server server = null; if (specification == Specification.COM_DA_30) { server = new Opc.Da.Server(factory, url); } else if (specification == Specification.COM_DA_20) { server = new Opc.Da.Server(factory, url); } else if (specification == Specification.COM_AE_10) { server = new Opc.Ae.Server(factory, url); } else if (specification == Specification.COM_HDA_10) { server = new Opc.Hda.Server(factory, url); } else if (specification == Specification.COM_DX_10) { server = new Opc.Dx.Server(factory, url); } servers.Add(server); } catch (Exception) { // ignore bad clsids. } } return((Opc.Server[])servers.ToArray(typeof(Opc.Server))); } finally { // free the server. OpcCom.Interop.ReleaseServer(m_server); m_server = null; } } }
// Token: 0x06000004 RID: 4 RVA: 0x000020E4 File Offset: 0x000010E4 public Opc.Server[] GetAvailableServers(Specification specification, string host, ConnectData connectData) { Opc.Server[] result; lock (this) { NetworkCredential credential = (connectData != null) ? connectData.GetCredential(null, null) : null; this.m_server = (IOPCServerList2)Interop.CreateInstance(ServerEnumerator.CLSID, host, credential); this.m_host = host; try { ArrayList arrayList = new ArrayList(); Guid guid = new Guid(specification.ID); IOPCEnumGUID iopcenumGUID = null; this.m_server.EnumClassesOfCategories(1, new Guid[] { guid }, 0, null, out iopcenumGUID); Guid[] array = this.ReadClasses(iopcenumGUID); Interop.ReleaseServer(iopcenumGUID); iopcenumGUID = null; foreach (Guid clsid in array) { Factory factory = new Factory(); try { URL url = this.CreateUrl(specification, clsid); Opc.Server value = null; if (specification == Specification.COM_DA_30) { value = new Opc.Da.Server(factory, url); } else if (specification == Specification.COM_DA_20) { value = new Opc.Da.Server(factory, url); } else if (specification == Specification.COM_AE_10) { value = new Opc.Ae.Server(factory, url); } else if (specification == Specification.COM_HDA_10) { value = new Opc.Hda.Server(factory, url); } else if (specification == Specification.COM_DX_10) { value = new Opc.Dx.Server(factory, url); } arrayList.Add(value); } catch (Exception) { } } result = (Opc.Server[])arrayList.ToArray(typeof(Opc.Server)); } finally { Interop.ReleaseServer(this.m_server); this.m_server = null; } } return(result); }
public Opc.Server[] GetAvailableServers(Specification specification, string host, ConnectData connectData) { Opc.Server[] serverArray; lock (this) { NetworkCredential credential = (connectData != null) ? connectData.GetCredential(null, null) : null; this.m_server = (IOPCServerList2)Interop.CreateInstance(CLSID, host, credential); this.m_host = host; try { ArrayList list = new ArrayList(); Guid guid = new Guid(specification.ID); IOPCEnumGUID ppenumClsid = null; Guid[] rgcatidImpl = new Guid[] { guid }; this.m_server.EnumClassesOfCategories(1, rgcatidImpl, 0, null, out ppenumClsid); Guid[] guidArray = this.ReadClasses(ppenumClsid); Interop.ReleaseServer(ppenumClsid); ppenumClsid = null; foreach (Guid guid2 in guidArray) { OpcCom.Factory factory = new OpcCom.Factory(); try { URL url = this.CreateUrl(specification, guid2); Opc.Server server = null; if (specification == Specification.COM_DA_30) { server = new Opc.Da.Server(factory, url); } else if (specification == Specification.COM_DA_20) { server = new Opc.Da.Server(factory, url); } else if (specification == Specification.COM_AE_10) { server = new Opc.Ae.Server(factory, url); } else if (specification == Specification.COM_HDA_10) { server = new Opc.Hda.Server(factory, url); } else if (specification == Specification.COM_DX_10) { server = new Opc.Dx.Server(factory, url); } list.Add(server); } catch (Exception) { } } serverArray = (Opc.Server[])list.ToArray(typeof(Opc.Server)); } finally { Interop.ReleaseServer(this.m_server); this.m_server = null; } } return(serverArray); }
public void EnumClassesOfCategories(int cImplemented, Guid[] rgcatidImpl, int cRequired, Guid[] rgcatidReq, out IOPCEnumGUID ppenumClsid) { var methodName = nameof(IOPCServerList2) + "." + nameof(IOPCServerList2.EnumClassesOfCategories); try { var server = BeginComCall<IOPCServerList2>(methodName, true); server.EnumClassesOfCategories(cImplemented, rgcatidImpl, cRequired, rgcatidReq, out ppenumClsid); } finally { EndComCall(methodName); } }
internal OpcEnumGuid(IOPCEnumGUID comServer) { if (comServer == null) throw new ArgumentNullException("comServer"); ComServer = comServer; }
public void EnumClassesOfCategories(int cImplemented, Guid[] rgcatidImpl, int cRequired, Guid[] rgcatidReq, out IOPCEnumGUID ppenumClsid) { var methodName = nameof(IOPCServerList2) + "." + nameof(IOPCServerList2.EnumClassesOfCategories); try { var server = BeginComCall <IOPCServerList2>(methodName, true); server.EnumClassesOfCategories(cImplemented, rgcatidImpl, cRequired, rgcatidReq, out ppenumClsid); } finally { EndComCall(methodName); } }
/// <summary> /// Reads the guids from the enumerator. /// </summary> private List<Guid> ReadClasses(IOPCEnumGUID enumerator) { List<Guid> guids = new List<Guid>(); int fetched = 0; Guid[] buffer = new Guid[10]; do { try { IntPtr pGuids = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Guid))*buffer.Length); try { enumerator.Next(buffer.Length, pGuids, out fetched); if (fetched > 0) { IntPtr pos = pGuids; for (int ii = 0; ii < fetched; ii++) { buffer[ii] = (Guid)Marshal.PtrToStructure(pos, typeof(Guid)); pos = (IntPtr)(pos.ToInt64() + Marshal.SizeOf(typeof(Guid))); guids.Add(buffer[ii]); } } } finally { Marshal.FreeCoTaskMem(pGuids); } } catch { break; } } while (fetched > 0); return guids; }
/// <summary> /// Return a list of local OPC A&E servers URLs. /// </summary> public static string[] GetLocalAeServers() { IOPCServerList2 m_server = null; // Establish connection to the OPC Server m_server = (IOPCServerList2)Helper.CreateInstance(CLSID, "localhost", null); try { ArrayList servers = new ArrayList(); string[] urls = null; // convert the interface version to a guid. Guid catid = new Guid("58E13251-AC87-11d1-84D5-00608CB8A7E9"); // get list of servers in the specified specification. IOPCEnumGUID enumerator = null; m_server.EnumClassesOfCategories( 1, new Guid[] { catid }, 0, null, out enumerator); // read clsids. Guid[] clsids = ReadClasses(enumerator); enumerator = null; urls = new string[clsids.Length]; int i = 0; // fetch class descriptions. foreach (Guid clsid in clsids) { string progId; string sTemp1; string sTemp2; Guid tempClsid = clsid; try { m_server.GetClassDetails( ref tempClsid, out progId, out sTemp1, out sTemp2); sTemp1 = "opcae://localhost/" + progId; urls[i] = sTemp1; i++; } catch (Exception) { // ignore bad clsids. } } // free the server. Helper.ReleaseServer(m_server); m_server = null; return(urls); } finally { // free the server. Helper.ReleaseServer(m_server); m_server = null; } }