Example #1
0
        /// <summary>
        /// Set the service to trigger-start when the first IP address on the TCP/IP
        /// networking stack becomes available, and trigger-stop when the last IP
        /// address on the TCP/IP networking stack becomes unavailable.
        /// </summary>
        /// <param name="serviceName">The name of the service</param>
        public static void SetServiceTriggerStartOnIPAddressArrival(string serviceName)
        {
            // Open the local default service control manager database
            SafeServiceHandle schSCManager = NativeMethods.OpenSCManager(null, null,
                                                                         ServiceControlAccessRights.SC_MANAGER_CONNECT);

            if (schSCManager.IsInvalid)
            {
                // If the handle is invalid, get the last Win32 error and throw a
                // Win32Exception
                throw new Win32Exception();
            }

            try
            {
                // Try to open the service with the SERVICE_CHANGE_CONFIG access right
                SafeServiceHandle schService = NativeMethods.OpenService(schSCManager,
                                                                         serviceName, ServiceAccessRights.SERVICE_CHANGE_CONFIG);
                if (schService.IsInvalid)
                {
                    // If the handle is invalid, get the last Win32 error and throw a
                    // Win32Exception
                    throw new Win32Exception();
                }

                try
                {
                    SetServiceTriggerStartOnIPAddressArrival(schService);
                }
                finally
                {
                    schService.Close();
                }
            }
            finally
            {
                schSCManager.Close();
            }
        }
Example #2
0
        /// <summary>
        /// Determine whether the specified service is configured to trigger start
        /// </summary>
        /// <param name="serviceName">The name of the service</param>
        /// <returns></returns>
        public static bool IsTriggerStartService(string serviceName)
        {
            // Open the local default service control manager database
            SafeServiceHandle schSCManager = NativeMethods.OpenSCManager(null, null,
                                                                         ServiceControlAccessRights.SC_MANAGER_CONNECT);

            if (schSCManager.IsInvalid)
            {
                // If the handle is invalid, get the last Win32 error and throw a
                // Win32Exception
                throw new Win32Exception();
            }

            try
            {
                // Try to open the service to query its config
                SafeServiceHandle schService = NativeMethods.OpenService(schSCManager,
                                                                         serviceName, ServiceAccessRights.SERVICE_QUERY_CONFIG);
                if (schService.IsInvalid)
                {
                    // If the handle is invalid, get the last Win32 error and throw a
                    // Win32Exception
                    throw new Win32Exception();
                }

                try
                {
                    return(IsTriggerStartService(schService));
                }
                finally
                {
                    schService.Close();
                }
            }
            finally
            {
                schSCManager.Close();
            }
        }
Example #3
0
        /// <summary>
        /// Set the service to trigger-start when the first IP address on the TCP/IP
        /// networking stack becomes available, and trigger-stop when the last IP
        /// address on the TCP/IP networking stack becomes unavailable.
        /// </summary>
        /// <param name="hService">
        /// A handle to the service. This handle is returned by the OpenService or
        /// CreateService function and must have the SERVICE_CHANGE_CONFIG access right.
        /// </param>
        public static void SetServiceTriggerStartOnIPAddressArrival(SafeServiceHandle hService)
        {
            IntPtr pGuidIpAddressArrival = IntPtr.Zero;
            IntPtr pGuidIpAddressRemoval = IntPtr.Zero;
            IntPtr pServiceTriggers      = IntPtr.Zero;
            IntPtr pServiceTriggerInfo   = IntPtr.Zero;

            try
            {
                // Marshal Guid struct NETWORK_MANAGER_FIRST_IP_ADDRESS_ARRIVAL_GUID
                // and NETWORK_MANAGER_LAST_IP_ADDRESS_REMOVAL_GUID to native memory

                int cbGuid = Marshal.SizeOf(typeof(Guid));
                pGuidIpAddressArrival = Marshal.AllocHGlobal(cbGuid);
                Marshal.StructureToPtr(NETWORK_MANAGER_FIRST_IP_ADDRESS_ARRIVAL_GUID,
                                       pGuidIpAddressArrival, false);
                pGuidIpAddressRemoval = Marshal.AllocHGlobal(cbGuid);
                Marshal.StructureToPtr(NETWORK_MANAGER_LAST_IP_ADDRESS_REMOVAL_GUID,
                                       pGuidIpAddressRemoval, false);

                // Allocate and set the SERVICE_TRIGGER structure for
                // NETWORK_MANAGER_FIRST_IP_ADDRESS_ARRIVAL_GUID to start the service

                SERVICE_TRIGGER serviceTrigger1 = new SERVICE_TRIGGER();
                serviceTrigger1.dwTriggerType =
                    ServiceTriggerType.SERVICE_TRIGGER_TYPE_IP_ADDRESS_AVAILABILITY;
                serviceTrigger1.dwAction =
                    ServiceTriggerAction.SERVICE_TRIGGER_ACTION_SERVICE_START;
                serviceTrigger1.pTriggerSubtype = pGuidIpAddressArrival;

                // Allocate and set the SERVICE_TRIGGER structure for
                // NETWORK_MANAGER_LAST_IP_ADDRESS_REMOVAL_GUID to stop the service

                SERVICE_TRIGGER serviceTrigger2 = new SERVICE_TRIGGER();
                serviceTrigger2.dwTriggerType =
                    ServiceTriggerType.SERVICE_TRIGGER_TYPE_IP_ADDRESS_AVAILABILITY;
                serviceTrigger2.dwAction =
                    ServiceTriggerAction.SERVICE_TRIGGER_ACTION_SERVICE_STOP;
                serviceTrigger2.pTriggerSubtype = pGuidIpAddressRemoval;

                // Marshal the 2 SERVICE_TRIGGER structs to native memory as an array

                int cbServiceTrigger = Marshal.SizeOf(typeof(SERVICE_TRIGGER));
                pServiceTriggers = Marshal.AllocHGlobal(cbServiceTrigger * 2);
                Marshal.StructureToPtr(serviceTrigger1, pServiceTriggers, false);
                Marshal.StructureToPtr(serviceTrigger2,
                                       new IntPtr((long)pServiceTriggers + cbServiceTrigger), false);

                // Allocate and set the SERVICE_TRIGGER_INFO structure

                SERVICE_TRIGGER_INFO serviceTriggerInfo = new SERVICE_TRIGGER_INFO();
                serviceTriggerInfo.cTriggers = 2;
                serviceTriggerInfo.pTriggers = pServiceTriggers;

                // Marshal the SERVICE_TRIGGER_INFO struct to native memory

                int cbServiceTriggerInfo = Marshal.SizeOf(typeof(SERVICE_TRIGGER_INFO));
                pServiceTriggerInfo = Marshal.AllocHGlobal(cbServiceTriggerInfo);
                Marshal.StructureToPtr(serviceTriggerInfo, pServiceTriggerInfo, false);

                // Call ChangeServiceConfig2 with the SERVICE_CONFIG_TRIGGER_INFO level
                // and pass to it the address of the SERVICE_TRIGGER_INFO structure

                if (!NativeMethods.ChangeServiceConfig2(hService,
                                                        ServiceConfig2InfoLevel.SERVICE_CONFIG_TRIGGER_INFO,
                                                        pServiceTriggerInfo))
                {
                    // If the handle is invalid, get the last Win32 error and throw a
                    // Win32Exception
                    throw new Win32Exception();
                }
            }
            finally
            {
                // Clean up the native memory

                if (pGuidIpAddressArrival != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pGuidIpAddressArrival);
                }

                if (pGuidIpAddressRemoval != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pGuidIpAddressRemoval);
                }

                if (pServiceTriggers != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pServiceTriggers);
                }

                if (pServiceTriggerInfo != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pServiceTriggerInfo);
                }
            }
        }
