예제 #1
0
        public void SetWindowsServiceCredentials(string serviceName, string username, string password)
        {
            IntPtr hManager = IntPtr.Zero;
            IntPtr hService = IntPtr.Zero;

            try
            {
                hManager = ServiceWrapper.OpenSCManager(null, null, ServiceWrapper.SC_MANAGER_ALL_ACCESS);
                if (hManager == IntPtr.Zero)
                {
                    ThrowWin32Exception();
                }
                hService = ServiceWrapper.OpenService(hManager, serviceName, ServiceWrapper.SERVICE_QUERY_CONFIG | ServiceWrapper.SERVICE_CHANGE_CONFIG);
                if (hService == IntPtr.Zero)
                {
                    ThrowWin32Exception();
                }

                if (!ServiceWrapper.ChangeServiceConfig(hService, ServiceWrapper.SERVICE_NO_CHANGE, ServiceWrapper.SERVICE_NO_CHANGE, ServiceWrapper.SERVICE_NO_CHANGE, null, null, IntPtr.Zero, null, username, password, null))
                {
                    ThrowWin32Exception();
                }
            }
            finally
            {
                if (hService != IntPtr.Zero)
                {
                    ServiceWrapper.CloseServiceHandle(hService);
                }
                if (hManager != IntPtr.Zero)
                {
                    ServiceWrapper.CloseServiceHandle(hManager);
                }
            }
        }
예제 #2
0
        public string GetWindowsServiceUserName(string serviceName)
        {
            IntPtr hManager = IntPtr.Zero;
            IntPtr hService = IntPtr.Zero;

            try
            {
                hManager = ServiceWrapper.OpenSCManager(null, null, ServiceWrapper.SC_MANAGER_QUERY_ACESS);

                if (hManager == IntPtr.Zero)
                {
                    ThrowWin32Exception();
                }
                hService = ServiceWrapper.OpenService(hManager, serviceName, ServiceWrapper.SERVICE_QUERY_CONFIG | ServiceWrapper.SERVICE_CHANGE_CONFIG);
                if (hService == IntPtr.Zero)
                {
                    ThrowWin32Exception();
                }

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

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

                    var sci = (ServiceConfigInfo)Marshal.PtrToStructure(qscPtr, typeof(ServiceConfigInfo));
                    return(sci.ServiceStartName);
                }
                finally
                {
                    Marshal.FreeCoTaskMem(qscPtr);
                }
            }
            finally
            {
                if (hService != IntPtr.Zero)
                {
                    ServiceWrapper.CloseServiceHandle(hService);
                }
                if (hManager != IntPtr.Zero)
                {
                    ServiceWrapper.CloseServiceHandle(hManager);
                }
            }
        }