begin process at 2008 08 22 06:44:08
1 229 780 membres
51 nouveaux aujourd'hui
14 267 membres club

Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

Sujet : hook souris dans Word URGENT [ Algorithme / Divers ] (sylvain230)

hook souris dans Word URGENT le 02/06/2008 15:00:13

sylvain230
Bonjour. Je n'arrive pas à installer un hook dans Word

J'ai toujours l'erreur suivante
" Tentative de lecture ou d'écriture de mémoire protégée. Cela indique souvent qu'une autre mémoire est endommagée."
Si quelqu'un sait ca serai cool de me dire :)
Merci d'avance . C'est assez urgent !


Voici mon code :



Imports System.Drawing
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.ComponentModel


'************************************************
'MODULE SECTION
'************************************************
Public Module mod1
    'declare delegate object
    Delegate Function HookCallback(ByVal Code As Integer, ByVal wParam As Integer, _
      ByVal lParam As Integer) As Integer

    'declare API's
    Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" _
      (ByVal HookID As Integer, ByVal lpfn As HookCallback, ByVal hModule As Integer, _
      ByVal ThreadId As Integer) As Integer
    Public Declare Function UnhookWindowsHookEx Lib "user32" Alias "UnhookWindowsHookEx" _
      (ByVal hHook As Integer) As Integer
    Public Declare Function CallNextHookEx Lib "user32" Alias "CallNextHookEx" _
      (ByVal hHook As Integer, ByVal Code As Integer, ByVal wParam As Integer, _
      ByVal lParam As Integer) As Integer
    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
      (ByRef Destination As mod1.MOUSEHOOKSTRUCT, ByVal Source As Integer, _
      ByVal Length As Integer)
    'declare API's
    Public Declare Function getThreadID Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hwnd As Integer, ByVal lpdwProcessId As Integer) As Integer
    Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
    Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Integer, ByRef lpdwProcessId As Integer) As Integer
    'Global constants
    Public Const WH_MOUSE As Integer = 7

    Public Structure POINTAPI
        Public x As Integer
        Public y As Integer
    End Structure

   Public Structure MOUSEHOOKSTRUCT
        Public pt As POINTAPI
        Public hwnd As Integer
        Public wHitTestCode As Integer
        Public dwExtraInfo As Integer
    End Structure

    Private m_hHook As Integer

    'Property to access hook handle
    Property hHook() As Integer
        Set
            If m_hHook <> 0 And Value <> 0 Then
                'Don't overwrite the handle
                '   Unless it is being reset to 0
            Else
                m_hHook = Value
            End If
        End Set

        Get
            hHook = m_hHook
        End Get
    End Property
End Module


'************************************************
'FORM SECTION
'************************************************
Public Class frmHook
    Inherits System.Windows.Forms.Form

    'declare nested class var
    Public hk As Hook

    'declare delegate object
    Public cb As HookCallback
   
   
    'START HERE - default ctor for Form1 class
    Public Sub New()
        MyBase.New()
        InitializeComponent()
        'frmHook = Me
       
        'instantiate a new nested Hook class passing in a reference to this form
        hk = New Hook(Me)
       
        'instantiate a new delegate object pointing to the HookProc function
        '   in the nested class
        cb = New HookCallback(AddressOf hk.HookProc)
    End Sub
   
   
    '************************************************
    'NESTED CLASS-containing the callback HookProc
    '************************************************
    Public Class Hook
        'reference back to the form
        Private TheWin As frmHook
       
        'ctor - used to get a reference to the main window
        '       in order to write info to the textbox1 text box
        Public Sub New(ByVal MainWnd As frmHook)
            TheWin = MainWnd
        End Sub
       
        'HOOK PROC
        Public Function HookProc(ByVal Code As Integer, ByVal wParam As Integer, _
          ByVal lParam As Integer) As Integer
            Dim Dest As New mod1.MOUSEHOOKSTRUCT()
           
            CopyMemory(Dest, lParam, Len(Dest))
            TheWin.PrintTxt(Dest.hwnd & " : " & Dest.pt.x & " : " & Dest.pt.y & _
              " : " & Dest.wHitTestCode)
           
            HookProc = CallNextHookEx(hHook, Code, wParam, lParam)
        End Function
    End Class
   
   
    'used by Form1 to add text to the textbox1 text box, called by HookProc
    Public Sub PrintTxt(ByVal ip As String)
        textBox1.Text = TextBox1.Text & ip & chr(13) & chr(10)
    End Sub
   
   
    '************************************************
    'REGULAR WINDOWS .NET STUFF
    '************************************************
#Region " Windows Form Designer generated code "
   
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.Container
    Private WithEvents Button2 As System.Windows.Forms.Button
    Private WithEvents Button1 As System.Windows.Forms.Button
    Private WithEvents TextBox1 As System.Windows.Forms.TextBox
   
    Dim WithEvents Hook_Try2 As System.Windows.Forms.Form
   
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    Private Sub InitializeComponent()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(8, 40)
        Me.Button2.Name = "Button2"
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "UnHook"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(88, 0)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both
        Me.TextBox1.Size = New System.Drawing.Size(448, 620)
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = ""
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(8, 8)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Hook"
        '
        'frmHook
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(536, 617)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button2, Me.Button1, Me.TextBox1})
        Me.Name = "frmHook"
        Me.Text = "VB.NET Hooking Example"
        Me.ResumeLayout(False)

    End Sub

#End Region
   
    'Form overrides dispose to clean up the component list.
    Public Overloads Sub Dispose()
        MyBase.Dispose()
        components.Dispose()
    End Sub

    '************************************************
    'EVENT HANDLERS FOR BUTTON CONTROLS
    '************************************************
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Install hook

        Dim Word_hwnd As Integer = FindWindow(vbNullString, "Document3 - Microsoft Word")
        Dim numID As Integer
        Dim NumThread As Integer = GetWindowThreadProcessId(Word_hwnd, numID)
        'hHook = SetWindowsHookEx(WH_MOUSE, cb, 0, AppDomain.GetCurrentThreadId)
        hHook = SetWindowsHookEx(WH_MOUSE, cb, 0, getThreadID(Word_hwnd, NumThread))
        MsgBox(hHook)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Release hook
        MsgBox(UnhookWindowsHookEx(hHook))
        hHook = 0
    End Sub
End Class



Classé sous : public, windows, system, integer, byval

Participer à cet échange

Pub



Appels d'offres

CalendriCode

Août 2008
LMMJVSD
    123
45678910
11121314151617
18192021222324
25262728293031

Boutique

Boutique de goodies CodeS-SourceS