예제 #1
0
        private static void ChangeDelayedAutoStart(IntPtr hService, bool delayed)
        {
            // Create structure that contains DelayedAutoStart property.
            SERVICE_DELAYED_AUTO_START_INFO info = new SERVICE_DELAYED_AUTO_START_INFO();

            // Set the DelayedAutostart property in that structure.
            info.fDelayedAutostart = delayed;

            // Allocate necessary memory.
            IntPtr hInfo = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SERVICE_DELAYED_AUTO_START_INFO)));

            // Convert structure to pointer.
            Marshal.StructureToPtr(info, hInfo, true);

            // Change the configuration.
            bool result = ChangeServiceConfig2(hService, SERVICE_CONFIG_DELAYED_AUTO_START_INFO, hInfo);

            // Release memory.
            Marshal.FreeHGlobal(hInfo);

            if (result == false)
            {
                ThrowLastWin32Error("Could not set service to delayed automatic");
            }
        }
예제 #2
0
#pragma warning restore

        #endregion

        public static void ChangeStartMode(string serviceName, ServiceStartMode mode, bool delayedAutoStart)
        {
            var scManagerHandle = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS);

            if (scManagerHandle == IntPtr.Zero)
            {
                throw new Win32Exception();
            }
            try
            {
                var serviceHandle = OpenService(scManagerHandle, serviceName, SERVICE_QUERY_CONFIG | SERVICE_CHANGE_CONFIG);

                if (serviceHandle == IntPtr.Zero)
                {
                    throw new Win32Exception();
                }
                try
                {
                    var retFlag = ChangeServiceConfig(serviceHandle, SERVICE_NO_CHANGE, (uint)mode, SERVICE_NO_CHANGE, null, null, IntPtr.Zero, null, null, null, null);

                    if (!retFlag)
                    {
                        int nError         = Marshal.GetLastWin32Error();
                        var win32Exception = new Win32Exception(nError);
                        throw new ExternalException("Could not change service start type: " + win32Exception.Message);
                    }

                    if (mode == ServiceStartMode.Automatic) //自动模式,需要设置延迟启动
                    {
                        SERVICE_DELAYED_AUTO_START_INFO info = new SERVICE_DELAYED_AUTO_START_INFO();
                        info.fDelayedAutostart = delayedAutoStart;
                        var ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(info));
                        try
                        {
                            Marshal.StructureToPtr(info, ptr, true);
                            retFlag = ChangeServiceConfig2(serviceHandle, SERVICE_CONFIG_DELAYED_AUTO_START_INFO, ptr);
                            if (!retFlag)
                            {
                                throw new Win32Exception();
                            }
                        }
                        finally { Marshal.FreeCoTaskMem(ptr); }
                    }
                }
                finally { CloseServiceHandle(serviceHandle); }
            }
            finally { CloseServiceHandle(scManagerHandle); }
        }
예제 #3
0
파일: ServiceInfo.cs 프로젝트: wmw23/Carbon
        private void SetDelayedAutoStart(IntPtr serviceHandle)
        {
            UInt32 dwBytesNeeded;

            // Determine the buffer size needed
            QueryServiceConfig2(serviceHandle, SERVICE_CONFIG_DELAYED_AUTO_START, IntPtr.Zero, 0, out dwBytesNeeded);
            var ptr = Marshal.AllocHGlobal((int)dwBytesNeeded);

            QueryServiceConfig2(serviceHandle, SERVICE_CONFIG_DELAYED_AUTO_START, ptr, dwBytesNeeded, out dwBytesNeeded);
            var delayedAutoStartStruct = new SERVICE_DELAYED_AUTO_START_INFO();

            Marshal.PtrToStructure(ptr, delayedAutoStartStruct);
            Marshal.FreeHGlobal(ptr);

            DelayedAutoStart = delayedAutoStartStruct.fDelayedAutostart;
        }
