public static bool RegisterRecovery()
 {
     if (!IsSupported)
     {
         throw new PlatformNotSupportedException();
     }
     if (FRecoveryEnabled)
     {
         throw new InvalidOperationException();
     }
     if (FRecovery == null)
     {
         throw new ArgumentException();
     }
     if (RecoveryCallbackHandler == null)
     {
         RecoveryCallbackHandler = new ApplicationRecoveryCallback(RestartAndRecoveryManager.RecoveryCallback);
     }
     int num = Windows.RegisterApplicationRecoveryCallback(RecoveryCallbackHandler, IntPtr.Zero, (uint) FPingInterval, 0);
     if (num == -2147024809)
     {
         throw new ArgumentException();
     }
     FRecoveryEnabled = num == 0;
     return FRecoveryEnabled;
 }
        /// <summary>
        /// Registers the application for notification by windows of a failure.
        /// </summary>
        /// <returns>0 if successfully registered for restart notification</returns>
        public static uint RegisterApplicationRecoveryCallback()
        {
            if (!IsWindowsVistaOrLater)
            {
                return(1);
            }
            //  By default, the interval is 5 seconds (RECOVERY_DEFAULT_PING_INTERVAL). The maximum interval is 5 minutes.
            _recoverApplication = new ApplicationRecoveryCallback(HandleApplicationCrash);
            IntPtr ptrOnApplicationCrash = Marshal.GetFunctionPointerForDelegate(_recoverApplication);
            var    result = CrashRecoveryApi.RegisterApplicationRecoveryCallback(ptrOnApplicationCrash, IntPtr.Zero, (int)TimeSpan.FromMinutes(5).TotalMilliseconds,
                                                                                 0);

            return(result);
        }
