Example #1
0
 internal Service(ServiceEnumInfo info, ServiceCollection parent)
 {
     m_prefix = info.Prefix;
     m_handle = info.Handle;
     m_state  = info.State;
     m_dll    = info.DLLName;
     m_parent = parent;
 }
Example #2
0
        /// <summary>
        /// Gets a collection of Services supported on the device
        /// </summary>
        /// <returns></returns>
        public static ServiceCollection GetServices()
        {
            ServiceCollection collection = new ServiceCollection();

            if (Environment.OSVersion.Version.Major > 5)
            {
                // jsm - in CE 6, GetServiceHandle is now deprecated

                // 1. Open registry to inspect services
                string DLLName = String.Empty;

                using (Microsoft.Win32.RegistryKey root = Registry.LocalMachine.OpenSubKey("Services"))
                {
                    string[] subkeyNames = root.GetSubKeyNames();

                    foreach (string name in subkeyNames)
                    {
                        using (RegistryKey svcKey = root.OpenSubKey(name, true))
                        {
                            string prefixVal = (string)svcKey.GetValue("Prefix");
                            int?   svcIndex  = (int?)svcKey.GetValue("Index");
                            DLLName = (string)svcKey.GetValue("Dll");

                            if (!String.IsNullOrEmpty(prefixVal))
                            {
                                string strIndex   = (svcIndex == null) ? "" : svcIndex.ToString();
                                string prefixFull = String.Format("{0}:", prefixVal.ToUpper().Substring(0, 3) + strIndex);

                                // 2. Get handles by way of CreateFile
                                IntPtr svcHandle = CreateFile(prefixFull, 0, 0, 0, OPEN_EXISTING, 0, 0);

                                int numBytesReturned = 0;
                                int lastError        = 0;

                                int[] outBuffer = new int[1];
                                int   outSize   = sizeof(int) * outBuffer.Length;

                                if (!DeviceIoControl(svcHandle, IOCTL_SERVICE_STATUS, null, 0, outBuffer, outSize, ref numBytesReturned, IntPtr.Zero))
                                {
                                    lastError = Marshal.GetLastWin32Error();
                                }
                                else
                                {
                                    ServiceState svcState = (ServiceState)outBuffer[0];
                                    collection.Add(new Service(new ServiceEnumInfo(prefixFull, DLLName, svcHandle, svcState), collection));
                                }
                            }
                        }
                    }
                }

                return(collection);
            }

            else
            {
                byte[] data = new byte[0];
                int    count;
                int    size = 0;

                NativeMethods.EnumServices(data, out count, ref size);

                data = new byte[size];

                NativeMethods.EnumServices(data, out count, ref size);

                for (int i = 0; i < count; i++)
                {
                    ServiceEnumInfo info = new ServiceEnumInfo(data, i * ServiceEnumInfo.SIZE);
                    collection.Add(new Service(info, collection));
                }
            }

            return(collection);
        }