您的位置:程序门 -> .net技术 -> vb.net



vb.net真的不支持全局钩子???


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


vb.net真的不支持全局钩子???[已结贴,结贴人:wyndham]
发表于:2007-03-07 21:20:42 楼主
想在vb.net中实现wh_keyboard_ll和wh_mouse_ll的全局钩子。

我在网上已经找了很久,发现这两种钩子vb6和c#均可实现,唯独vb.net不行!vb6中有app.hinstance可用,vb.net没有,用“等效”的东东去替代,却不能达到同样的效果。

用hookproc   =   setwindowshookex(wh_keyboard,   addressof   mykeyboardproc,   0,   getcurrentthreadid())
可以轻松实现线程钩子,而无论如何wh_keyboard_ll都实现不了,我试过下面几种方法:

1.hookproc   =   setwindowshookex(wh_keyboard_ll,   new   hookproc(addressof   mykeyboardproc),   marshal.gethinstance([assembly].getEXECutingassembly.getmodules()(0)).toint32,   0)

2.hookproc   =   setwindowshookex(wh_keyboard_ll,   addressof   mykeyboardproc,   0,   getcurrentthreadid())

3.hookproc   =   setwindowshookex(wh_keyboard_ll,   addressof   mykeyboardproc,   marshal.gethinstance([assembly].getEXECutingassembly().getmodules()(0)),   0)

4.hookproc   =   setwindowshookex(wh_keyboard_ll,   new   hookproc(addressof   mykeyboardproc),   compatibility.vb6.gethinstance.toint32(),   0)

hookproc始终为0.

不知是否有高人已经解决此问题?如果真是不行,是否有其它方法来获知键盘和鼠标的全部活动?
发表于:2007-03-07 23:17:111楼 得分:0
可以的.你把钩子类作为一个项目,编译成dll,   再给别的引用.
如果代码同在一个项目上,则没有效果.
发表于:2007-03-08 00:14:552楼 得分:100
谁说不支持了,

imports   system.runtime.interopservices
public   class   myhook

        private   m_ikeyhandle   as   integer   =   0
        private   m_clshookcallback   as   hookcallback

        private   const   hc_action   as   integer   =   0
        private   const   wm_key_enter   as   integer   =   &hd
        private   const   wh_keyboard_ll   =   13
        private   const   wm_keydown   as   integer   =   &h100
        private   const   wm_keyup   as   integer   =   &h101
        private   const   wm_syskeydown   as   integer   =   &h104
        private   const   wm_syskeyup   as   integer   =   &h105
        private   fix_cocd   as   hookcallback         '定义引用变量防止callbackoncollecteddelegate错误的产生

        public   delegate   function   hookcallback(byval   ncode   as   integer,   byval   wparam   as   intptr,   byval   lparam   as   intptr)   as   integer

        public   declare   function   getmodulehandle   lib   "kernel32.dll "   alias   "getmodulehandlea "   (byval   modulename   as   string)   as   intptr

        <dllimport( "user32.dll ",   charset:=charset.auto,   callingconvention:=callingconvention.stdcall)>   _
        public   overloads   shared   function   setwindowshookex(byval   idhook   as   integer,   byval   hookproc   as   hookcallback,   byval   hinstance   as   intptr,   byval   wparam   as   integer)   as   integer
        end   function

        <dllimport( "user32.dll ",   charset:=charset.auto,   callingconvention:=callingconvention.stdcall)>   _
        public   overloads   shared   function   callnexthookex(byval   idhook   as   integer,   byval   ncode   as   integer,   byval   wparam   as   intptr,   byval   lparam   as   intptr)   as   integer
        end   function

        <dllimport( "user32.dll ",   charset:=charset.auto,   callingconvention:=callingconvention.stdcall)>   _
        public   overloads   shared   function   unhookwindowshookex(byval   idhook   as   integer)   as   boolean
        end   function

        'public   event   keydown   as   system.windows.forms.keyeventhandler
        'public   event   keypress   as   system.windows.forms.keypresseventhandler
        public   event   keyup   as   system.windows.forms.keyeventhandler

        public   structure   keyboardhookstruct
                '   specifies   a   virtual-key   code.   the   code   must   be   a   value   in   the   range   1   to   254.  
                public   vkcode   as   integer
                'specifies   a   hardware   scan   code   for   the   key.  
                public   scancode   as   integer
                'specifies   the   extended-key   flag,   event-injected   flag,   context   code,   and   transition-state   flag.
                public   flags   as   integer
                'specifies   the   time   stamp   for   this   message.
                public   time   as   integer
                'specifies   extra   information   associated   with   the   message.  
                public   dwextrainfo   as   integer
        end   structure

        private   function   keyboardproc(byval   ncode   as   integer,   byval   wparam   as   intptr,   byval   lparam   as   intptr)   as   integer
                dim   newptr   as   keyboardhookstruct   =   ctype(marshal.ptrtostructure(lparam,   newptr.gettype()),   keyboardhookstruct)

                if   (ncode   =   hc_action)   then
                        if   wparam.toint32   =   wm_keyup   or   wparam.toint32   =   wm_syskeyup   then
                                if   newptr.vkcode   =   wm_key_enter   then
                                        raiseevent   keyup(me,   new   windows.forms.keyeventargs(ctype(newptr.vkcode,   windows.forms.keys)))
                                end   if
                        end   if
                end   if

                return   callnexthookex(m_ikeyhandle,   ncode,   wparam,   lparam)

        end   function

        public   sub   sethook()
                fix_cocd   =   new   hookcallback(addressof   keyboardproc)
                m_ikeyhandle   =   setwindowshookex(wh_keyboard_ll,   fix_cocd,   getmodulehandle(process.getcurrentprocess().mainmodule.modulename),   0)
                if   m_ikeyhandle   =   0   then
                        throw   new   system.exception( "hook   failed. ")
                else
                end   if
        end   sub

        public   sub   unhook()
                if   (m_ikeyhandle   <>   0)   then   unhookwindowshookex(m_ikeyhandle)
        end   sub

