IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Forms Discussion :

[VB.NET] API hook clavier


Sujet :

Windows Forms

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    47
    Détails du profil
    Informations personnelles :
    Âge : 35
    Localisation : Belgique

    Informations forums :
    Inscription : Janvier 2006
    Messages : 47
    Par défaut [VB.NET] API hook clavier
    Voici mon problème :
    J'ai trouver ce hook sur internet (enfin ... presque, j'ai seulement modifier les debug.writeline() pour msgbox()) et je voulais savoir si quelqu'un peut me dire pourquoi lorsque je l'exécute, j'obtien un msgbox disant :
    Keyboard hook failed: 0 le zéro corespond a un code d'erreur provenant de la dll user32.

    Je ne connais pas très bien les hooks, c'est mo premier, alors ne soyez pas trop méchant


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    
    PublicDeclareFunction UnhookWindowsHookEx Lib"user32" (ByVal hHook AsInteger) AsInteger
    PublicDeclareFunction SetWindowsHookEx Lib"user32"Alias"SetWindowsHookExA" (ByVal idHook AsInteger, ByVal lpfn As KeyboardHookDelegate, ByVal hmod AsInteger, ByVal dwThreadId AsInteger) AsInteger
    PrivateDeclareFunction GetAsyncKeyState Lib"user32" (ByVal vKey AsInteger) AsInteger
    PrivateDeclareFunction CallNextHookEx Lib"user32" (ByVal hHook AsInteger, ByVal nCode AsInteger, ByVal wParam AsInteger, ByVal lParam As KBDLLHOOKSTRUCT) AsInteger
    PublicStructure KBDLLHOOKSTRUCT
    Public vkCode AsInteger
    Public scanCode AsInteger
    Public flags AsInteger
    Public time AsInteger
    Public dwExtraInfo AsInteger
    EndStructure
    ' Low-Level Keyboard Constants
    PrivateConst HC_ACTION AsInteger = 0
    PrivateConst LLKHF_EXTENDED AsInteger = &H1
    PrivateConst LLKHF_INJECTED AsInteger = &H10
    PrivateConst LLKHF_ALTDOWN AsInteger = &H20
    PrivateConst LLKHF_UP AsInteger = &H80
    ' Virtual Keys
    PublicConst VK_TAB = &H9
    PublicConst VK_CONTROL = &H11
    PublicConst VK_ESCAPE = &H1B
    PublicConst VK_DELETE = &H2E
    PublicConst VK_LWIN = &H5B
    PublicConst VK_RWIN = &H5C
     
    PrivateConst WH_KEYBOARD_LL AsInteger = 13&
    Public KeyboardHandle AsInteger
     
    ' Implement this function to block as many
    ' key combinations as you'd like
    PublicFunction IsHooked(ByRef Hookstruct As KBDLLHOOKSTRUCT) AsBoolean
    Debug.WriteLine("Hookstruct.vkCode: " & Hookstruct.vkCode)
    Debug.WriteLine(Hookstruct.vkCode = VK_ESCAPE)
    Debug.WriteLine(Hookstruct.vkCode = VK_TAB)
    If (Hookstruct.vkCode = VK_ESCAPE) AndCBool(GetAsyncKeyState(VK_CONTROL) And&H8000) Then
    Call HookedState("Ctrl + Esc blocked")
    ReturnTrue
    EndIf
    If (Hookstruct.vkCode = VK_TAB) AndCBool(Hookstruct.flags And LLKHF_ALTDOWN) Then
    Call HookedState("Alt + Tab blockd")
    ReturnTrue
    EndIf
    If (Hookstruct.vkCode = VK_ESCAPE) AndCBool(Hookstruct.flags And LLKHF_ALTDOWN) Then
    Call HookedState("Alt + Escape blocked")
    ReturnTrue
    EndIf
    ReturnFalse
    EndFunction
    
    
    Suite du code au prochain message ^^

  2. #2
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    47
    Détails du profil
    Informations personnelles :
    Âge : 35
    Localisation : Belgique

    Informations forums :
    Inscription : Janvier 2006
    Messages : 47
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    PrivateSub HookedState(ByVal Text AsString)
    MsgBox(Text)
    EndSub
    PublicFunction KeyboardCallback(ByVal Code AsInteger, ByVal wParam AsInteger, ByRef lParam As KBDLLHOOKSTRUCT) AsInteger
    If (Code = HC_ACTION) Then
    MsgBox("Calling IsHooked")
    If (IsHooked(lParam)) Then
    Return1
    EndIf
    EndIf
    Return CallNextHookEx(KeyboardHandle, Code, wParam, lParam)
    EndFunction
     
    PublicDelegateFunction KeyboardHookDelegate(ByVal Code AsInteger, ByVal wParam AsInteger, ByRef lParam As KBDLLHOOKSTRUCT) AsInteger
    <MarshalAs(UnmanagedType.FunctionPtr)> Private callback As KeyboardHookDelegate
    PublicSub HookKeyboard()
    callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
    KeyboardHandle = SetWindowsHookEx(WH_KEYBOARD_LL, callback, Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
    Call CheckHooked()
    EndSub
    PublicSub CheckHooked()
    If (Hooked()) Then
    MsgBox("Keyboard hooked")
    Else
    MsgBox("Keyboard hook failed: " & Err.LastDllError)
    EndIf
    EndSub
    PrivateFunction Hooked()
    Hooked = KeyboardHandle <> 0
    EndFunction
    PublicSub UnhookKeyboard()
    If (Hooked()) Then
    Call UnhookWindowsHookEx(KeyboardHandle)
    EndIf
    EndSub
    

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    47
    Détails du profil
    Informations personnelles :
    Âge : 35
    Localisation : Belgique

    Informations forums :
    Inscription : Janvier 2006
    Messages : 47
    Par défaut

  4. #4
    Xno
    Xno est déconnecté
    Membre éclairé
    Profil pro
    Inscrit en
    Septembre 2005
    Messages
    71
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2005
    Messages : 71
    Par défaut
    Salut!

    Euh...je ne suis pas spécialiste VB.NET, loin de là, car je fais plutôt du C#, mais j'ai déjà développé des hooks en C++.

    Une question, où est ton programme principal? Je ne vois que des fonctions...
    Est-ce que la fonction HookKeyboad() est bien appelée car, apparemment, la variable KeyboardHandle n'est pas initialisée par l'appel de la méthode SetWindowsHookEx (à moins que cet appel foire...).


  5. #5
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    47
    Détails du profil
    Informations personnelles :
    Âge : 35
    Localisation : Belgique

    Informations forums :
    Inscription : Janvier 2006
    Messages : 47
    Par défaut
    Nouveautée !!!!!
    J'ai réeussi a le débuger mais, j'ai un autre problème. Quand la fonction

    Return
    CallNextHookEx(KeyboardHandle, _
    Code, wParam, lParam)

    est appeler, la pile est désiquilibré (eummm, sa veut dire quoi ??? ). Est-ce que quelqu'un pourait me donnné un petit coup de main ?

    Je post le nouveau code.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    
    PublicClass KeyboardHook
    PublicDeclareFunction UnhookWindowsHookEx Lib"user32" _
    (ByVal hHook AsInteger) AsInteger
    PublicDeclareFunction SetWindowsHookEx Lib"user32" _
    Alias"SetWindowsHookExA" (ByVal idHook AsInteger, _
    ByVal lpfn As KeyboardHookDelegate, ByVal hmod AsInteger, _
    ByVal dwThreadId AsInteger) AsInteger
    PrivateDeclareFunction GetAsyncKeyState Lib"user32" _
    (ByVal vKey AsInteger) AsInteger
    PrivateDeclareFunction CallNextHookEx Lib"user32" _
    (ByVal hHook AsInteger, _
    ByVal nCode AsInteger, _
    ByVal wParam AsInteger, _
    ByVal lParam As KBDLLHOOKSTRUCT) AsInteger
    PublicStructure KBDLLHOOKSTRUCT
    Public vkCode AsInteger
    Public scanCode AsInteger
    Public flags AsInteger
    Public time AsInteger
    Public dwExtraInfo AsInteger
    EndStructure
    ' Low-Level Keyboard Constants
    PrivateConst HC_ACTION AsInteger = 0
    PrivateConst LLKHF_EXTENDED AsInteger = &H1
    PrivateConst LLKHF_INJECTED AsInteger = &H10
    PrivateConst LLKHF_ALTDOWN AsInteger = &H20
    PrivateConst LLKHF_UP AsInteger = &H80
    ' Virtual Keys
    PublicConst VK_TAB = &H9
    PublicConst VK_CONTROL = &H11
    PublicConst VK_ESCAPE = &H1B
    PublicConst VK_DELETE = &H2E
    PrivateConst WH_KEYBOARD_LL AsInteger = 13&
    PublicShared KeyboardHandle AsInteger
     
    ' Implement this function to block as many
    ' key combinations as you'd like
    PublicSharedFunction IsHooked( _
    ByRef Hookstruct As KBDLLHOOKSTRUCT) AsBoolean
    Debug.WriteLine("Hookstruct.vkCode: " & Hookstruct.vkCode)
    Debug.WriteLine(Hookstruct.vkCode = VK_ESCAPE)
    Debug.WriteLine(Hookstruct.vkCode = VK_TAB)
    If (Hookstruct.vkCode = VK_ESCAPE) And _
    CBool(GetAsyncKeyState(VK_CONTROL) _
    And&H8000) Then
    Call HookedState("Ctrl + Esc blocked")
    ReturnTrue
    EndIf
    If (Hookstruct.vkCode = VK_TAB) And _
    CBool(Hookstruct.flags And _
    LLKHF_ALTDOWN) Then
    Call HookedState("Alt + Tab blockd")
    ReturnTrue
    EndIf
    If (Hookstruct.vkCode = VK_ESCAPE) And _
    CBool(Hookstruct.flags And _
    LLKHF_ALTDOWN) Then
    Call HookedState("Alt + Escape blocked")
    ReturnTrue
    EndIf
    ReturnFalse
    EndFunction
    PrivateSharedSub HookedState(ByVal Text AsString)
    Debug.WriteLine(Text)
    EndSub
     
    ...
    
    

  6. #6
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    47
    Détails du profil
    Informations personnelles :
    Âge : 35
    Localisation : Belgique

    Informations forums :
    Inscription : Janvier 2006
    Messages : 47
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    ...
     
    PublicSharedFunction KeyboardCallback(ByVal Code AsInteger, _
    ByVal wParam AsInteger, _
    ByRef lParam As KBDLLHOOKSTRUCT) AsInteger
    If (Code = HC_ACTION) Then
    Debug.WriteLine("Calling IsHooked")
    If (IsHooked(lParam)) Then
    Return1
    EndIf
    EndIf
    Return CallNextHookEx(KeyboardHandle, _
    Code, wParam, lParam)
    EndFunction
    PublicDelegateFunction KeyboardHookDelegate( _
    ByVal Code AsInteger, _
    ByVal wParam AsInteger, ByRef lParam As KBDLLHOOKSTRUCT) _
    AsInteger
    <MarshalAs(UnmanagedType.FunctionPtr)> _
    PrivateShared callback As KeyboardHookDelegate
    PublicSharedSub HookKeyboard()
    callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
    KeyboardHandle = SetWindowsHookEx( _
    WH_KEYBOARD_LL, callback, _
    Marshal.GetHINSTANCE( _
    [Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
    Call CheckHooked()
    EndSub
    PublicSharedSub CheckHooked()
    If (Hooked()) Then
    Debug.WriteLine("Keyboard hooked")
    Else
    Debug.WriteLine("Keyboard hook failed: " & Err.LastDllError)
    EndIf
    EndSub
    PrivateSharedFunction Hooked()
    Hooked = KeyboardHandle <> 0
    EndFunction
    PublicSharedSub UnhookKeyboard()
    If (Hooked()) Then
    Call UnhookWindowsHookEx(KeyboardHandle)
    EndIf
    EndSub
    EndClass
    
    Voir post précédent.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Win32 API] Hook clavier combinaison de touches
    Par Trunks dans le forum Windows
    Réponses: 3
    Dernier message: 27/11/2010, 13h23
  2. [API Win32] Hook clavier
    Par NeoKript dans le forum Windows
    Réponses: 3
    Dernier message: 21/09/2009, 14h19
  3. Vb.net Hook Clavier
    Par olixelle dans le forum Windows Forms
    Réponses: 1
    Dernier message: 25/09/2006, 10h59
  4. [vb.net] API souris et claviers
    Par roxanne dans le forum Windows Forms
    Réponses: 3
    Dernier message: 07/07/2006, 22h07

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo