Ejemplo n.º 1
0
        /// <summary>
        /// Creates the specified service.
        /// </summary>
        /// <param name="ServiceName">Name of the service.</param>
        /// <param name="DisplayName">The display name.</param>
        /// <param name="ServiceAccess">The service access.</param>
        /// <param name="ServiceType">Type of the service.</param>
        /// <param name="ServiceStart">The service start.</param>
        /// <param name="ServiceError">The service error.</param>
        /// <param name="File">The file.</param>
        internal static IntPtr Create(string ServiceName, string DisplayName, ServiceAccess ServiceAccess, ServiceType ServiceType, ServiceStart ServiceStart, ServiceError ServiceError, FileInfo File)
        {
            IntPtr ServiceManager = WinApi.OpenSCManager(null, null, (uint)ScmAccess.ScManagerAllAccess);

            if (ServiceManager == IntPtr.Zero)
            {
                return(IntPtr.Zero);
            }

            IntPtr Service = WinApi.CreateService(
                ServiceManager,
                ServiceName,
                DisplayName,
                (uint)ServiceAccess,
                (uint)ServiceType,
                (uint)ServiceStart,
                (uint)ServiceError,
                File.FullName,
                null, null, null, null, null
                );

            WinApi.CloseServiceHandle(ServiceManager);

            if (Service == IntPtr.Zero)
            {
                return(IntPtr.Zero);
            }

            return(Service);
        }
Ejemplo n.º 2
0
        private void InstallUsbAgentService()
        {
            string binDir  = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string srcFile = Path.Combine(binDir, Constants.UsbAgentFilename);

            try
            {
                File.Copy(srcFile, UsbAgentServiceInstallPath, true);
            }
            catch (Exception e)
            {
                throw new Error("Failed to install USB agent driver: {0}", e.Message);
            }

            IntPtr manager = WinApi.OpenSCManager(null, null, WinApi.SC_MANAGER_ALL_ACCESS);

            if (manager == IntPtr.Zero)
            {
                throw new Error("OpenSCManager failed");
            }

            IntPtr service = IntPtr.Zero;

            try
            {
                service = WinApi.OpenService(manager, Constants.UsbAgentName, WinApi.SERVICE_ALL_ACCESS);
                if (service == IntPtr.Zero && Marshal.GetLastWin32Error() == WinApi.ERROR_SERVICE_DOES_NOT_EXIST)
                {
                    service = WinApi.CreateService(manager, Constants.UsbAgentName, Constants.UsbAgentDescription, WinApi.SERVICE_ALL_ACCESS, WinApi.SERVICE_KERNEL_DRIVER, WinApi.SERVICE_DEMAND_START, WinApi.SERVICE_ERROR_NORMAL, Constants.UsbAgentPath, null, 0, null, null, null);
                    if (service == IntPtr.Zero)
                    {
                        throw new Error("CreateService failed");
                    }
                }

                if (!WinApi.StartService(service, 0, null))
                {
                    int lastError = Marshal.GetLastWin32Error();
                    if (lastError != WinApi.ERROR_SERVICE_ALREADY_RUNNING && lastError != WinApi.ERROR_SERVICE_DISABLED)
                    {
                        throw new Error("Failed to start service: 0x{0:x8}", lastError);
                    }
                }
            }
            finally
            {
                if (service != IntPtr.Zero)
                {
                    WinApi.CloseServiceHandle(service);
                }

                WinApi.CloseServiceHandle(manager);
            }
        }
Ejemplo n.º 3
0
        private void WaitForUsbAgentServiceToStop()
        {
            IntPtr manager = WinApi.OpenSCManager(null, null, WinApi.SC_MANAGER_ALL_ACCESS);

            if (manager == IntPtr.Zero)
            {
                throw new Error("OpenSCManager failed");
            }

            IntPtr service = IntPtr.Zero;

            try
            {
                service = WinApi.OpenService(manager, Constants.UsbAgentName, WinApi.SERVICE_ALL_ACCESS);
                if (service == IntPtr.Zero)
                {
                    throw new Error("OpenService failed");
                }

                WinApi.SERVICE_STATUS status = new WinApi.SERVICE_STATUS();

                progress.ProgressUpdate("Unplug any USB device being monitored now", 100);

                bool stopped = false;

                while (!stopped)
                {
                    if (!WinApi.QueryServiceStatus(service, ref status))
                    {
                        throw new Error("Failed to query for service status: 0x{0:x8}", Marshal.GetLastWin32Error());
                    }

                    stopped = status.dwCurrentState == WinApi.SERVICE_STOPPED;
                    if (!stopped)
                    {
                        Thread.Sleep(250);
                    }
                }
            }
            finally
            {
                if (service != IntPtr.Zero)
                {
                    WinApi.CloseServiceHandle(service);
                }

                WinApi.CloseServiceHandle(manager);
            }
        }
Ejemplo n.º 4
0
        private void RemoveUsbAgentService()
        {
            IntPtr manager = WinApi.OpenSCManager(null, null, WinApi.SC_MANAGER_ALL_ACCESS);

            if (manager == IntPtr.Zero)
            {
                throw new Error("OpenSCManager failed");
            }

            IntPtr service = IntPtr.Zero;

            try
            {
                service = WinApi.OpenService(manager, Constants.UsbAgentName, WinApi.SERVICE_ALL_ACCESS);
                if (service != IntPtr.Zero)
                {
                    if (!WinApi.DeleteService(service))
                    {
                        throw new Error("DeleteService failed: 0x{0:x8}", Marshal.GetLastWin32Error());
                    }
                }
                else
                {
                    if (Marshal.GetLastWin32Error() != WinApi.ERROR_SERVICE_DOES_NOT_EXIST)
                    {
                        throw new Error("OpenService failed");
                    }
                }
            }
            finally
            {
                if (service != IntPtr.Zero)
                {
                    WinApi.CloseServiceHandle(service);
                }

                WinApi.CloseServiceHandle(manager);
            }

            if (File.Exists(UsbAgentServiceInstallPath))
            {
                File.Delete(UsbAgentServiceInstallPath);
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Closes the specified service handle.
 /// </summary>
 /// <param name="Handle">The handle.</param>
 internal static bool Close(IntPtr Handle)
 {
     return(WinApi.CloseServiceHandle(Handle));
 }