kangyu-california / PersistentWindows

fork of http://www.ninjacrab.com/persistent-windows/ with windows 10 update
GNU General Public License v3.0
1.45k stars 50 forks source link

"The handle is invalid" exception and crash when restoring windows #181

Closed qhfreddy closed 1 year ago

qhfreddy commented 2 years ago

I am running W10 21H1 and recently with 5.34 and also after updating to 5.38 I am getting a "The handle is invalid" exception shortly after starting PW. It also appears to happen when capturing a snapshot.

The application worked fine for a long time and I am not sure what change prompted this error to pop up.

Exception text:

************** Exception Text **************
System.ComponentModel.Win32Exception (0x80004005): The handle is invalid
   at System.Diagnostics.EventLogInternal.InternalWriteEvent(UInt32 eventID, UInt16 category, EventLogEntryType type, String[] strings, Byte[] rawData, String currentMachineName)
   at System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)
   at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category)
   at Ninjacrab.PersistentWindows.Common.Diagnostics.Log.Event(String format, Object[] args) in H:\proj\PersistentWindows.orig\sourceCode\persistentwindows\Ninjacrab.PersistentWindows.Solution\Ninjacrab.PersistentWindows.Common\Diagnostics\Log.cs:line 87
   at Ninjacrab.PersistentWindows.Common.PersistentWindowProcessor.TakeSnapshot(Int32 snapshotId) in H:\proj\PersistentWindows.orig\sourceCode\persistentwindows\Ninjacrab.PersistentWindows.Solution\Ninjacrab.PersistentWindows.Common\PersistentWindowProcessor.cs:line 1432
   at Ninjacrab.PersistentWindows.SystrayShell.Program.CaptureSnapshot(Int32 id, Boolean prompt)
   at Ninjacrab.PersistentWindows.SystrayShell.SystrayForm.ClickTimerCallBack(Object source, ElapsedEventArgs e)

************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.4515.0 built by: NET48REL1LAST_C
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll
----------------------------------------
PersistentWindows
    Assembly Version: 5.38.8263.40891
    Win32 Version: 5.38.8263.40891
    CodeBase: file:///D:/Programs/persistentwindows/PersistentWindows.exe
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.4390.0 built by: NET48REL1LAST_C
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.4536.0 built by: NET48REL1LAST_C
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
Ninjacrab.PersistentWindows.Common
    Assembly Version: 1.1.0.0
    Win32 Version: 1.1.0.0
    CodeBase: file:///D:/Programs/persistentwindows/Ninjacrab.PersistentWindows.Common.DLL
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.4550.0 built by: NET48REL1LAST_C
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Configuration
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.4190.0 built by: NET48REL1LAST_B
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Core
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.4536.0 built by: NET48REL1LAST_C
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Xml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.4084.0 built by: NET48REL1
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
LiteDB
    Assembly Version: 5.0.9.0
    Win32 Version: 5.0.9
    CodeBase: file:///D:/Programs/persistentwindows/LiteDB.DLL
----------------------------------------
Anonymously Hosted DynamicMethods Assembly
    Assembly Version: 0.0.0.0
    Win32 Version: 4.8.4515.0 built by: NET48REL1LAST_C
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_64/mscorlib/v4.0_4.0.0.0__b77a5c561934e089/mscorlib.dll
----------------------------------------
System.IO.Compression.FileSystem
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.4084.0
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.IO.Compression.FileSystem/v4.0_4.0.0.0__b77a5c561934e089/System.IO.Compression.FileSystem.dll
----------------------------------------
kangyu-california commented 2 years ago

The issue is possibly caused by incomplete auto upgrade due to app crash in the Exit() call, I have removed the culprit hack code (hide icon before Exit()) just now.

To workaround the issue, please restart PersistentWindows, or manually upgrade to 5.38 (not click the upgrade menu) to guarantee a clean restart.

qhfreddy commented 2 years ago

The upgrade I did previously was a manual upgrade, even when removing all related files and clearing the appdata folder the crash still happens. It can still be caused directly with the "Capture a Snapshot" function with the following exception.

