Ejemplo n.º 1
0
        private static SafeServiceHandle OpenSCManager()
        {
            SafeServiceHandle handle = ListenerUnsafeNativeMethods.OpenSCManager(null, null, 1);

            if (handle.IsInvalid)
            {
                Exception exception = new Win32Exception();
                handle.SetHandleAsInvalid();
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception);
            }
            return(handle);
        }
Ejemplo n.º 2
0
        private static SafeServiceHandle OpenService(SafeServiceHandle scManager, string serviceName, int purpose)
        {
            SafeServiceHandle handle = ListenerUnsafeNativeMethods.OpenService(scManager, serviceName, purpose);

            if (handle.IsInvalid)
            {
                Exception exception = new Win32Exception();
                handle.SetHandleAsInvalid();
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception);
            }
            return(handle);
        }
Ejemplo n.º 3
0
        static SafeServiceHandle OpenSCManager()
        {
#pragma warning suppress 56523 // Microsoft, Win32Exception ctor calls Marshal.GetLastWin32Error()
            SafeServiceHandle scManager = ListenerUnsafeNativeMethods.OpenSCManager(null, null, ListenerUnsafeNativeMethods.SC_MANAGER_CONNECT);
            if (scManager.IsInvalid)
            {
                Exception exception = new Win32Exception();
                scManager.SetHandleAsInvalid();
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception);
            }
            return(scManager);
        }
Ejemplo n.º 4
0
        static SafeServiceHandle OpenService(SafeServiceHandle scManager, string serviceName, int purpose)
        {
#pragma warning suppress 56523 // Microsoft, Win32Exception ctor calls Marshal.GetLastWin32Error()
            SafeServiceHandle service = ListenerUnsafeNativeMethods.OpenService(scManager, serviceName, purpose);
            if (service.IsInvalid)
            {
                Exception exception = new Win32Exception();
                service.SetHandleAsInvalid();
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception);
            }
            return(service);
        }
Ejemplo n.º 5
0
        private static unsafe ListenerUnsafeNativeMethods.SERVICE_STATUS_PROCESS GetStatusForService(string serviceName)
        {
            ListenerUnsafeNativeMethods.SERVICE_STATUS_PROCESS service_status_process;
            SafeServiceHandle scManager = OpenSCManager();

            try
            {
                SafeServiceHandle hService = OpenService(scManager, serviceName, 4);
                try
                {
                    int num;
                    if (!ListenerUnsafeNativeMethods.QueryServiceStatusEx(hService, 0, null, 0, out num))
                    {
                        int error = Marshal.GetLastWin32Error();
                        if (error != 0x7a)
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(error));
                        }
                    }
                    byte[] pBuffer = new byte[num];
                    if (!ListenerUnsafeNativeMethods.QueryServiceStatusEx(hService, 0, pBuffer, pBuffer.Length, out num))
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception());
                    }
                    try
                    {
                        fixed(byte *numRef = pBuffer)
                        {
                            service_status_process = (ListenerUnsafeNativeMethods.SERVICE_STATUS_PROCESS)Marshal.PtrToStructure((IntPtr)numRef, typeof(ListenerUnsafeNativeMethods.SERVICE_STATUS_PROCESS));
                        }
                    }
                    finally
                    {
                        numRef = null;
                    }
                }
                finally
                {
                    hService.Close();
                }
            }
            finally
            {
                scManager.Close();
            }
            return(service_status_process);
        }