Esempio n. 3
0
        /// <summary>
        /// Registers the application for notification by windows of a failure.
        /// </summary>
        /// <returns>true if successfully registered for restart notification</returns>

        public static bool RegisterForRestart()
        {
            uint i = RegisterApplicationRestart(APPLICATION_CRASHED, RestartFlags.NONE);

            if (i == 0)
            {
                //Hook the callback function.
                RecoverApplication = new ApplicationRecoveryCallback(HandleApplicationCrash);
                IntPtr ptrOnApplicationCrash = Marshal.GetFunctionPointerForDelegate(RecoverApplication);

                i = RegisterApplicationRecoveryCallback(ptrOnApplicationCrash, IntPtr.Zero, 50000, 0);
            }

            return(i == 0);
        }
        public void RegisterRecovery()
        {
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode‌​.ThrowException);
            var    callback = new ApplicationRecoveryCallback(CreateRecoveryData);
            IntPtr del      = Marshal.GetFunctionPointerForDelegate(callback);

            RegisterApplicationRecoveryCallback(del, IntPtr.Zero, 5000, 0);

            if (_appName == null)
            {
                using (var process = Process.GetCurrentProcess())
                {
                    var path = process.MainModule.FileName;
                    _appName    = Path.GetFileNameWithoutExtension(path);
                    _appCompany = FileVersionInfo.GetVersionInfo(path).CompanyName;
                    if (string.IsNullOrWhiteSpace(_appName))
                    {
                        _appCompany = "{EC8B5FBC-2565-4CBD-9289-0000F25C1DFB}";
                    }
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Must be called once and only once.
        /// Registers the application for notification by windows of a failure if <see cref="IsOSRecoveryAvailable"/> is true.
        /// Initializes the <see cref="CrashLogManager"/> with the given directory.
        /// </summary>
        /// <returns>True if successfully registered for restart notification. False otherwise.</returns>
        /// <remarks>
        /// <para>
        /// The <see cref="AppDomain.CurrentDomain.UnhandledException"/> event is listened and react to such unhandled exceptions
        /// int two different ways: If <see cref="IsOSRecoveryAvailable"/> is true, the exception is memorized and will be dumped out
        /// to the crash log during application recovery. If it is false, this immediately triggers the <see cref="ApplicationCrashed"/>
        /// event.
        /// </para>
        /// <para>
        /// Windows forms is configured to let unhandled execptions be thrown thanks to a call to <see cref="Application.SetUnhandledExceptionMode"/> with <see cref="UnhandledExceptionMode.ThrowException"/>.
        /// </para>
        /// <para>
        /// This method also initializes the <see cref="CurrentCrashCount"/> from the <see cref="Environment.GetCommandLineArgs"/> (if any).
        /// </para>
        /// </remarks>
        public static bool Initialize(string crashLogDirectory)
        {
            CrashLogManager.Initialize(crashLogDirectory);
            Debug.Assert(_registeredExceptions == null, "Since CrashLogManager.Initialize above is called once and only once.");
            try
            {
                _registeredExceptions = new List <string>();
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
                System.Windows.Forms.Application.SetUnhandledExceptionMode(System.Windows.Forms.UnhandledExceptionMode.ThrowException);
                if (!CK.Core.OSVersionInfo.IsWindowsVistaOrGreater)
                {
                    return(_isOSRecoveryAvailable = false);
                }

                string[] args = Environment.GetCommandLineArgs();
                if (args != null && args.Length == 3 && args[1] == _crashcountParameterName)
                {
                    int.TryParse(args[2], out _crashCount);
                }
                // TODO: Parameters passing is far from perfect here. One should better reinject the current parameters and append the crash count.
                uint i = RecoveryFunctions.RegisterApplicationRestart(String.Format("{0} {1}", _crashcountParameterName, _crashCount + 1), RestartFlags.NONE);
                if (i == 0)
                {
                    // Hook the callback function.
                    _recoverCallback = new ApplicationRecoveryCallback(HandleApplicationCrashByOS);
                    IntPtr ptrOnApplicationCrash = Marshal.GetFunctionPointerForDelegate(_recoverCallback);
                    i = RecoveryFunctions.RegisterApplicationRecoveryCallback(ptrOnApplicationCrash, IntPtr.Zero, 50000, 0);
                    return(_isOSRecoveryAvailable = true);
                }
            }
            catch (Exception ex)
            {
                CrashLogManager.CreateNew().WriteLineException(ex);
            }
            return(false);
        }
Esempio n. 6
0
 internal static extern int RegisterApplicationRecoveryCallback(ApplicationRecoveryCallback pRecoveryCallback, IntPtr pvParameter, uint dwPingInterval, uint dwFlags);
Esempio n. 7
0
 internal static extern int GetApplicationRecoveryCallback(IntPtr hProcess, out ApplicationRecoveryCallback pRecoveryCallback, out IntPtr ppvParameter, out uint dwPingInterval, out uint dwFlags);
Esempio n. 8
0
 /// <summary>
 /// Registers the application for notification by windows of a failure if <see cref="IsOSRecoveryAvailable"/> is true.
 /// </summary>
 /// <returns>True if successfully registered for restart notification. False otherwise.</returns>   
 /// <remarks>
 /// <para>
 /// The <see cref="AppDomain.CurrentDomain.UnhandledException"/> event is listened and react to such unhandled exceptions
 /// int two different ways: If <see cref="IsOSRecoveryAvailable"/> is true, the exception is memorized and will be dumped out
 /// to the crash log during application recovery. If it is false, this immediately triggers the <see cref="ApplicationCrashed"/>
 /// event.
 /// </para>
 /// <para>
 /// Windows forms is configured to let unhandled execptions be thrown thanks to a call to <see cref="Application.SetUnhandledExceptionMode"/> with <see cref="UnhandledExceptionMode.ThrowException"/>.
 /// </para>
 /// <para>
 /// This method also initializes the <see cref="CurrentCrashCount"/> from the <see cref="Environment.GetCommandLineArgs"/> (if any).
 /// </para>
 /// </remarks>
 public static bool Initialize()
 {
     if( _registeredExceptions != null ) return _isOSRecoveryAvailable;
     _registeredExceptions = new List<string>();
     System.Windows.Forms.Application.SetUnhandledExceptionMode( System.Windows.Forms.UnhandledExceptionMode.ThrowException );
     AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler( CurrentDomain_UnhandledException );
     if( !CK.Core.OSVersionInfo.IsWindowsVistaOrGreater ) return _isOSRecoveryAvailable = false;
     string[] args = Environment.GetCommandLineArgs();
     if( args != null && args.Length == 3 && args[1] == _crashcountParameterName )
     {
         int.TryParse( args[2], out _crashCount );
     }
     // TODO: Parameters passing is far from perfect here. One should better reinject the current paramaters and append the crash count.
     uint i = RecoveryFunctions.RegisterApplicationRestart( String.Format( "{0} {1}", _crashcountParameterName, _crashCount + 1 ), RestartFlags.NONE );
     if( i == 0 )
     {
         // Hook the callback function.
         _recoverCallback = new ApplicationRecoveryCallback( HandleApplicationCrashByOS );
         IntPtr ptrOnApplicationCrash = Marshal.GetFunctionPointerForDelegate( _recoverCallback );
         i = RecoveryFunctions.RegisterApplicationRecoveryCallback( ptrOnApplicationCrash, IntPtr.Zero, 50000, 0 );
     }
     return _isOSRecoveryAvailable = i == 0;
 }
Esempio n. 9
0
        private static void SetupRecovery()
        {
            crashCallback = new ApplicationRecoveryCallback(CrashCallback);

            IntPtr cb = Marshal.GetFunctionPointerForDelegate(crashCallback);

            RegisterApplicationRecoveryCallback(cb, IntPtr.Zero, 6000, 0);
        }
Esempio n. 10
0
 public static extern int RegisterApplicationRecoveryCallback(ApplicationRecoveryCallback pRecoveryCallback, IntPtr pvParameter, uint dwPingInterval, uint dwFlags);
Esempio n. 11
0
 public static extern int GetApplicationRecoveryCallback(IntPtr hProcess, out ApplicationRecoveryCallback pRecoveryCallback, out IntPtr ppvParameter, out uint pdwPingInterval, out int pdwFlags);