Beispiel #1
0
        public static IntPtr ApiOpenService(string sServicename, uint dwDesiredAccess)
        {
            try
            {
                if (phSCManager == IntPtr.Zero)
                {
                    ApiOpnSCManager();
                }

                Logger.Log(string.Format("ServiceManagerWindowsWrapper.ApiOpenService(sHostname={0}, sServicename={1})", System.Environment.MachineName, sServicename), Logger.ServiceManagerLoglevel);

                if (phService != IntPtr.Zero)
                {
                    ApiCloseServiceHandle(phService);
                }

                phService = ServiceManagerInteropWindows.OpenService(phSCManager, sServicename, dwDesiredAccess);

                if (phService == IntPtr.Zero)
                {
                    Logger.Log("ServiceManagerWindowsWrapper.ApiOpenService returns invalid handle", Logger.ServiceManagerLoglevel);
                }
                Logger.Log("ServiceManagerWindowsWrapper.ApiOpenService is Success", Logger.ServiceManagerLoglevel);
            }
            catch (Exception ex)
            {
                phService = IntPtr.Zero;
                Logger.LogException("ServiceManagerWindowsWrapper.ApiOpenService()", ex);
            }

            return(phService);
        }
Beispiel #2
0
        public static bool ApiCloseServiceHandle(IntPtr pServiceHandle)
        {
            bool bRet = false;

            try
            {
                Logger.Log(string.Format("ServiceManagerWindowsWrapper.ApiCloseServiceHandle()"), Logger.ServiceManagerLoglevel);

                bRet = ServiceManagerInteropWindows.CloseServiceHandle(pServiceHandle);

                if (pServiceHandle != IntPtr.Zero)
                {
                    Logger.Log("ServiceManagerWindowsWrapper.ApiCloseServiceHandle returns invalid handle", Logger.ServiceManagerLoglevel);
                }

                phService = IntPtr.Zero;
                Logger.Log("ServiceManagerWindowsWrapper.ApiCloseServiceHandle is Success", Logger.ServiceManagerLoglevel);
            }
            catch (Exception ex)
            {
                Logger.LogException("ServiceManagerWindowsWrapper.ApiCloseServiceHandle()", ex);
            }

            return(bRet);
        }
Beispiel #3
0
        public static int ApiOpnSCManager()
        {
            string sHostname = System.Environment.MachineName;

            Logger.Log(string.Format("ServiceManagerWindowsWrapper.ApiOpnSCManager(sHostname={0})", sHostname), Logger.ServiceManagerLoglevel);

            try
            {
                if (phSCManager == IntPtr.Zero)
                {
                    phSCManager = ServiceManagerInteropWindows.OpenSCManager(sHostname, null, (uint)ServiceManagerInteropWindows.SC_MANAGER_ALL_ACCESS);
                }

                if (phSCManager == IntPtr.Zero)
                {
                    Logger.Log("ServiceManagerWindowsWrapper.ApiOpnSCManager returns invalid handle", Logger.ServiceManagerLoglevel);
                }
                Logger.Log("ServiceManagerWindowsWrapper.ApiOpnSCManager is Success", Logger.ServiceManagerLoglevel);
            }
            catch (Exception ex)
            {
                phSCManager = IntPtr.Zero;
                Logger.LogException("ServiceManagerWindowsWrapper.ApiOpnSCManager()", ex);
            }

            return(phSCManager != IntPtr.Zero ? 0 : -1);
        }
Beispiel #4
0
        public static ServiceManagerApi.SERVICE_FAILURE_ACTIONS ApiQueryServiceConfig2(
            string sServicename)
        {
            UInt32 dwBytesNeeded;

            //UInt32 INFINITE = 0xFFFFFFFF;
            ServiceManagerApi.SERVICE_FAILURE_ACTIONS failureActions = new ServiceManagerApi.SERVICE_FAILURE_ACTIONS();
            try
            {
                ApiOpenService(sServicename, ServiceManagerInteropWindows.SERVICE_QUERY_CONFIG);

                Logger.Log(string.Format("ServiceManagerWindowsWrapper.ApiQueryServiceConfig2(sHostname={0})", sServicename), Logger.ServiceManagerLoglevel);

                bool bSuccess = ServiceManagerInteropWindows.QueryServiceConfig2(phService,
                                                                                 ServiceManagerInteropWindows.SERVICE_CONFIG_FAILURE_ACTIONS,
                                                                                 IntPtr.Zero, 0, out dwBytesNeeded);
                if (!bSuccess)
                {
                    Logger.Log("ServiceManagerWindowsWrapper.ApiOpenService returns false", Logger.ServiceManagerLoglevel);
                }

                IntPtr ptr = Marshal.AllocHGlobal((int)dwBytesNeeded);
                bSuccess = ServiceManagerInteropWindows.QueryServiceConfig2(phService,
                                                                            ServiceManagerInteropWindows.SERVICE_CONFIG_FAILURE_ACTIONS,
                                                                            ptr, dwBytesNeeded, out dwBytesNeeded);

                Marshal.PtrToStructure(ptr, failureActions);

                ApiCloseServiceHandle(phService);

                Logger.Log("ServiceManagerWindowsWrapper.ApiOpnSCManager is Success", Logger.ServiceManagerLoglevel);
            }
            catch (Exception ex)
            {
                Logger.LogException("ServiceManagerWindowsWrapper.ApiOpnSCManager()", ex);
            }

            return(failureActions);
        }
Beispiel #5
0
        public static bool ApiChangeServiceConfig2(string sServicename,
                                                   ServiceManagerApi.SERVICE_FAILURE_ACTIONS failureActions)
        {
            try
            {
                ApiOpenService(sServicename, ServiceManagerInteropWindows.SERVICE_CHANGE_CONFIG);

                IntPtr lpInfo = Marshal.AllocHGlobal(Marshal.SizeOf(failureActions));
                if (lpInfo == IntPtr.Zero)
                {
                    throw new Exception(String.Format("Unable to allocate memory, error was: 0x{0:X}", Marshal.GetLastWin32Error()));
                }

                Marshal.StructureToPtr(failureActions, lpInfo, false);

                if (!ServiceManagerInteropWindows.ChangeServiceConfig2(phService, (int)ServiceManagerInteropWindows.SERVICE_CONFIG_FAILURE_ACTIONS, lpInfo))
                {
                    Marshal.FreeHGlobal(lpInfo);
                    Logger.Log(String.Format("Error setting service config, error was: 0x{0:X}", Marshal.GetLastWin32Error()));
                    return(false);
                }

                Marshal.FreeHGlobal(lpInfo);

                Logger.Log("MyService: Service modification completed");

                ApiCloseServiceHandle(phService);
            }
            catch (Exception ex)
            {
                Logger.LogException("ApiChangeServiceConfig2()", ex);
                return(false);
            }

            return(true);
        }