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