Example #1
0
        private void RegisterDeviceNotification()
        {
            myCallback = new Win32.ServiceControlHandlerEx(ServiceControlHandler);
            Win32.RegisterServiceCtrlHandlerEx(this.ServiceName, myCallback, IntPtr.Zero);

            if (this.ServiceHandle == IntPtr.Zero)
            {
                // TODO handle error
            }

            Win32.DEV_BROADCAST_DEVICEINTERFACE deviceInterface = new Win32.DEV_BROADCAST_DEVICEINTERFACE();
            int size = Marshal.SizeOf(deviceInterface);

            deviceInterface.dbcc_size       = size;
            deviceInterface.dbcc_devicetype = Win32.DBT_DEVTYP_DEVICEINTERFACE;
            IntPtr buffer = default(IntPtr);

            buffer = Marshal.AllocHGlobal(size);
            Marshal.StructureToPtr(deviceInterface, buffer, true);
            deviceEventHandle = Win32.RegisterDeviceNotification(this.ServiceHandle, buffer, Win32.DEVICE_NOTIFY_SERVICE_HANDLE | Win32.DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
            if (deviceEventHandle == IntPtr.Zero)
            {
                // TODO handle error
            }
        }
        public bool Start(HostControl hostControl)
        {
            this.startHostControl = hostControl;

            myCallback = new Win32.ServiceControlHandlerEx(ServiceControlHandler);
            var serviceStatusHandle = Win32.RegisterServiceCtrlHandlerEx(DummyService.ServiceName,
                                                                            myCallback,
                                                                            IntPtr.Zero);
            var deviceInterface = new Win32.DEV_BROADCAST_DEVICEINTERFACE();
            int size = Marshal.SizeOf(deviceInterface);
            deviceInterface.dbcc_size = size;
            deviceInterface.dbcc_devicetype = Win32.DBT_DEVTYP_DEVICEINTERFACE;
            IntPtr buffer = default(IntPtr);
            buffer = Marshal.AllocHGlobal(size);
            Marshal.StructureToPtr(deviceInterface, buffer, true);
            deviceEventHandle = Win32.RegisterDeviceNotification(serviceStatusHandle,
                                                                    buffer,
                                                                    Win32.DEVICE_NOTIFY_SERVICE_HANDLE |
                                                                    Win32.DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
            if (deviceEventHandle == IntPtr.Zero)
            {
                // TODO handle error
            }
            return true;
        }
Example #3
0
 private void RegisterForServiceControlHandler()
 {
     serviceControlHandlerCallback = new Win32.ServiceControlHandlerEx(ServiceControlHandler);
     Win32.RegisterServiceCtrlHandlerEx(this.ServiceName, serviceControlHandlerCallback, IntPtr.Zero);
     if (this.ServiceHandle == IntPtr.Zero)
     {
         String error = "Error occured while registering for ServiceControlHandler";
         _notifier.Notify(error, false);
         throw new Exception(error);
     }
 }
Example #4
0
        protected override void OnStart(string[] args)
        {
            System.Threading.Thread.Sleep(10000);
            //AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            myCallback = new Win32.ServiceControlHandlerEx(ServiceControlHandler);
            Win32.RegisterServiceCtrlHandlerEx(this.ServiceName, myCallback, IntPtr.Zero);

            if (this.ServiceHandle == IntPtr.Zero)
            {
                // TODO handle error
            }

            dt = DateTime.Now;
            sc = new ServiceControl();
            try
            {
                sc.start();
            }
            catch (Exception e)
            {
                try
                {
                    Service.ServiceBussiness.SendMail();
                }
                catch (Exception ex)
                {
                    TimeSpan ts1      = new TimeSpan(ServiceControl.StartTime.Ticks);
                    TimeSpan ts2      = new TimeSpan(DateTime.Now.Ticks);
                    TimeSpan ts       = ts1.Subtract(ts2).Duration();
                    string   dateDiff = ts.Days.ToString() + "天" + ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分钟" + ts.Seconds.ToString() + "秒";

                    ServiceControl.log.Error(DateTime.Now + "系统运行时长:" + dateDiff + "服务启动,发送异常邮件时异常,异常原因:" + ex.ToString());
                }
                throw e;
            }
        }
Example #5
0
        private void RegisterDeviceNotification()
        {
            myCallback = new Win32.ServiceControlHandlerEx(ServiceControlHandler);
            Win32.RegisterServiceCtrlHandlerEx(this.ServiceName, myCallback, IntPtr.Zero);

            if (this.ServiceHandle == IntPtr.Zero)
            {
                // TODO handle error
            }

            Win32.DEV_BROADCAST_DEVICEINTERFACE deviceInterface = new Win32.DEV_BROADCAST_DEVICEINTERFACE();
            int size = Marshal.SizeOf(deviceInterface);
            deviceInterface.dbcc_size = size;
            deviceInterface.dbcc_devicetype = Win32.DBT_DEVTYP_DEVICEINTERFACE;
            IntPtr buffer = default(IntPtr);
            buffer = Marshal.AllocHGlobal(size);
            Marshal.StructureToPtr(deviceInterface, buffer, true);
            deviceEventHandle = Win32.RegisterDeviceNotification(this.ServiceHandle, buffer, Win32.DEVICE_NOTIFY_SERVICE_HANDLE | Win32.DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
            if (deviceEventHandle == IntPtr.Zero)
            {
                // TODO handle error
            }
        }