예제 #4
0
        private static void ChangeDelayedAutoStart(IntPtr hService, bool delayed)
        {
            SERVICE_DELAYED_AUTO_START_INFO info = new SERVICE_DELAYED_AUTO_START_INFO();

            info.fDelayedAutostart = delayed;
            IntPtr hInfo = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SERVICE_DELAYED_AUTO_START_INFO)));

            Marshal.StructureToPtr(info, hInfo, true);
            bool ret = ChangeServiceConfig2(hService, SERVICE_CONFIG_DELAYED_AUTO_START_INFO, hInfo);

            Marshal.FreeHGlobal(hInfo);
            if (!ret)
            {
                string message = "サービス遅延自動設定の変更に失敗";
                Console.WriteLine(message);
                ThrowLastWin32Error(message);
            }
        }
    static public void SetServiceProperties(
        string serviceName,
        ServiceControllerStatus status,
        ServiceStartMode startMode)
    {
        ServiceProperties props = GetServiceProperties(serviceName);

        IntPtr databaseHandle = OpenSCManager(
            null,
            null,
            NativeConstants.ServiceControlManager.SC_MANAGER_ALL_ACCESS);

        // An error might happen here if we are not running as administrator and the service
        // database is locked.
        if (databaseHandle == IntPtr.Zero)
        {
            throw new ExternalException(
                      "Unable to OpenSCManager. Not enough rights.");
        }

        IntPtr serviceHandle = OpenService(
            databaseHandle,
            serviceName,
            NativeConstants.Service.SERVICE_ALL_ACCESS);

        if (serviceHandle == IntPtr.Zero)
        {
            string errMsg = GetErrorMessage(Marshal.GetLastWin32Error());
            CloseServiceHandle(databaseHandle);
            throw new ExternalException(
                      "Unable to OpenService '" + serviceName + "':" + errMsg);
        }

        // Change service startType config
        Int32 dwStartType;
        SERVICE_DELAYED_AUTO_START_INFO delayedInfo =
            new SERVICE_DELAYED_AUTO_START_INFO();

        delayedInfo.fDelayedAutostart = false;
        switch (startMode)
        {
        case ServiceStartMode.AutomaticDelayed:
            dwStartType = NativeConstants.Service.SERVICE_AUTO_START;
            delayedInfo.fDelayedAutostart = true;
            break;

        case ServiceStartMode.Automatic:
            dwStartType = NativeConstants.Service.SERVICE_AUTO_START;
            break;

        case ServiceStartMode.Boot:
            dwStartType = NativeConstants.Service.SERVICE_BOOT_START;
            break;

        case ServiceStartMode.Disabled:
            dwStartType = NativeConstants.Service.SERVICE_DISABLED;
            break;

        case ServiceStartMode.Manual:
            dwStartType = NativeConstants.Service.SERVICE_DEMAND_START;
            break;

        case ServiceStartMode.System:
            dwStartType = NativeConstants.Service.SERVICE_SYSTEM_START;
            break;

        default:
            CloseServiceHandle(databaseHandle);
            CloseServiceHandle(serviceHandle);
            throw new ExternalException(
                      "The service '" + serviceName + "' has an invalid start type");
        }

        if (!ChangeServiceConfig(
                // handle of service
                serviceHandle,
                // service type: no change
                NativeConstants.Service.SERVICE_NO_CHANGE,
                // service start type
                dwStartType,
                // error control: no change
                NativeConstants.Service.SERVICE_NO_CHANGE,
                // binary path: no change
                IntPtr.Zero,
                // load order group: no change
                IntPtr.Zero,
                // tag ID: no change
                IntPtr.Zero,
                // dependencies: no change
                IntPtr.Zero,
                // account name: no change
                IntPtr.Zero,
                // password: no change
                IntPtr.Zero,
                // display name: no change
                IntPtr.Zero))
        {
            string errMsg = GetErrorMessage(Marshal.GetLastWin32Error());
            CloseServiceHandle(serviceHandle);
            CloseServiceHandle(databaseHandle);
            throw new ExternalException(
                      "Unable to change configuration for service '" + serviceName + "':" + errMsg);
        }

        IntPtr pDelayedInfo = Marshal.AllocHGlobal(Marshal.SizeOf(delayedInfo));

        Marshal.StructureToPtr(delayedInfo, pDelayedInfo, true);

        if (!ChangeServiceConfig2(
                serviceHandle,
                NativeConstants.Service.SERVICE_CONFIG_DELAYED_AUTO_START_INFO,
                pDelayedInfo))
        {
            string errMsg = GetErrorMessage(Marshal.GetLastWin32Error());
            CloseServiceHandle(serviceHandle);
            CloseServiceHandle(databaseHandle);
            Marshal.FreeHGlobal(pDelayedInfo);
            pDelayedInfo = IntPtr.Zero;
            throw new ExternalException(
                      "Unable to change configuration for service '" + serviceName + "':" + errMsg);
        }

        Marshal.FreeHGlobal(pDelayedInfo);
        pDelayedInfo = IntPtr.Zero;

        // If the user wants to start the service
        if (status == ServiceControllerStatus.Running ||
            status == ServiceControllerStatus.StartPending)
        {
            // Try to start the service, only if it is not already started
            if (props.Status == ServiceControllerStatus.Stopped ||
                props.Status == ServiceControllerStatus.StopPending)
            {
            }
        }
    }