SetWindowsHookEx and VB.NET 2005

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bruno

    SetWindowsHookEx and VB.NET 2005

    I need some help on how write SetWindowsHookE x in vb.net 2005 to hook kb input.
    this is the statement:
    hHOOKKb = SetWindowsHookE x( _
    WH_KEYBOARD, _
    lpfn, _
    hMod, _
    dwThreadId)

    And this is from MSDN Library:
    lpfn [in] Pointer to the hook procedure.
    "hMod [in] Handle to the DLL containing the hook procedure pointed to
    by the lpfn parameter."
    "dwThreadId [in] Specifies the identifier of the thread with which the
    hook
    procedure is to be associated."

    To get kb events from another thread, outside my program or from any desktop
    thread should I put the hook procedure in a DLL (a separate module.vb)? And
    if so how do I get its Handle and lpfn?

    Thanks.
    --
    bruno
  • Mattias Sjögren

    #2
    Re: SetWindowsHookE x and VB.NET 2005

    [color=blue]
    >To get kb events from another thread, outside my program or from any desktop
    >thread should I put the hook procedure in a DLL (a separate module.vb)? And
    >if so how do I get its Handle and lpfn?[/color]

    You can't write global hooks in VB.NET or any other language producing
    managed code. Better use plain old C for that.


    Mattias

    --
    Mattias Sjögren [C# MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Colin Neller

      #3
      Re: SetWindowsHookE x and VB.NET 2005

      Bruno,

      I just posted an example on my blog:
      http://www.colinneller.com/blog/Perm...e5918a39c.aspx.
      Take a look and see if it's what you're looking for.

      --
      Colin Neller



      "bruno" <bruno@communit y.nospam> wrote in message
      news:BE8FD86E-EB04-4D01-A17C-743D7EBEFBFE@mi crosoft.com...[color=blue]
      >I need some help on how write SetWindowsHookE x in vb.net 2005 to hook kb
      >input.
      > this is the statement:
      > hHOOKKb = SetWindowsHookE x( _
      > WH_KEYBOARD, _
      > lpfn, _
      > hMod, _
      > dwThreadId)
      >
      > And this is from MSDN Library:
      > lpfn [in] Pointer to the hook procedure.
      > "hMod [in] Handle to the DLL containing the hook procedure pointed to
      > by the lpfn parameter."
      > "dwThreadId [in] Specifies the identifier of the thread with which the
      > hook
      > procedure is to be associated."
      >
      > To get kb events from another thread, outside my program or from any
      > desktop
      > thread should I put the hook procedure in a DLL (a separate module.vb)?
      > And
      > if so how do I get its Handle and lpfn?
      >
      > Thanks.
      > --
      > bruno[/color]


      Comment

      • bruno

        #4
        Re: SetWindowsHookE x and VB.NET 2005

        Colin,
        thank you for the sample. It is what I was looking for.
        I just tried it:

        Dim Hks As New GlobalHook
        Hks.InstallHook s()
        but I got an error: "SetWindowsHook Ex failed."

        In my trials, writing:
        hHOOK = SetWindowsHookE x(WH_KEYBOARD, _
        hookproc, _
        IntPtr.Zero, _
        AppDomain.GetCu rrentThreadId() )
        I was able to hook my running program. But I was not able to hook all
        threads.
        Can you explain me more about :
        Marshal.GetHINS TANCE(Reflectio n.Assembly.GetE xecutingAssembl y().GetModules( )(0)), 0)
        what does it mean?
        --
        bruno


        "Colin Neller" wrote:
        [color=blue]
        > Bruno,
        >
        > I just posted an example on my blog:
        > http://www.colinneller.com/blog/Perm...e5918a39c.aspx.
        > Take a look and see if it's what you're looking for.
        >
        > --
        > Colin Neller
        > http://www.colinneller.com/blog
        >
        >
        > "bruno" <bruno@communit y.nospam> wrote in message
        > news:BE8FD86E-EB04-4D01-A17C-743D7EBEFBFE@mi crosoft.com...[color=green]
        > >I need some help on how write SetWindowsHookE x in vb.net 2005 to hook kb
        > >input.
        > > this is the statement:
        > > hHOOKKb = SetWindowsHookE x( _
        > > WH_KEYBOARD, _
        > > lpfn, _
        > > hMod, _
        > > dwThreadId)
        > >
        > > And this is from MSDN Library:
        > > lpfn [in] Pointer to the hook procedure.
        > > "hMod [in] Handle to the DLL containing the hook procedure pointed to
        > > by the lpfn parameter."
        > > "dwThreadId [in] Specifies the identifier of the thread with which the
        > > hook
        > > procedure is to be associated."
        > >
        > > To get kb events from another thread, outside my program or from any
        > > desktop
        > > thread should I put the hook procedure in a DLL (a separate module.vb)?
        > > And
        > > if so how do I get its Handle and lpfn?
        > >
        > > Thanks.
        > > --
        > > bruno[/color]
        >
        >
        >[/color]

        Comment

        • Rocky

          #5
          Re: SetWindowsHookE x and VB.NET 2005




          "bruno" <bruno@communit y.nospam> wrote in message
          news:A3D1DFC0-AA30-4007-9F70-705C8AFFFA77@mi crosoft.com...[color=blue]
          > Colin,
          > thank you for the sample. It is what I was looking for.
          > I just tried it:
          >
          > Dim Hks As New GlobalHook
          > Hks.InstallHook s()
          > but I got an error: "SetWindowsHook Ex failed."
          >
          > In my trials, writing:
          > hHOOK = SetWindowsHookE x(WH_KEYBOARD, _
          > hookproc, _
          > IntPtr.Zero, _
          > AppDomain.GetCu rrentThreadId() )
          > I was able to hook my running program. But I was not able to hook all
          > threads.
          > Can you explain me more about :
          > Marshal.GetHINS TANCE(Reflectio n.Assembly.GetE xecutingAssembl y().GetModules( )(0)),
          > 0)
          > what does it mean?
          > --
          > bruno
          >
          >
          > "Colin Neller" wrote:
          >[color=green]
          >> Bruno,
          >>
          >> I just posted an example on my blog:
          >> http://www.colinneller.com/blog/Perm...e5918a39c.aspx.
          >> Take a look and see if it's what you're looking for.
          >>
          >> --
          >> Colin Neller
          >> http://www.colinneller.com/blog
          >>
          >>
          >> "bruno" <bruno@communit y.nospam> wrote in message
          >> news:BE8FD86E-EB04-4D01-A17C-743D7EBEFBFE@mi crosoft.com...[color=darkred]
          >> >I need some help on how write SetWindowsHookE x in vb.net 2005 to hook kb
          >> >input.
          >> > this is the statement:
          >> > hHOOKKb = SetWindowsHookE x( _
          >> > WH_KEYBOARD, _
          >> > lpfn, _
          >> > hMod, _
          >> > dwThreadId)
          >> >
          >> > And this is from MSDN Library:
          >> > lpfn [in] Pointer to the hook procedure.
          >> > "hMod [in] Handle to the DLL containing the hook procedure pointed to
          >> > by the lpfn parameter."
          >> > "dwThreadId [in] Specifies the identifier of the thread with which
          >> > the
          >> > hook
          >> > procedure is to be associated."
          >> >
          >> > To get kb events from another thread, outside my program or from any
          >> > desktop
          >> > thread should I put the hook procedure in a DLL (a separate module.vb)?
          >> > And
          >> > if so how do I get its Handle and lpfn?
          >> >
          >> > Thanks.
          >> > --
          >> > bruno[/color]
          >>
          >>
          >>[/color][/color]


          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: SetWindowsHookE x and VB.NET 2005

            "bruno" <bruno@communit y.nospam> schrieb:[color=blue]
            >I need some help on how write SetWindowsHookE x in vb.net 2005 to hook kb
            >input.[/color]

            <URL:http://www.developer.c om/net/net/article.php/11087_2193301_1 >

            --
            M S Herfried K. Wagner
            M V P <URL:http://dotnet.mvps.org/>
            V B <URL:http://classicvb.org/petition/>

            Comment

            • Colin Neller

              #7
              Re: SetWindowsHookE x and VB.NET 2005

              Bruno,

              I had not seen the article given by Rocky... it is a good one.

              To help you better understand the getHINSTANCE call, I have broken it up
              into smaller pieces. Think of and HINSTANCE as a pointer to your DLL:


              ****
              'get a reference to this assembly
              Dim thisAssembly As System.Reflecti on.Assembly
              thisAssembly = System.Reflecti on.Assembly.Get ExecutingAssemb ly

              'get all modules in this assembly (there is only one)
              Dim myModules() As System.Reflecti on.Module
              myModules = thisAssembly.Ge tModules

              'get the only module
              Dim thisModule As System.Reflecti on.Module
              thisModule = myModules(0)

              'get a pointer to the module
              Dim thisModulePoint er As IntPtr
              thisModulePoint er = Marshal.GetHINS TANCE(thisModul e)

              hMouseHook = Win32.SetWindow sHookEx( _
              Win32.WH.WH_KEY BOARD_LL, _
              KeyboardHookPro cedure, _
              thisModulePoint er, _
              0)
              *****

              I think the problem with your code is that you are not using the low-level
              hook (WH_KEYBOARD_LL ) but the regular hook (WH_KEYBOARD). Also, you are
              specifying an ID for the thread parameter. You should pass in a "0" here to
              indicate that you want to be notified for all threads (and processes.) Here
              is the description of the dwThreadId parameter from the docs:

              "If this parameter is zero, the hook procedure is associated with all
              existing threads running in the same desktop as the calling thread."

              --
              Colin Neller



              "Rocky" <noplace@nowher e.com> wrote in message
              news:%23wZkPavE GHA.2184@TK2MSF TNGP10.phx.gbl. ..[color=blue]
              > http://www.developer.com/net/vb/article.php/2193301
              >
              >
              > "bruno" <bruno@communit y.nospam> wrote in message
              > news:A3D1DFC0-AA30-4007-9F70-705C8AFFFA77@mi crosoft.com...[color=green]
              >> Colin,
              >> thank you for the sample. It is what I was looking for.
              >> I just tried it:
              >>
              >> Dim Hks As New GlobalHook
              >> Hks.InstallHook s()
              >> but I got an error: "SetWindowsHook Ex failed."
              >>
              >> In my trials, writing:
              >> hHOOK = SetWindowsHookE x(WH_KEYBOARD, _
              >> hookproc, _
              >> IntPtr.Zero, _
              >> AppDomain.GetCu rrentThreadId() )
              >> I was able to hook my running program. But I was not able to hook all
              >> threads.
              >> Can you explain me more about :
              >> Marshal.GetHINS TANCE(Reflectio n.Assembly.GetE xecutingAssembl y().GetModules( )(0)),
              >> 0)
              >> what does it mean?
              >> --
              >> bruno
              >>
              >>
              >> "Colin Neller" wrote:
              >>[color=darkred]
              >>> Bruno,
              >>>
              >>> I just posted an example on my blog:
              >>> http://www.colinneller.com/blog/Perm...e5918a39c.aspx.
              >>> Take a look and see if it's what you're looking for.
              >>>
              >>> --
              >>> Colin Neller
              >>> http://www.colinneller.com/blog
              >>>
              >>>
              >>> "bruno" <bruno@communit y.nospam> wrote in message
              >>> news:BE8FD86E-EB04-4D01-A17C-743D7EBEFBFE@mi crosoft.com...
              >>> >I need some help on how write SetWindowsHookE x in vb.net 2005 to hook
              >>> >kb
              >>> >input.
              >>> > this is the statement:
              >>> > hHOOKKb = SetWindowsHookE x( _
              >>> > WH_KEYBOARD, _
              >>> > lpfn, _
              >>> > hMod, _
              >>> > dwThreadId)
              >>> >
              >>> > And this is from MSDN Library:
              >>> > lpfn [in] Pointer to the hook procedure.
              >>> > "hMod [in] Handle to the DLL containing the hook procedure pointed
              >>> > to
              >>> > by the lpfn parameter."
              >>> > "dwThreadId [in] Specifies the identifier of the thread with which
              >>> > the
              >>> > hook
              >>> > procedure is to be associated."
              >>> >
              >>> > To get kb events from another thread, outside my program or from any
              >>> > desktop
              >>> > thread should I put the hook procedure in a DLL (a separate
              >>> > module.vb)?
              >>> > And
              >>> > if so how do I get its Handle and lpfn?
              >>> >
              >>> > Thanks.
              >>> > --
              >>> > bruno
              >>>
              >>>
              >>>[/color][/color]
              >
              >[/color]


              Comment

              • Mattias Sjögren

                #8
                Re: SetWindowsHookE x and VB.NET 2005

                Colin,
                [color=blue]
                >I had not seen the article given by Rocky... it is a good one.[/color]

                Interesitng, I was just going to reply to Herfired who posted the same
                link and say that I thought it was a poorly written article. It
                contains incorrect statements and buggy code. The code in your blog
                post was at least somewhat more correct.


                Mattias

                --
                Mattias Sjögren [C# MVP] mattias @ mvps.org
                http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
                Please reply only to the newsgroup.

                Comment

                • Colin Neller

                  #9
                  Re: SetWindowsHookE x and VB.NET 2005

                  Mattias,
                  [color=blue]
                  > Interesitng, I was just going to reply to Herfired who posted the same
                  > link and say that I thought it was a poorly written article. It
                  > contains incorrect statements and buggy code.[/color]

                  I must confess I only skimmed it... at a high level, it seemed to cover most
                  of the bases. I suppose I should not endorse something that I have only
                  skimmed :) Would you like to elaborate on "the statements that are
                  incorrect"?

                  [color=blue]
                  > The code in your blog post was at least somewhat more correct.[/color]

                  That's good. I welcome any suggestions for improvement.

                  --
                  Colin Neller



                  Comment

                  • bruno

                    #10
                    Re: SetWindowsHookE x and VB.NET 2005

                    Thank you Rocky,
                    the article is very clear and explained me thinks I was looking for.
                    I've just created a form with two buttons: Hook and Unhook.
                    I copy/pasted the full sample but I'm getting : "Keyboard hook failed: 0"

                    I' using:
                    Microsoft Visual Studio 2005 Version 8.0.50727.42 (RTM.050727-4200)
                    Microsoft .NET Framework Version 2.0.50727
                    Installed Edition: Professional
                    Microsoft Visual Basic 2005 77626-009-0000007-41133

                    --
                    bruno


                    "Rocky" wrote:
                    [color=blue]
                    > http://www.developer.com/net/vb/article.php/2193301
                    >
                    >
                    > "bruno" <bruno@communit y.nospam> wrote in message
                    > news:A3D1DFC0-AA30-4007-9F70-705C8AFFFA77@mi crosoft.com...[color=green]
                    > > Colin,
                    > > thank you for the sample. It is what I was looking for.
                    > > I just tried it:
                    > >
                    > > Dim Hks As New GlobalHook
                    > > Hks.InstallHook s()
                    > > but I got an error: "SetWindowsHook Ex failed."
                    > >
                    > > In my trials, writing:
                    > > hHOOK = SetWindowsHookE x(WH_KEYBOARD, _
                    > > hookproc, _
                    > > IntPtr.Zero, _
                    > > AppDomain.GetCu rrentThreadId() )
                    > > I was able to hook my running program. But I was not able to hook all
                    > > threads.
                    > > Can you explain me more about :
                    > > Marshal.GetHINS TANCE(Reflectio n.Assembly.GetE xecutingAssembl y().GetModules( )(0)),
                    > > 0)
                    > > what does it mean?
                    > > --
                    > > bruno
                    > >
                    > >
                    > > "Colin Neller" wrote:
                    > >[color=darkred]
                    > >> Bruno,
                    > >>
                    > >> I just posted an example on my blog:
                    > >> http://www.colinneller.com/blog/Perm...e5918a39c.aspx.
                    > >> Take a look and see if it's what you're looking for.
                    > >>
                    > >> --
                    > >> Colin Neller
                    > >> http://www.colinneller.com/blog
                    > >>
                    > >>
                    > >> "bruno" <bruno@communit y.nospam> wrote in message
                    > >> news:BE8FD86E-EB04-4D01-A17C-743D7EBEFBFE@mi crosoft.com...
                    > >> >I need some help on how write SetWindowsHookE x in vb.net 2005 to hook kb
                    > >> >input.
                    > >> > this is the statement:
                    > >> > hHOOKKb = SetWindowsHookE x( _
                    > >> > WH_KEYBOARD, _
                    > >> > lpfn, _
                    > >> > hMod, _
                    > >> > dwThreadId)
                    > >> >
                    > >> > And this is from MSDN Library:
                    > >> > lpfn [in] Pointer to the hook procedure.
                    > >> > "hMod [in] Handle to the DLL containing the hook procedure pointed to
                    > >> > by the lpfn parameter."
                    > >> > "dwThreadId [in] Specifies the identifier of the thread with which
                    > >> > the
                    > >> > hook
                    > >> > procedure is to be associated."
                    > >> >
                    > >> > To get kb events from another thread, outside my program or from any
                    > >> > desktop
                    > >> > thread should I put the hook procedure in a DLL (a separate module.vb)?
                    > >> > And
                    > >> > if so how do I get its Handle and lpfn?
                    > >> >
                    > >> > Thanks.
                    > >> > --
                    > >> > bruno
                    > >>
                    > >>
                    > >>[/color][/color]
                    >
                    >
                    >[/color]

                    Comment

                    • Colin Neller

                      #11
                      Re: SetWindowsHookE x and VB.NET 2005

                      > I copy/pasted the full sample but I'm getting : "Keyboard hook failed: 0"

                      What version of windows are you running?

                      --
                      Colin Neller



                      Comment

                      • bruno

                        #12
                        Re: SetWindowsHookE x and VB.NET 2005

                        WinXP prof sp2
                        --
                        bruno


                        "Colin Neller" wrote:
                        [color=blue][color=green]
                        > > I copy/pasted the full sample but I'm getting : "Keyboard hook failed: 0"[/color]
                        >
                        > What version of windows are you running?
                        >
                        > --
                        > Colin Neller
                        > http://www.colinneller.com/blog
                        >
                        >
                        >[/color]

                        Comment

                        • bruno

                          #13
                          Re: SetWindowsHookE x and VB.NET 2005

                          Collin,
                          Thanks for your well detailed explanation of getHINSTANCE.
                          After revising the SetWindowsHookE x documentation I've done this:
                          - put your two classes "Win32" and "GlobalHook " toghether in a VB.NET
                          project,
                          - compile and get the DLL
                          - create a separate EXE Appl. that references that DLL

                          Now SetWindowsHookE x (mouse and Kb) doesn't give me error, but
                          "MouseHookP roc" gets never control and "KeyboardHookPr oc" gets control always
                          with KeyDownEvent and KeyUpEvent= nothing, so no "Onxxx" procedure get fired.
                          I will go on with more test, but at the moment have no idea.

                          Thanks to all you for informations.
                          --
                          bruno

                          Comment

                          • Rocky

                            #14
                            Re: SetWindowsHookE x and VB.NET 2005

                            Here's another one to look at:
                            Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


                            "bruno" <bruno@communit y.nospam> wrote in message
                            news:E7E7056E-818F-4074-9A74-85CF68ABFD3D@mi crosoft.com...[color=blue]
                            > Collin,
                            > Thanks for your well detailed explanation of getHINSTANCE.
                            > After revising the SetWindowsHookE x documentation I've done this:
                            > - put your two classes "Win32" and "GlobalHook " toghether in a VB.NET
                            > project,
                            > - compile and get the DLL
                            > - create a separate EXE Appl. that references that DLL
                            >
                            > Now SetWindowsHookE x (mouse and Kb) doesn't give me error, but
                            > "MouseHookP roc" gets never control and "KeyboardHookPr oc" gets control
                            > always
                            > with KeyDownEvent and KeyUpEvent= nothing, so no "Onxxx" procedure get
                            > fired.
                            > I will go on with more test, but at the moment have no idea.
                            >
                            > Thanks to all you for informations.
                            > --
                            > bruno
                            >[/color]


                            Comment

                            • Rocky

                              #15
                              Re: SetWindowsHookE x and VB.NET 2005

                              Bruno,

                              I found the problem. You must disable the "Enable the Visual Studio hosting
                              process" option in the Menu--->Project-->Properties---->Debug form. Uncheck
                              the box .


                              "bruno" <bruno@communit y.nospam> wrote in message
                              news:E7E7056E-818F-4074-9A74-85CF68ABFD3D@mi crosoft.com...[color=blue]
                              > Collin,
                              > Thanks for your well detailed explanation of getHINSTANCE.
                              > After revising the SetWindowsHookE x documentation I've done this:
                              > - put your two classes "Win32" and "GlobalHook " toghether in a VB.NET
                              > project,
                              > - compile and get the DLL
                              > - create a separate EXE Appl. that references that DLL
                              >
                              > Now SetWindowsHookE x (mouse and Kb) doesn't give me error, but
                              > "MouseHookP roc" gets never control and "KeyboardHookPr oc" gets control
                              > always
                              > with KeyDownEvent and KeyUpEvent= nothing, so no "Onxxx" procedure get
                              > fired.
                              > I will go on with more test, but at the moment have no idea.
                              >
                              > Thanks to all you for informations.
                              > --
                              > bruno
                              >[/color]


                              Comment

                              Working...