在安装钩子(WH_MOUSE)的时候使用了SetWindowsHookEx方法,其中最后一个参数是关联的线程ID(MSDN说明:Specifies the identifier of the thread with which the hook procedure is to be associated. If this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread.)。我使用了AppDomain.GetCurrentThreadId()方法运行正常,就是编译器提示此方法过时了要我改用Thread类的ManagedThreadId属性,我在Thread.CurrentThread.ManagedThreadId下找到了此属性,但是安装钩子不能成功,最后发现Thread.CurrentThread.ManagedThreadId和AppDomain.GetCurrentThreadId()返回的结果根本不一样??我就奇怪了,请问各位怎么解决呢??谢谢!!!

解决方案 »

  1.   

     ProcessThreadCollection ptCollection = Process.GetCurrentProcess().Threads;
       foreach (ProcessThread pt in ptCollection)
          {
                Console.WriteLine("ID:{0",pt.Id);
            }
      

  2.   

    安装全局WH_MOUSE的时候需要把Dll注入到远程空间。
    由于C#没有办法直接做出可以导出函数的Dll以供回调,C#程序并不能安装全局WH_MOUSE钩子。
    拿不拿的到线程ID已经无关紧要了。你可以把WH_MOUSE改为WH_MOUSE_LL,后者的回调并不运行在远程进程,而是通过上下文切换(context switch)到安装者的线程下执行。
    因此C#程序可以安装WH_MOUSE_LL。