System.ComponentModel.Win32Exception (0x80004005): The handle is invalid
   at System.Diagnostics.EventLogInternal.InternalWriteEvent(UInt32 eventID, UInt16 category, EventLogEntryType type, String[] strings, Byte[] rawData, String currentMachineName)
   at System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)
   at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category)
   at Ninjacrab.PersistentWindows.Common.Diagnostics.Log.Event(String format, Object[] args) in H:\proj\PersistentWindows.orig\sourceCode\persistentwindows\Ninjacrab.PersistentWindows.Solution\Ninjacrab.PersistentWindows.Common\Diagnostics\Log.cs:line 87
   at Ninjacrab.PersistentWindows.Common.PersistentWindowProcessor.TakeSnapshot(Int32 snapshotId) in H:\proj\PersistentWindows.orig\sourceCode\persistentwindows\Ninjacrab.PersistentWindows.Solution\Ninjacrab.PersistentWindows.Common\PersistentWindowProcessor.cs:line 1432
   at Ninjacrab.PersistentWindows.SystrayShell.Program.CaptureSnapshot(Int32 id, Boolean prompt) in H:\proj\PersistentWindows.orig\sourceCode\persistentwindows\Ninjacrab.PersistentWindows.Solution\Ninjacrab.PersistentWindows.SystrayShell\Program.cs:line 335
   at Ninjacrab.PersistentWindows.SystrayShell.SystrayForm.CaptureSnapshotClickHandler(Object sender, EventArgs e) in H:\proj\PersistentWindows.orig\sourceCode\persistentwindows\Ninjacrab.PersistentWindows.Solution\Ninjacrab.PersistentWindows.SystrayShell\SystrayForm.cs:line 282
   at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
   at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
   at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
   at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ToolStrip.WndProc(Message& m)
   at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
kangyu-california commented 2 years ago

I have made a speculative fix, can you please try 5.39

qhfreddy commented 2 years ago

The behaviour appears unchanged. The application still fails to restore windows (from disk or automatically), crashing when it attempts to do so, and when using "Capture a Snapshot" the following Exception is generated.

System.ComponentModel.Win32Exception (0x80004005): The handle is invalid
   at System.Diagnostics.EventLogInternal.InternalWriteEvent(UInt32 eventID, UInt16 category, EventLogEntryType type, String[] strings, Byte[] rawData, String currentMachineName)
   at System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)
   at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category)
   at Ninjacrab.PersistentWindows.Common.Diagnostics.Log.Event(String format, Object[] args) in H:\proj\PersistentWindows.orig\sourceCode\persistentwindows\Ninjacrab.PersistentWindows.Solution\Ninjacrab.PersistentWindows.Common\Diagnostics\Log.cs:line 92
   at Ninjacrab.PersistentWindows.Common.PersistentWindowProcessor.TakeSnapshot(Int32 snapshotId) in H:\proj\PersistentWindows.orig\sourceCode\persistentwindows\Ninjacrab.PersistentWindows.Solution\Ninjacrab.PersistentWindows.Common\PersistentWindowProcessor.cs:line 1463
   at Ninjacrab.PersistentWindows.SystrayShell.Program.CaptureSnapshot(Int32 id, Boolean prompt) in H:\proj\PersistentWindows.orig\sourceCode\persistentwindows\Ninjacrab.PersistentWindows.Solution\Ninjacrab.PersistentWindows.SystrayShell\Program.cs:line 336
   at Ninjacrab.PersistentWindows.SystrayShell.SystrayForm.CaptureSnapshotClickHandler(Object sender, EventArgs e) in H:\proj\PersistentWindows.orig\sourceCode\persistentwindows\Ninjacrab.PersistentWindows.Solution\Ninjacrab.PersistentWindows.SystrayShell\SystrayForm.cs:line 289
   at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
   at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
   at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
   at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ToolStrip.WndProc(Message& m)
   at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
kangyu-california commented 2 years ago

It seems PersistentWindows does not get the permission to write to event log, and I have no clue how to reproduce it. Can you run PW with admin privilege? If PW would still fail, the workaround is to specify '-silent' as command line option to totally disable event logging.

qhfreddy commented 2 years ago

With admin privilege it still gives the error. It does seem to work fine using the -silent option.