Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
I set a mouse hook to hold up the message WM_LBUTTONUP,but to my suprive ,the hook proc is executed once again.why does it happen ?

[EDIT CHill60 - OP Code from Solution]
public partial class Form1 : Form
{
   MOUSEHOOK mouseHook = new MOUSEHOOK();
   MOUSEHOOK.HookHandle hookhandle = null;
   MOUSEHOOK.MouseHookStruct mhs = new MOUSEHOOK.MouseHookStruct();
       
   public Form1()
   {
      InitializeComponent();
   }
   private void Form1_Load(object sender, EventArgs e)
   {
      hookhandle = new MOUSEHOOK.HookHandle(HookProc);
      if (true == mouseHook.InstallHook(hookhandle))
      {
         Console.WriteLine("successed");
      }
      Console.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString());//10 
      Console.WriteLine(AppDomain.GetCurrentThreadId().ToString());//2588
   }
 
   //hook procedure 
   int HookProc(int nCode, int wParam, IntPtr lParam)
   {
      Marshal.PtrToStructure(lParam, mhs);
      switch (wParam)
      {
         case MOUSEHOOK.WM_LBUTTONUP:
            Console.WriteLine("how many times to enter?");//here print the sentence two times,
            break;
         default :
            break;
      }
 
      return MOUSEHOOK.CallNextHookEx(0, nCode, wParam, lParam);
   }
}

//hook class
public class MOUSEHOOK
{
   [DllImport("user32.dll")]
   private static extern int SetWindowsHookEx(int idHook, HookHandle HookProc, IntPtr hMod, int dwThreadId);
   //cancel
   [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
   private static extern bool UnhookWindowsHookEx(int idHook);
   //call next hook proc
   [DllImport("user32.dll")]
   public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);
   [DllImport("kernel32.dll")]
   public static extern int GetCurrentThreadId();
 
   [StructLayout(LayoutKind.Sequential)]
   public class POINT
   {
      public int x;
      public int y;
   }
   [StructLayout(LayoutKind.Sequential)]
   public class MouseHookStruct
   {
      public POINT pt;
      public int hwnd;
      public int wHitTestCode;
      public int dwExtraInfo;
   }
 
   public const int WH_MOUSE = 7;
   public const int WM_MOUSEMOVE = 0x200;
   public const int WM_LBUTTONDOWN = 0x201;
   public const int WM_LBUTTONUP = 0x202;
   public const int WM_NCLBUTTONDOWN = 0xA1;
   public const int WM_NCLBUTTONUP = 0xA2;
   public const int WM_NCMOUSEMOVE = 0xA0;
   public MouseHookStruct mhs = new MouseHookStruct();
   public int hhook;
   public delegate int HookHandle(int nCode, int wParam, IntPtr lParam);
 
   public bool InstallHook(HookHandle MouseProc)
   {
      hhook = SetWindowsHookEx(WH_MOUSE, MouseProc, IntPtr.Zero, GetCurrentThreadId());//AppDomain.GetCurrentThreadId()
      if (hhook != 0)
         return true;
      return false;
   }
   public void UnInstallHook()
   {
      bool ret = false;
      if (hhook != 0)
         ret = UnhookWindowsHookEx(hhook);
      if (ret)
         hhook = 0;
   }
}

[Edit - OP's comment in a solution removed]
[Edit 2 - Indentation reduced and corrected in some places]
Posted
Updated 11-May-14 6:44am
v5
Comments
[no name] 7-May-14 9:36am    
"connection with .net environment ?", probably not. Most likely has everything to do with your code.
Sergey Alexandrovich Kryukov 7-May-14 9:38am    
...and without seeing the code sample...
—SA
[no name] 7-May-14 9:51am    
first where is your problem we couldn't understand. Tell it briefly
codeprojectddx 7-May-14 10:20am    
Enter the mouse hook proc two times.the codes below:
CHill60 7-May-14 10:51am    
By putting your code in a Solution you made your question drop out of the "Unanswered Questions" list. I've amended your question using the "Improve Question" link and deleted the solution you posted. More people will now see your question and you won't get more downvotes

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900