Click here to Skip to main content
15,921,062 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the situation is that,when I operate a mouse hook,I find that there are two types of current thread id ,codes below:
Thread.CurrentThread.ManagedThreadId
AppDomain.GetCurrentThreadId()
the ids are defferent with each other,why?

Entering the mouse hook procedure two times,codes below:
C#
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;
    }
}
Posted
Updated 6-May-14 17:06pm
v3

check AppDomain.GetCurrentThreadId Depreciated[^] and
Replacing AppDomain.GetCurrentThreadId(); with ManagedThreadId[^]

if you want to get a id for a thread to pass to unmanaged, pinvoke GetCurrentThreadId() and it will bypass the obsoletion warning.
C#
[DllImport("kernel32.dll")]
public static extern int GetCurrentThreadId();
 
Share this answer
 
thank you ,Damith,I have a doubt that when I unstall a mouse hook,when I entercept the message WM_LBUTTONUP ,the hook proc will be executed two times . Then I change the forth parameter in the SetWindowsHookEx function,but the question exists always.
 
Share this answer
 

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