Beispiel #1
0
        private string GetWindowsServiceUserName()
        {
            IntPtr manager = IntPtr.Zero;
            IntPtr service = IntPtr.Zero;

            try
            {
                manager = WindowsServiceBridge.OpenSCManager(null, null, WindowsServiceBridge.SC_MANAGER_QUERY_ACESS);

                if (manager == IntPtr.Zero)
                {
                    ThrowWin32Exception();
                }

                service = WindowsServiceBridge.OpenService(manager, _serviceController.ServiceName, WindowsServiceBridge.SERVICE_QUERY_CONFIG | WindowsServiceBridge.SERVICE_CHANGE_CONFIG);
                if (service == IntPtr.Zero)
                {
                    ThrowWin32Exception();
                }

                bool retCode = WindowsServiceBridge.QueryServiceConfig(service, IntPtr.Zero, 0, out uint bytesNeeded);
                if (!retCode && bytesNeeded == 0)
                {
                    ThrowWin32Exception();
                }

                IntPtr qscPtr = Marshal.AllocCoTaskMem(Convert.ToInt32(bytesNeeded));
                try
                {
                    retCode = WindowsServiceBridge.QueryServiceConfig(service, qscPtr, bytesNeeded, out bytesNeeded);
                    if (!retCode)
                    {
                        ThrowWin32Exception();
                    }

                    var sci = (ServiceConfigInfo)Marshal.PtrToStructure(qscPtr, typeof(ServiceConfigInfo));
                    return(sci.ServiceStartName);
                }
                finally
                {
                    Marshal.FreeCoTaskMem(qscPtr);
                }
            }
            finally
            {
                if (service != IntPtr.Zero)
                {
                    WindowsServiceBridge.CloseServiceHandle(service);
                }
                if (manager != IntPtr.Zero)
                {
                    WindowsServiceBridge.CloseServiceHandle(manager);
                }
            }
        }
Beispiel #2
0
        public void SetWindowsServiceCredentials(string serviceName, string username, string password)
        {
            IntPtr manager = IntPtr.Zero;
            IntPtr service = IntPtr.Zero;

            try
            {
                manager = WindowsServiceBridge.OpenSCManager(null, null, WindowsServiceBridge.SC_MANAGER_ALL_ACCESS);
                if (manager == IntPtr.Zero)
                {
                    ThrowWin32Exception();
                }

                service = WindowsServiceBridge.OpenService(manager, serviceName, WindowsServiceBridge.SERVICE_QUERY_CONFIG | WindowsServiceBridge.SERVICE_CHANGE_CONFIG);
                if (service == IntPtr.Zero)
                {
                    ThrowWin32Exception();
                }

                if (!WindowsServiceBridge.ChangeServiceConfig(service, WindowsServiceBridge.SERVICE_NO_CHANGE, WindowsServiceBridge.SERVICE_NO_CHANGE, WindowsServiceBridge.SERVICE_NO_CHANGE, null, null, IntPtr.Zero, null, username, password, null))
                {
                    ThrowWin32Exception();
                }
            }
            finally
            {
                if (service != IntPtr.Zero)
                {
                    WindowsServiceBridge.CloseServiceHandle(service);
                }
                if (manager != IntPtr.Zero)
                {
                    WindowsServiceBridge.CloseServiceHandle(manager);
                }
            }
        }