Opc.Ua.Configuration.ConfigUtils.EnumClassesInCategory C# (CSharp) Method

EnumClassesInCategory() public static method

Fetches the classes in the specified category.
public static EnumClassesInCategory ( System.Guid category ) : List
category System.Guid
return List
		public static List<Guid> EnumClassesInCategory(Guid category)
		{
			ICatInformation manager = (ICatInformation)CreateServer(CLSID_StdComponentCategoriesMgr);
	
			object unknown = null;

			try
			{
				manager.EnumClassesOfCategories(
					1, 
					new Guid[] { category }, 
					0, 
					null,
					out unknown);
                
				IEnumGUID enumerator = (IEnumGUID)unknown;

				List<Guid> classes = new List<Guid>();

				Guid[] buffer = new Guid[10];

				while (true)
				{
					int fetched = 0;

                    IntPtr pGuids = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Guid))*buffer.Length);
                    
                    try
                    {
					    enumerator.Next(buffer.Length, pGuids, out fetched);

					    if (fetched == 0)
					    {
						    break;
					    }
                        
			            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)));
			            }
                    }
                    finally
                    {
                        Marshal.FreeCoTaskMem(pGuids);
                    }

					for (int ii = 0; ii < fetched; ii++)
					{
						classes.Add(buffer[ii]);
					}
				}
			
				return classes;
			}
			finally
			{
				ReleaseServer(unknown);
				ReleaseServer(manager);
			}
        }

Usage Example

コード例 #1
0
        /// <summary>
        /// Returns the UA COM Pseudo-servers registered on the local computer.
        /// </summary>
        public static List <ConfiguredEndpoint> Enumerate()
        {
            // enumerate server clsids.
            List <Guid> clsids = ConfigUtils.EnumClassesInCategory(ConfigUtils.CATID_PseudoComServers);

            // initialize server objects.
            List <ConfiguredEndpoint> servers = new List <ConfiguredEndpoint>();

            for (int ii = 0; ii < clsids.Count; ii++)
            {
                ConfiguredEndpoint server = null;

                try {
                    server = Load(clsids[ii]);
                    servers.Add(server);
                } catch (Exception e) {
                    Utils.Trace(e, "Unexpected error loading psuedo-server: {0}", clsids[ii]);
                    continue;
                }

                // ensure the Pseudo-server points to the correct host process.
                Guid hostClsid = Guid.Empty;

                if (server.ComIdentity != null)
                {
                    hostClsid = GetServerHostClsid(server.ComIdentity.Specification);
                }

                string hostPath = ConfigUtils.GetExecutablePath(hostClsid);

                if (String.IsNullOrEmpty(hostPath))
                {
                    continue;
                }

                RegistryKey key =
                    Registry.ClassesRoot.OpenSubKey(String.Format(@"CLSID\{{{0}}}\LocalServer32", clsids[ii]), false);

                if (key != null)
                {
                    if (hostPath != (string)key.GetValue(null, ""))
                    {
                        try {
                            key.Close();
                            key = Registry.ClassesRoot.OpenSubKey(
                                String.Format(@"CLSID\{{{0}}}\LocalServer32", clsids[ii]), true);
                            key.SetValue(null, hostPath);
                        } catch (Exception) {
                            Utils.Trace("Could not update COM proxy server path for {0}.", server.ComIdentity.ProgId);
                        }
                    }

                    key.Close();
                }
            }

            return(servers);
        }