Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AccessViolationException on Windows 11 #21

Open
LAB02-Admin opened this issue Nov 8, 2021 · 5 comments
Open

AccessViolationException on Windows 11 #21

LAB02-Admin opened this issue Nov 8, 2021 · 5 comments
Labels
requires testing Requires some client-side tests for feedback

Comments

@LAB02-Admin
Copy link

I've been using your nuget for a while on W10, works really great, thanks for sharing :)

Recently I made the switch to W11, and now when using the same code, I get the following exception:

[MAIN] AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at MS.Internal.Automation.UiaCoreApi.RawTextRange_GetText(SafeTextRangeHandle hobj, Int32 maxLength, String& result)
   at System.Windows.Automation.Text.TextPatternRange.GetText(Int32 maxLength)
   at WK.Libraries.HotkeyListenerNS.Helpers.TextSelectionReader.<>c.<GetTextFromAutomationElement>b__5_0(TextPatternRange r)
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
   at System.String.Join(String separator, IEnumerable`1 values)
   at WK.Libraries.HotkeyListenerNS.Helpers.TextSelectionReader.GetTextFromAutomationElement()
   at WK.Libraries.HotkeyListenerNS.Helpers.TextSelectionReader.<.ctor>b__0_0()
   at WK.Libraries.HotkeyListenerNS.Helpers.TextSelectionReader.TryGetSelectedTextFromActiveControl()
   at WK.Libraries.HotkeyListenerNS.Helpers.SourceAttributes.GetSelection()
   at WK.Libraries.HotkeyListenerNS.Helpers.HotkeyHandle.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at Program.Main()

It looks like the TextSelectionReader is causing a crash, perhaps it's breaking some security measures.

Do you have the time to look into this, or could you give me some pointers on how to remove/disable the TextSelectionReader from your source?

Thanks!

@LAB02-Admin
Copy link
Author

Update: I've removed TextSelectionReader.cs and its references, and changed SourceAttributes.GetSelection() to just return an empty string. This resolved the crash.

@Willy-Kimura
Copy link
Owner

Thank you for reporting this. I'll be sure to look into it.

In the meantime, checkout this SO thread.

@Willy-Kimura Willy-Kimura added the requires testing Requires some client-side tests for feedback label Jan 15, 2022
@LAB02-Admin
Copy link
Author

Yea it's probably a x86/x64 thing. Not an issue anymore when not looking into the target application through GetSelection(), perhaps make it optional/configurable?

@Willy-Kimura
Copy link
Owner

Sure, I can work on that.

@tGecko
Copy link

tGecko commented Aug 8, 2023

Can confirm this issue on Win 11 22621.1848
My program using the HKL class crashes when the hotkey is pressed while certain programs are in focus. (MS Edge, Outlook are two examples)

This function is what crashes the application with a System.AccessViolationException as it is trying to access secure memory

commenting out this function here works for me as a workaround

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
requires testing Requires some client-side tests for feedback
Projects
None yet
Development

No branches or pull requests

3 participants