Hook system wide with DLL in C++
Hook system wide with DLL in C++
Hook system wide with DLL in C++
Hook system wide with DLL in C++
Hook system wide with DLL in C++
Hook system wide with DLL in C++ Hook system wide with DLL in C++ Hook system wide with DLL in C++ Hook system wide with DLL in C++ Hook system wide with DLL in C++ Hook system wide with DLL in C++ Hook system wide with DLL in C++ Hook system wide with DLL in C++
Hook system wide with DLL in C++ Hook system wide with DLL in C++
Hook system wide with DLL in C++
Go Back  Xtreme Visual Basic Talk > > > Hook system wide with DLL in C++


Reply
 
Thread Tools Display Modes
  #1  
Old 06-22-2015, 02:46 PM
philippe734 philippe734 is offline
Newcomer
 
Join Date: Jun 2015
Posts: 5
Default Hook system wide with DLL in C++


Hello,

Even we are in 2015 and vb6 is old, it’s still great. As I had been in difficult to find a simple and efficient method to do hooking system wide, so I share my source code, here in zip attachment.

This project allow you to do global hook, system wide in Windows. The principle is to put the hook with a DLL, then get the message by subclassing our program. This hook system wide work only with 32 bits applications.

The DLL in attachment was compiled in C++. Which allow you to put hook system wide in Windows, then send to our program, with SendMessage, the message WM_USER and the hook code (nCode). With subclassing of our program, we can get the hook code by subtract WM_USER.

The hook provided from the DLL is not specific to our program but global in all Windows (system wide).

The DLL support these hooks types :
' WH_CALLWNDPROC = CallWndProc;
' WH_CALLWNDPROCRET = CallWndRetProc;
' WH_CBT = CBTProc;
' WH_DEBUG = DebugProc;
' WH_FOREGROUNDIDLE = ForegroundIdleProc;
' WH_GETMESSAGE = GetMsgProc;
' WH_JOURNALPLAYBACK = JournalPlaybackProc;
' WH_JOURNALRECORD = JournalRecordProc;
' WH_KEYBOARD = KeyboardProc;
' WH_KEYBOARD_LL = LowLevelKeyboardProc;
' WH_MSGFILTER = MessageProc;
' WH_MOUSE = MouseProc;
' WH_MOUSE_LL = LowLevelMouseProc;
' WH_SHELL = ShellProc;
' WH_SYSMSGFILTER = SysMsgProc;

This project provide a demo of these hooks system wide :
CBT / CreateWnd : get the name of the handle parent of the window to be created.
Keyboard = get the code of the keystroke.
Mouse = get the name of the handle pointed by the left click.

I’m not the author of the DLL, neither hooking and subclassing methods.
I took these three elements and make a simple project.
The DLL was coded in C++ by Renfield – 2007
Source code of subclassing by Renfield – 2010
Hooking routines by vbAccelerator – 2003





Do you want to see the source code without download the zip ?


The routine to subclass and get the hook message code :
Code:
Private Function ISubclasser_WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Dim HookCode As Long
    
    Select Case hWnd
    Case mtWin1.hWnd

        ' The hook'DLL send the message WM_USER with the Hook code, it's why we need to substract WM_USER
        HookCode = (uMsg - WM_USER)

        Select Case HookType
        Case WH_CALLWNDPROC: Call procWH_CALLWNDPROC(HookCode, wParam, lParam)
        Case WH_CALLWNDPROCRET: Call procWH_CALLWNDPROCRET(HookCode, wParam, lParam)
        Case WH_CBT: Call procWH_CBT(HookCode, wParam, lParam)
        Case WH_FOREGROUNDIDLE: Call procWH_FOREGROUNDIDLE(HookCode, wParam, lParam)
        Case WH_GETMESSAGE: Call procWH_GETMESSAGE(HookCode, wParam, lParam)
        Case WH_KEYBOARD: Call procWH_KEYBOARD(HookCode, wParam, lParam)
        Case WH_MOUSE: Call procWH_MOUSE(HookCode, wParam, lParam)
        Case WH_MSGFILTER: Call procWH_MSGFILTER(HookCode, wParam, lParam)
        Case WH_SHELL: Call procWH_SHELL(HookCode, wParam, lParam)
        End Select

        If bSkipWindowProc = False Then
            ' Here, we need to allow the standard message of our program,
            ' because we don't subclass the target hooked.
            ISubclasser_WindowProc = CallOldProc(mtWin1, hWnd, uMsg, wParam, lParam)
        Else
            ' Not allow the standard message
        End If
        
        bSkipWindowProc = False

    End Select
End Function

The routine of the hook type CBT :
Code:
Private Sub procWH_CBT(nCode As eHookConstants, wParam As Long, lParam As Long)
    Dim lCBT_CREATEWND As tCBT_CREATEWND
    Dim lCreateStruc As tCREATESTRUCT
    Dim hNewWnd As Long

    If nCode = HCBT_CREATEWND Then

        ' Handle of the window to be created
        hNewWnd = wParam

        ' Get informations from pointers
        CopyMemory ByVal VarPtr(lCBT_CREATEWND), ByVal lParam, LenB(lCBT_CREATEWND)    ' OK
        CopyMemory ByVal VarPtr(lCreateStruc), ByVal lCBT_CREATEWND.lpcs, LenB(lCreateStruc)    ' OK

        Me.Text1 = Me.Text1 & vbNewLine & Now & "  Name of the handle parent : " & GetHandleName(lCreateStruc.hWndParent)
    End If
End Sub

Have fun ;-)
Attached Files
File Type: zip Hook_system_wide_DLL_Subclass.zip (68.0 KB, 9 views)

Last edited by philippe734; 06-22-2015 at 02:59 PM.
Reply With Quote
Reply

Tags
hook system wide global


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Hook system wide with DLL in C++
Hook system wide with DLL in C++
Hook system wide with DLL in C++ Hook system wide with DLL in C++
Hook system wide with DLL in C++
Hook system wide with DLL in C++
Hook system wide with DLL in C++ Hook system wide with DLL in C++ Hook system wide with DLL in C++ Hook system wide with DLL in C++ Hook system wide with DLL in C++ Hook system wide with DLL in C++ Hook system wide with DLL in C++
Hook system wide with DLL in C++
Hook system wide with DLL in C++
 
Hook system wide with DLL in C++
Hook system wide with DLL in C++
 
-->