Can someone pls help me turn this vb code into vb.net?? (simple subclass)

simchiner

New member
Joined
May 8, 2007
Messages
1
Programming Experience
1-3
Hi, I have this code that will notify my vb 6 program if any program in windows is launched or closed. It is done by subclassing a vb form.

I am trying to turn it into a vb.net application, but took me very very long time but still no luck. Program is not long, but I don't undertand subclassing too much. Can someone help me take a look and turn it into something that I can use in vb.net?

here is callback module, I have attached the whole program.

Option Explicit
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcW" ( _
ByVal lpPrevWndFunc As Long, _
ByVal hWnd As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long _
) As Long
Declare Function DefWindowProc Lib "user32" Alias "DefWindowProcW" ( _
ByVal hWnd As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long _
) As Long
Declare Function RegisterShellHookWindow Lib "user32" ( _
ByVal hWnd As Long _
) As Long
Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageW" ( _
ByVal lpString As Long _
) As Long

Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongW" ( _
ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long _
) As Long

Const HSHELL_WINDOWCREATED = 1
Const HSHELL_WINDOWDESTROYED = 2
Const GWL_WNDPROC = -4
Private mpNotify As IShell
Private mlShellMsg As Long
Private mhWndForm As Long
Private mOldWndProc As Long
'
Public Sub main()
Dim hForm As FMain
mlShellMsg = RegisterWindowMessage(StrPtr("SHELLHOOK"))

Set hForm = New FMain
hForm.Show

Set hForm = Nothing
End Sub
'
Public Sub Subclass(ByRef pNotify As IShell)
mhWndForm = pNotify.Handle
Set mpNotify = pNotify
mOldWndProc = SetWindowLong(pNotify.Handle, GWL_WNDPROC, AddressOf WndProc)
End Sub
'
Public Sub Unsubclass(ByRef pNotify As IShell)
SetWindowLong mpNotify.Handle, -4, mOldWndProc
Set mpNotify = Nothing
End Sub
'
Private Function WndProc( _
ByVal hWnd As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long _
) As Long
WndProc = CallWindowProc(mOldWndProc, hWnd, uMsg, wParam, lParam)
If (hWnd = mhWndForm) Then
If (uMsg = mlShellMsg) Then
Select Case (wParam)
Case HSHELL_WINDOWCREATED:
mpNotify.WindowCreated lParam

Case HSHELL_WINDOWDESTROYED:
mpNotify.WindowDestroyed lParam
End Select

End If

End If
End Function

Thanks for any help.
 

Attachments

  • Shell Event Notifications.zip
    2.9 KB · Views: 14
you can hook windows messages in .net

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

Select Case m.WParam.ToInt32
'message
End Select
MyBase.WndProc(m)
End Sub
 
Back
Top