none
MouseKeyHook crashes C# RRS feed

  • Question

  • Hi

    I'm trying to use the MouseKeyHook class in my program but after a while running it crashes and I get various of error names for the MouseKeyHook .dll crashing.

    This is the line that crashes:

    Application.Run(new TheContext());

    Any ideas what could cause the crash? (I tried both x86 & x64 builds but both crash)

    I'm using Visual studio community 2017

    thanks!

    (Sorry if this is in wrong forum, please move)


    • Edited by Jy8 Monday, March 11, 2019 3:18 PM
    Monday, March 11, 2019 3:16 PM

Answers

  • Hi

    I was given answer in here: https://stackoverflow.com/questions/55206255/crash-in-mousekeyhook

    • Marked as answer by Jy8 Monday, March 18, 2019 10:17 AM
    Monday, March 18, 2019 10:17 AM

All replies

  • Hi Jy8,

    Thank you for posting here.

    For your question, what is the error message? Could you provide a code sample which could reproduce the error for us to test?

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 12, 2019 5:17 AM
    Moderator
  • The error code:

    System.AccessViolationException
      HResult=0x80004003

    And my source code:

    Here's my program.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Threading;
    
    namespace MouseFixer
    {
        static class Program
        {
    
            static Mutex pmu;
    
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
    
                // Console.WriteLine("Hello");
                
                try
                {
                    Mutex.OpenExisting("MouseFixer");
    
                    MessageBox.Show("MouseFixer is already running", "", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    return;
                }
                catch
                {
                    pmu = new Mutex(true, "MouseFixer");
                }
    
                Application.Run(new TheContext());
    
            }
    
    
        }
    }


    And here's my TheContext.cs

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    using Gma.System.MouseKeyHook;
    
    using System.Diagnostics;
    
    
    namespace MouseFixer
    {
        public class TheContext : ApplicationContext
        {
    
            // https://stackoverflow.com/questions/30061813/intercept-mouse-click
    
            StringBuilder logText;
    
            long lastClickTime;
            long lastMouseUp;
            bool ignoreClick = false;
    
            void writeLog(string msg)
            {
                logText.Append(msg + Environment.NewLine);
                File.AppendAllText("log.txt", logText.ToString());
                logText.Clear();
            }
    
            bool errorShown = false;
            void errorMsg(string str)
            {
                if(!errorShown)
                MessageBox.Show(str);
    
                errorShown = true;
            }
    
            long getTime()
            {
               return DateTimeOffset.Now.ToUnixTimeMilliseconds();
            }
    
            public TheContext()
            {
                
                Application.ApplicationExit += new EventHandler(this.OnExit);
    
                logText = new StringBuilder();
    
                lastClickTime = getTime();
                lastMouseUp = getTime();
    
                Hook.GlobalEvents().MouseDownExt += async (sender, e) =>
                {
                    if (e.Button == MouseButtons.Left)
                    {
                        //  e.Handled = true;
    
                        //  writeLog("Handling click DOWN! " + e.Delta);
    
                        long lmu = (getTime() - lastMouseUp);
    
                        if (lmu < 10)
                        {
                            Debug.WriteLine("Too fast click - ignoring " + (getTime() - lastMouseUp) + Environment.NewLine);
                            e.Handled = true;
                            ignoreClick = true;
                        }
    
                        long lct = getTime() - lastClickTime;
    
                        lastClickTime = getTime();
    
                        Debug.WriteLine("MouseDOWN " + lct + " ( " + lmu + " ) " + Environment.NewLine);
                    }
                };
    
                Hook.GlobalEvents().MouseUpExt += async (sender, e) =>
                {
                    if (e.Button == MouseButtons.Left)
                    {
                        if (!ignoreClick)
                        {
    
                            //  e.Handled = true;
    
                            //    writeLog("Handling click UP! " + e.Delta);
    
                            long lct = getTime() - lastClickTime;
    
                            lastClickTime = getTime();
    
                            Debug.WriteLine("MouseUP " + lct + Environment.NewLine);
    
                            lastMouseUp = getTime();
    
    
                        }
                        else
                        {
                            Debug.WriteLine("Ignoring click " + Environment.NewLine);
    
                            e.Handled = true;
                            ignoreClick = false;
                        }
                    }
                };
    
    
                    }
    
            private void OnExit(object sender, EventArgs e)
            {
              //  File.AppendAllText("log.txt", logText.ToString());
            }
    
    
        }
    
    }



    • Edited by Jy8 Tuesday, March 12, 2019 10:53 AM
    Tuesday, March 12, 2019 10:52 AM
  • It's not easy to reproduce this bug because it takes 10 - 60 minutes to crash when debugging
    Tuesday, March 12, 2019 4:45 PM
  • Hi Jy8,

    I try to test the code your provided, but I could not reproduce the error.

    For this error, only the error message could not helpful me. Is there any details? What does this code doing for? Maybe we could find a new way to do that. 

    When it thrown the error, do you know which line of the code thrown it?

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 14, 2019 6:32 AM
    Moderator
  • @Wendy

    Sorry I don't have much details, it's the line that crashes that I pointed out in my first post. Like I said the crash is in the .dll so I have no trace or anything.

    What the code is supposed to do is ignore unwanted double clicks. Some mouses send the double click even they are clicked only once. And it works great but the crashing is now the problem

    Thursday, March 14, 2019 9:26 AM
  • Hi Jy8,

    We could not give you some suggestions. Maybe the developer of this dill would be helpful.

    Please post you errors with details in Issues of the link below.

    https://github.com/gmamaladze/globalmousekeyhook/issues

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 18, 2019 7:31 AM
    Moderator
  • Hi

    I was given answer in here: https://stackoverflow.com/questions/55206255/crash-in-mousekeyhook

    • Marked as answer by Jy8 Monday, March 18, 2019 10:17 AM
    Monday, March 18, 2019 10:17 AM