예제 #1
0
        private void SetAccount(string username, string password)
        {
            CheckControlPermission();

            IntPtr oServiceHandle = GetServiceHandle(WindowsServicesHelper.SERVICE_QUERY_CONFIG | WindowsServicesHelper.SERVICE_CHANGE_CONFIG);

            try
            {
                if (!WindowsServicesHelper.ChangeServiceConfig(
                        oServiceHandle,
                        WindowsServicesHelper.SERVICE_NO_CHANGE, WindowsServicesHelper.SERVICE_NO_CHANGE,

                        WindowsServicesHelper.SERVICE_NO_CHANGE,
                        null,
                        null,
                        IntPtr.Zero,
                        null,
                        username,
                        password,
                        null
                        ))
                {
                    throw CreateSafeWin32Exception();
                }
            }
            finally
            {
                WindowsServicesHelper.CloseServiceHandle(oServiceHandle);
            }
        }
예제 #2
0
        private void SetStartMode(ServiceStartMode eStartMode)
        {
            if (m_bStartModeAvailable && (eStartMode == m_eStartMode))
            {
                return;
            }

            CheckControlPermission();

            IntPtr oServiceHandle = GetServiceHandle(WindowsServicesHelper.SERVICE_QUERY_CONFIG | WindowsServicesHelper.SERVICE_CHANGE_CONFIG);

            try
            {
                if (!WindowsServicesHelper.ChangeServiceConfig(
                        oServiceHandle,
                        WindowsServicesHelper.SERVICE_NO_CHANGE,
                        (int)eStartMode,
                        WindowsServicesHelper.SERVICE_NO_CHANGE,
                        null,
                        null,
                        IntPtr.Zero,
                        null,
                        null,
                        null,
                        null
                        ))
                {
                    throw CreateSafeWin32Exception();
                }

                m_eStartMode          = eStartMode;
                m_bStartModeAvailable = true;
            }
            finally
            {
                WindowsServicesHelper.CloseServiceHandle(oServiceHandle);
            }
        }
예제 #3
0
        private ServiceStartMode GetStartMode()
        {
            //if (m_bStartModeAvailable)
            //{
            //    return m_eStartMode;
            //}

            CheckBrowsePermission();

            IntPtr oServiceHandle = GetServiceHandle(WindowsServicesHelper.SERVICE_QUERY_CONFIG);

            try
            {
                int nBytesNeeded;

                if (!WindowsServicesHelper.QueryServiceConfig(
                        oServiceHandle,
                        IntPtr.Zero,
                        0,
                        out nBytesNeeded
                        ))
                {
                    int nLastWin32Error = Marshal.GetLastWin32Error();

                    if (WindowsServicesHelper.ERROR_INSUFFICIENT_BUFFER != nLastWin32Error)
                    {
                        throw CreateSafeWin32Exception();
                    }

                    IntPtr oConfigPointer = Marshal.AllocHGlobal(nBytesNeeded);

                    try
                    {
                        if (!WindowsServicesHelper.QueryServiceConfig(
                                oServiceHandle,
                                oConfigPointer,
                                nBytesNeeded,
                                out nBytesNeeded
                                ))
                        {
                            throw CreateSafeWin32Exception();
                        }

                        WindowsServicesHelper.QUERY_SERVICE_CONFIG oQueryServiceConfig = new WindowsServicesHelper.QUERY_SERVICE_CONFIG();
                        Marshal.PtrToStructure(oConfigPointer, oQueryServiceConfig);

                        m_eStartMode          = (ServiceStartMode)oQueryServiceConfig.dwStartType;
                        m_bStartModeAvailable = true;
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(oConfigPointer);
                    }
                }

                return(m_eStartMode);
            }
            finally
            {
                WindowsServicesHelper.CloseServiceHandle(oServiceHandle);
            }
        }