end   class
发表于:2007-03-08 00:16:303楼 得分:0
线程键盘钩子,

imports   system.runtime.interopservices

public   class   myhook

        'hook键盘
        private   const   wh_keyboard   as   integer   =   2
        'hook返回值
        private   hhook   as   integer   =   0
        '定义引用变量防止callbackoncollecteddelegate错误的产生
        private   hookproc   as   callback
        '回调函数
        public   delegate   function   callback(byval   ncode   as   integer,   byval   wparam   as   intptr,   byval   lparam   as   intptr)   as   integer
        '事件
        public   event   keydown   as   system.windows.forms.keyeventhandler
        public   event   keyup   as   system.windows.forms.keyeventhandler

        '获取线程id的api声明
        <dllimport( "kernel32.dll ",   charset:=charset.auto,   callingconvention:=callingconvention.stdcall)>   _
        public   overloads   shared   function   getcurrentthreadid()   as   integer
        end   function

        '设置hook的api声明
        <dllimport( "user32.dll ",   charset:=charset.auto,   callingconvention:=callingconvention.stdcall)>   _
        public   overloads   shared   function   setwindowshookex(byval   idhook   as   integer,   byval   hookproc   as   callback,   byval   hinstance   as   intptr,   byval   wparam   as   integer)   as   integer
        end   function

        '处理下一hook的api声明
        <dllimport( "user32.dll ",   charset:=charset.auto,   callingconvention:=callingconvention.stdcall)>   _
        public   overloads   shared   function   callnexthookex(byval   idhook   as   integer,   byval   ncode   as   integer,   byval   wparam   as   intptr,   byval   lparam   as   intptr)   as   integer
        end   function

        '移除hook的api声明
        <dllimport( "user32.dll ",   charset:=charset.auto,   callingconvention:=callingconvention.stdcall)>   _
        public   overloads   shared   function   unhookwindowshookex(byval   idhook   as   integer)   as   boolean
        end   function

        '安装hook,类型键盘线程钩子,失败返回0,成功返回钩子句柄
        public   function   sethook()   as   integer
                hookproc   =   new   callback(addressof   keyboardproc)
                hhook   =   setwindowshookex(wh_keyboard,   hookproc,   intptr.zero,   getcurrentthreadid)
                return   hhook
        end   function

        '钩子处理子程
        private   function   keyboardproc(byval   ncode   as   integer,   byval   wparam   as   intptr,   byval   lparam   as   intptr)   as   integer
                if   ncode   =   0   then
                        if   lparam.toint32   >   0   then
                                raiseevent   keydown(me,   new   windows.forms.keyeventargs(ctype(wparam,   windows.forms.keys)))
                        end   if
                        if   lparam.toint32   <   0   then
                                raiseevent   keyup(me,   new   windows.forms.keyeventargs(ctype(wparam,   windows.forms.keys)))
                        end   if
                end   if

                return   callnexthookex(hhook,   ncode,   wparam,   lparam)

        end   function

        '移除hook,成功返回true,失败返回false
        public   function   unhook()   as   boolean
                return   unhookwindowshookex(hhook)
        end   function

end   class
发表于:2007-03-12 11:19:554楼 得分:0
谢谢,好几天没上网,我试试,成功了就给你分
发表于:2007-03-12 17:19:425楼 得分:0
hi,snto(神中神),多谢你啦,的确可用
我对vb.net不是很了解,你能不能帮忙解释一下这里全局钩子的用法,为什么我以前用的方法不行呢?
发表于:2007-03-12 17:21:166楼 得分:0
一般的方法不行
.net   framework官方说法不支持全局hook要使用一些特殊手段
发表于:2007-03-12 22:53:277楼 得分:0
问题的关键应该是getmodulehandle函数,你查一下吧,怎么用。ps:代码也是从别人那里改的,全局对我做的一个项目没有帮助,所以也没详细的写注释,线程钩子写得比较详细


快速检索

最新资讯
热门点击