コード例 #1
0
        private ServiceHandle GivenAServiceExists(string serviceName, bool canBeUpdated)
        {
            GivenTheServiceControlManagerCanBeOpened();

            var serviceHandle = A.Fake <ServiceHandle>(o => o.Wrapping(new ServiceHandle {
                NativeInterop = nativeInterop
            }));

            A.CallTo(() => serviceHandle.IsInvalid).Returns(value: false);

            ServiceHandle  dummyServiceHandle;
            Win32Exception dummyWin32Exception;

            A.CallTo(() => serviceControlManager.TryOpenService(serviceName, A <ServiceControlAccessRights> ._, out dummyServiceHandle, out dummyWin32Exception))
            .Returns(value: true)
            .AssignsOutAndRefParameters(serviceHandle, null);

            if (canBeUpdated)
            {
                A.CallTo(
                    () =>
                    nativeInterop.ChangeServiceConfigW(serviceHandle, A <ServiceType> ._, A <ServiceStartType> ._, A <ErrorSeverity> ._, A <string> ._,
                                                       A <string> ._, A <IntPtr> ._, A <string> ._, A <string> ._, A <string> ._, A <string> ._))
                .Returns(value: true);

                A.CallTo(() => nativeInterop.ChangeServiceConfig2W(serviceHandle, ServiceConfigInfoTypeLevel.ServiceDescription, A <IntPtr> ._))
                .Returns(value: true);
                A.CallTo(() => nativeInterop.ChangeServiceConfig2W(serviceHandle, ServiceConfigInfoTypeLevel.FailureActions, A <IntPtr> ._))
                .Returns(value: true);
                A.CallTo(() => nativeInterop.ChangeServiceConfig2W(serviceHandle, ServiceConfigInfoTypeLevel.FailureActionsFlag, A <IntPtr> ._))
                .Returns(value: true);
                A.CallTo(() => nativeInterop.ChangeServiceConfig2W(serviceHandle, ServiceConfigInfoTypeLevel.DelayedAutoStartInfo, A <IntPtr> ._))
                .ReturnsLazily((ServiceHandle handle, ServiceConfigInfoTypeLevel infoLevel, IntPtr info) =>
                {
                    if (info != IntPtr.Zero)
                    {
                        delayedAutoStartInfoSetOnNativeInterop = Marshal.ReadInt32(info) > 0;
                    }
                    else
                    {
                        delayedAutoStartInfoSetOnNativeInterop = null;
                    }
                    return(true);
                });

                A.CallTo(() => nativeInterop.StartServiceW(serviceHandle, A <uint> ._, A <IntPtr> ._))
                .Returns(value: true);
            }

            return(serviceHandle);
        }