Answered by:
How do you use "AddressOf" in VB 10?

Question
-
In vb6 "AddressOf" return a long pointer to an entry address for API calls, but in VB10 it returns a "Delegate" type. I have read a number of articles on "Delegates" but I am still having a hard time wrapping my head around the concept. I am trying to call an window API (SetWindowLong) that expects a long pointer (IntPtr) for the address of a sub routine that "overrides" the handler. I thought I had a solution using the "Marshal" class but it appears that I have run into snag on the "Delegate" type. .... the code below produces this error when I use "AddessOf" with "Marshal.GetFunctionPointerForDelegate" :
'AddressOf' expression cannot be converted to 'System.Delegate' because type 'System.Delegate' is declared 'MustInherit' and cannot be created.
<--- Code --->
Public Sub Hook()
Dim pointer As IntPtr = Marshal.GetFunctionPointerForDelegate(AddressOf WindowProc)
lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, pointer)
End Sub
Public Sub Unhook()
Dim temp As Long
temp = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
End Sub
Public Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim myScreens() As Screen = Screen.AllScreens
Dim MinMax As MINMAXINFO
If uMsg = WM_GETMINMAXINFO Then
CopyMemoryToMinMaxInfo(MinMax, lParam, Len(MinMax))
MinMax.ptMinTrackSize.x = 10
MinMax.ptMinTrackSize.y = 10
MinMax.ptMaxTrackSize.x = myScreens(0).Bounds.Height + 10
MinMax.ptMaxTrackSize.y = myScreens(0).Bounds.Width + 10
CopyMemoryFromMinMaxInfo(lParam, MinMax, Len(MinMax))
WindowProc = DefWindowProc(hw, uMsg, wParam, lParam)
Else
WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lParam)
End If
End Function
<--- End Code --->
I have been using VB for years to develop applications starting with the first version called "QuickBasic" and although there were always limitations I had to work around, and needed to use API calls to get things done, I never had problems using VB6 to access windows system calls. I am not dense, but this "Delegate" type is giving me fits!!Tuesday, July 31, 2012 12:15 AM
Answers
-
- Proposed as answer by Paul IshakModerator Wednesday, August 1, 2012 2:56 PM
- Marked as answer by Mark Liu-lxfModerator Tuesday, August 7, 2012 8:51 AM
Tuesday, July 31, 2012 12:51 AM -
Hi rdaviddd,
Welcome to the MSDN forum.
I think Armin has given your sample code. As for your code, it seems you are missing a delegate to use the code. Here is a sample and it seems can compile successfully (not test):
'declear a new delegate Delegate Function test(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Public Sub Hook() 'Using delegate to pass the WindowProc function Dim pointer As IntPtr = Marshal.GetFunctionPointerForDelegate(New test(AddressOf WindowProc)) lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, pointer) End Sub
Hope this helps.
Mark Liu-lxf [MSFT]
MSDN Community Support | Feedback to us
- Proposed as answer by Paul IshakModerator Wednesday, August 1, 2012 2:56 PM
- Marked as answer by Mark Liu-lxfModerator Tuesday, August 7, 2012 8:51 AM
Wednesday, August 1, 2012 3:18 AMModerator
All replies
-
- Proposed as answer by Paul IshakModerator Wednesday, August 1, 2012 2:56 PM
- Marked as answer by Mark Liu-lxfModerator Tuesday, August 7, 2012 8:51 AM
Tuesday, July 31, 2012 12:51 AM -
Hi rdaviddd,
Welcome to the MSDN forum.
I think Armin has given your sample code. As for your code, it seems you are missing a delegate to use the code. Here is a sample and it seems can compile successfully (not test):
'declear a new delegate Delegate Function test(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Public Sub Hook() 'Using delegate to pass the WindowProc function Dim pointer As IntPtr = Marshal.GetFunctionPointerForDelegate(New test(AddressOf WindowProc)) lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, pointer) End Sub
Hope this helps.
Mark Liu-lxf [MSFT]
MSDN Community Support | Feedback to us
- Proposed as answer by Paul IshakModerator Wednesday, August 1, 2012 2:56 PM
- Marked as answer by Mark Liu-lxfModerator Tuesday, August 7, 2012 8:51 AM
Wednesday, August 1, 2012 3:18 AMModerator