Results 1 to 4 of 4

Thread: Local WH_CBT hook causes GPF - but only on the first window..?

  1. #1

    Thread Starter
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616

    Local WH_CBT hook causes GPF - but only on the first window..?

    I have written a local WH_CBT hook and am using it to track window creation. It decodes the CBT_CREATEWND structure and raises an event. The sink for this event can then decide whether or not to allow the window creation to continue. However this will cause a GPF if I start the hook before any VB window is created...but not if I start it after the first form is shown. Any ideas?

    The code I have is:
    VB Code:
    1. Private Type CREATESTRUCT
    2.     lpCreateParams As Long
    3.     hInstance As Long
    4.     hMenu As Long
    5.     hWndParent As Long
    6.     Height As Integer
    7.     Width As Integer
    8.     Left As Integer
    9.     Top As Integer
    10.     Style As Long
    11.     lpszName As Long
    12.     lpszClass As Long
    13.     ExStyle As Long
    14. End Type
    15.  
    16. Private Declare Sub CopyMemoryCreateStruct Lib "kernel32" Alias "RtlMoveMemory" (Destination As CREATESTRUCT, ByVal Source As Long, ByVal Length As Long)
    17. Private Declare Sub CopyMemoryFromCREATESTRUCT Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Long, Source As CREATESTRUCT, ByVal Length As Long)
    18.  
    19. Private Type CBT_CREATEWND
    20.     csThis As Long '\\ Pointer to CREATESTRUCT
    21.     hWndInsertAfter As Long
    22. End Type
    23.  
    24. Private Declare Sub CopyMemoryCBT_CreateWnd Lib "kernel32" Alias "RtlMoveMemory" (Destination As CBT_CREATEWND, ByVal Source As Long, ByVal Length As Long)
    25.  
    26. Private Declare Sub CopyMemoryFromCBT_CreateWnd Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Long, Source As CBT_CREATEWND, ByVal Length As Long)
    27.  
    28. '....snippet from use....
    29. Case HOOKPROC_CBT
    30.     Code = Arguments(1)
    31.     wParam = Arguments(2)
    32.     lParam = Arguments(3)
    33.     lResult = Arguments(4)
    34.     '\\ Create a new window for these to use...
    35.     Set wndThis = New ApiWindow
    36.  
    37.     '\\ Different events raised according to which message this is...
    38.     Select Case True
    39.  
    40.     Case Code = HCBT_CREATEWND
    41.         Dim CreateWndStruct As CBT_CREATEWND
    42.         Dim csThis As CREATESTRUCT
    43.         Dim sClassName As String, sName As String
    44.         Call CopyMemoryCBT_CreateWnd(CreateWndStruct, lParam, Len(CreateWndStruct))
    45.         If Err.LastDllError Then
    46.             ReportError Err.LastDllError, CLSNAME & ":HCBT_CREATEWND", GetLastSystemError
    47.         Else
    48.             Call CopyMemoryCreateStruct(csThis, CreateWndStruct.csThis, Len(csThis))
    49.             With csThis
    50.                 sClassName = StringFromPointer(.lpszClass, 32)
    51.                 sName = StringFromPointer(.lpszName, 32)
    52.                 RaiseEvent BeforeWindowCreate(sName, sClassName, .Height, .Width, .Left, .Top, .Style, .ExStyle, Cancel)
    53.             End With
    54.             If Not Cancel Then
    55.                 Call CopyMemoryFromCREATESTRUCT(CreateWndStruct.csThis, csThis, Len(csThis))
    56.             Else
    57.                 Arguments(4) = 1 '\\ This value is the hookproc return
    58.             End If
    59.         End If

    Thanks in advance,
    Duncan
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  2. #2
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148
    Extraordinary - it seems that a system/hidden window is created to handle events when the first RaiseEvent occurs and that this falls over if you have a CBT hook on it...I have coded around that so I think that's the last of the EventVB release H issues - I think I'll get a full release out this week...

  3. #3
    Lively Member
    Join Date
    May 2004
    Location
    Home
    Posts
    85

    Re: Local WH_CBT hook causes GPF - but only on the first window..?

    Are you able to post the final working code. I am still new to CBTs and I would like to view your workig code to get a better understanding at what exactly works.

    I have been experimenting with them, but I keep getting many GPFs = not good hah

    If you can that would be great.
    Thanks.

  4. #4
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: Local WH_CBT hook causes GPF - but only on the first window..?

    The source code is in EventVB.dll release I....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width