Example #4
0
        /// <summary>
        /// Set the service to trigger-start when a generic USB disk becomes available.
        /// </summary>
        /// <param name="hService">
        /// A handle to the service. This handle is returned by the OpenService or
        /// CreateService function and must have the SERVICE_CHANGE_CONFIG access right.
        /// </param>
        public static void SetServiceTriggerStartOnUSBArrival(SafeServiceHandle hService)
        {
            IntPtr pGuidUSBDevice      = IntPtr.Zero;
            IntPtr pUSBHardwareId      = IntPtr.Zero;
            IntPtr pDeviceData         = IntPtr.Zero;
            IntPtr pServiceTrigger     = IntPtr.Zero;
            IntPtr pServiceTriggerInfo = IntPtr.Zero;

            try
            {
                // Marshal the Guid struct GUID_DEVINTERFACE_DISK to native memory

                pGuidUSBDevice = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Guid)));
                Marshal.StructureToPtr(GUID_DEVINTERFACE_DISK, pGuidUSBDevice, false);

                // Allocate and set the SERVICE_TRIGGER_SPECIFIC_DATA_ITEM structure

                SERVICE_TRIGGER_SPECIFIC_DATA_ITEM deviceData;
                deviceData.dwDataType =
                    ServiceTriggerDataType.SERVICE_TRIGGER_DATA_TYPE_STRING;
                deviceData.cbData = (uint)(USBHardwareId.Length + 1) * 2;
                pUSBHardwareId    = Marshal.StringToHGlobalUni(USBHardwareId);
                deviceData.pData  = pUSBHardwareId;

                // Marshal SERVICE_TRIGGER_SPECIFIC_DATA_ITEM to native memory

                int cbDataItem = Marshal.SizeOf(typeof(SERVICE_TRIGGER_SPECIFIC_DATA_ITEM));
                pDeviceData = Marshal.AllocHGlobal(cbDataItem);
                Marshal.StructureToPtr(deviceData, pDeviceData, false);

                // Allocate and set the SERVICE_TRIGGER structure

                SERVICE_TRIGGER serviceTrigger = new SERVICE_TRIGGER();
                serviceTrigger.dwTriggerType =
                    ServiceTriggerType.SERVICE_TRIGGER_TYPE_DEVICE_INTERFACE_ARRIVAL;
                serviceTrigger.dwAction =
                    ServiceTriggerAction.SERVICE_TRIGGER_ACTION_SERVICE_START;
                serviceTrigger.pTriggerSubtype = pGuidUSBDevice;
                serviceTrigger.cDataItems      = 1;
                serviceTrigger.pDataItems      = pDeviceData;

                // Marshal the SERVICE_TRIGGER struct to native memory

                int cbServiceTrigger = Marshal.SizeOf(typeof(SERVICE_TRIGGER));
                pServiceTrigger = Marshal.AllocHGlobal(cbServiceTrigger);
                Marshal.StructureToPtr(serviceTrigger, pServiceTrigger, false);

                // Allocate and set the SERVICE_TRIGGER_INFO structure

                SERVICE_TRIGGER_INFO serviceTriggerInfo = new SERVICE_TRIGGER_INFO();
                serviceTriggerInfo.cTriggers = 1;
                serviceTriggerInfo.pTriggers = pServiceTrigger;

                // Marshal the SERVICE_TRIGGER_INFO struct to native memory

                int cbServiceTriggerInfo = Marshal.SizeOf(typeof(SERVICE_TRIGGER_INFO));
                pServiceTriggerInfo = Marshal.AllocHGlobal(cbServiceTriggerInfo);
                Marshal.StructureToPtr(serviceTriggerInfo, pServiceTriggerInfo, false);

                // Call ChangeServiceConfig2 with the SERVICE_CONFIG_TRIGGER_INFO level
                // and pass to it the address of the SERVICE_TRIGGER_INFO structure

                if (!NativeMethods.ChangeServiceConfig2(hService,
                                                        ServiceConfig2InfoLevel.SERVICE_CONFIG_TRIGGER_INFO,
                                                        pServiceTriggerInfo))
                {
                    // If the handle is invalid, get the last Win32 error and throw a
                    // Win32Exception
                    throw new Win32Exception();
                }
            }
            finally
            {
                // Clean up the native memory

                if (pGuidUSBDevice != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pGuidUSBDevice);
                }

                if (pUSBHardwareId != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pUSBHardwareId);
                }

                if (pDeviceData != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pDeviceData);
                }

                if (pServiceTrigger != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pServiceTrigger);
                }

                if (pServiceTriggerInfo != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pServiceTriggerInfo);
                }
            }
        }
 public static extern bool ChangeServiceConfig2(SafeServiceHandle hService,
                                                ServiceConfig2InfoLevel dwInfoLevel, IntPtr lpInfo);
 public static extern SafeServiceHandle OpenService(
     SafeServiceHandle hSCManager,
     string lpServiceName,
     ServiceAccessRights dwDesiredAccess);
 public static extern bool QueryServiceConfig2(SafeServiceHandle hService,
                                               ServiceConfig2InfoLevel dwInfoLevel, IntPtr lpBuffer, int cbBufSize,
                                               out int pcbBytesNeeded);