Ejemplo n.º 6
0
        static ListenerUnsafeNativeMethods.SERVICE_STATUS_PROCESS GetStatusForService(string serviceName)
        {
            SafeServiceHandle scManager = OpenSCManager();

            try
            {
                SafeServiceHandle service = OpenService(scManager, serviceName, ListenerUnsafeNativeMethods.SERVICE_QUERY_STATUS);
                try
                {
                    int  lpnLengthNeeded;
                    bool success = ListenerUnsafeNativeMethods.QueryServiceStatusEx(service, ListenerUnsafeNativeMethods.SC_STATUS_PROCESS_INFO, null, 0, out lpnLengthNeeded);
                    if (!success)
                    {
                        int errorCode = Marshal.GetLastWin32Error();
                        if (errorCode != ListenerUnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER)
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(errorCode));
                        }
                    }
                    byte[] serviceStatusProcess = new byte[lpnLengthNeeded];
#pragma warning suppress 56523 // Microsoft, Win32Exception ctor calls Marshal.GetLastWin32Error()
                    success = ListenerUnsafeNativeMethods.QueryServiceStatusEx(service, ListenerUnsafeNativeMethods.SC_STATUS_PROCESS_INFO, serviceStatusProcess, serviceStatusProcess.Length, out lpnLengthNeeded);
                    if (!success)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception());
                    }
                    fixed(byte *pServiceStatusProcess = serviceStatusProcess)
                    {
                        return((ListenerUnsafeNativeMethods.SERVICE_STATUS_PROCESS)Marshal.PtrToStructure((IntPtr)pServiceStatusProcess, typeof(ListenerUnsafeNativeMethods.SERVICE_STATUS_PROCESS)));
                    }
                }
                finally
                {
                    service.Close();
                }
            }
            finally
            {
                scManager.Close();
            }
        }
Ejemplo n.º 7
0
 internal static extern bool QueryServiceStatusEx(SafeServiceHandle hService, int InfoLevel, [Out] byte[] pBuffer, int cbBufSize, out int pcbBytesNeeded);
Ejemplo n.º 8
0
 internal static extern bool QueryServiceStatus(SafeServiceHandle hService, out SERVICE_STATUS_PROCESS status);
Ejemplo n.º 9
0
 internal static extern bool QueryServiceConfig(SafeServiceHandle hService, [Out] byte[] pServiceConfig, int cbBufSize, out int pcbBytesNeeded);
 private static SafeServiceHandle OpenService(SafeServiceHandle scManager, string serviceName, int purpose)
 {
     SafeServiceHandle handle = ListenerUnsafeNativeMethods.OpenService(scManager, serviceName, purpose);
     if (handle.IsInvalid)
     {
         Exception exception = new Win32Exception();
         handle.SetHandleAsInvalid();
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception);
     }
     return handle;
 }
 internal static extern bool StartService(SafeServiceHandle hSCManager, int dwNumServiceArgs, string[] lpServiceArgVectors);
 internal static extern bool QueryServiceStatusEx(SafeServiceHandle hService, int InfoLevel, [Out] byte[] pBuffer, int cbBufSize, out int pcbBytesNeeded);
 internal static extern bool QueryServiceStatus(SafeServiceHandle hService, out SERVICE_STATUS_PROCESS status);
 internal static extern bool QueryServiceConfig(SafeServiceHandle hService, [Out] byte[] pServiceConfig, int cbBufSize, out int pcbBytesNeeded);
 internal static extern SafeServiceHandle OpenService(SafeServiceHandle hSCManager, string lpServiceName, int dwDesiredAccess);
Ejemplo n.º 16
0
 internal static extern bool StartService(SafeServiceHandle hSCManager, int dwNumServiceArgs, string[] lpServiceArgVectors);
Ejemplo n.º 17
0
 internal static extern SafeServiceHandle OpenService(SafeServiceHandle hSCManager, string lpServiceName, int dwDesiredAccess);
Ejemplo n.º 18
0
        static SafeServiceHandle OpenService(SafeServiceHandle scManager, string serviceName, int purpose)
        {
#pragma warning suppress 56523 // Microsoft, Win32Exception ctor calls Marshal.GetLastWin32Error()
            SafeServiceHandle service = ListenerUnsafeNativeMethods.OpenService(scManager, serviceName, purpose);
            if (service.IsInvalid)
            {
                Exception exception = new Win32Exception();
                service.SetHandleAsInvalid();
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception);
            }
            return service;
        }