Home All Groups Group Topic Archive Search About

AddressOf from VB6 to .Net VB

Author
22 Mar 2006 10:05 AM
pavel.orehov@gmail.com
Private Delegate Function WndCallback _
        (ByVal hw As Integer, _
        ByVal uMsg As Integer, _
        ByVal wParam As Integer, _
        ByVal lParam As Integer) As Integer

  Private Declare Function SetWindowLong Lib "USER32.DLL" Alias
"SetWindowLongA" _
        (ByVal hwnd As Integer, _
        ByVal nIndex As Integer, _
        ByVal dwNewLong As WndCallback) As WndCallback

Private PrevWndPRoc As WndCallback

Public Sub RegisterFormForOrientationDetection(ByRef f As
System.Windows.Forms.Form)
        PrevWndPRoc = SetWindowLong(f.Handle.ToInt32(), _
                                    GWL_WNDPROC, _
BBB->                           AddressOf WndProcWithRotationDetection)
End Sub

Hi,

I have converted the VB6 code to .NET according to microsoft.
The compilation is okey, but i get an exception in runtime  on line BBB
that says:

Invalid function pointer 0xffff0557 was passed into the runtime to be
converted to a delegate. Passing in invalid function pointers to be
converted to delegates can cause crashes, corruption or data loss.

What i did wrong ?

Thanks,
Pavel.

Author
22 Mar 2006 10:11 AM
pavel.orehov@gmail.com
Sorry, i forgit the function itself.

Public Function WndProcWithRotationDetection(ByVal hw As Integer, _
                                          ByVal uMsg As Integer, _
                                          ByVal wParam As Integer, _
                                          ByVal lParam As Integer) As
Integer
          ' Do the job

End Functon
Author
22 Mar 2006 11:05 AM
José_Manuel_Agüero
Hello Pavel,

If your WndProcWithRotationDetection function is not in a module, you must declare it Shared.
With Windows API functions that admit ANSI and Unicode versions, you should declare them Auto to avoid trouble when passing strings (it's only my opinion):
Declare Auto Function SetWindowLong Lib "USER32.DLL" (ByVal hwnd ...

Anyway, if you are processing Windows messages sent to your form, you don't need to do Platform Invoke: You can override the functions PreProcessMessage and WndProc.
If you only want to send messages, you can override DefWndProc.
You  can also create a class that implements IMessageFilter and use Application.AddMessageFilter to activate it. This only let you see the messages and to stop them from reaching WndProc.

Regards.


Show quoteHide quote
<pavel.ore***@gmail.com> escribió en el mensaje news:1143021942.238057.85900@g10g2000cwb.googlegroups.com...
| Private Delegate Function WndCallback _
|        (ByVal hw As Integer, _
|        ByVal uMsg As Integer, _
|        ByVal wParam As Integer, _
|        ByVal lParam As Integer) As Integer
|
|  Private Declare Function SetWindowLong Lib "USER32.DLL" Alias
| "SetWindowLongA" _
|        (ByVal hwnd As Integer, _
|        ByVal nIndex As Integer, _
|        ByVal dwNewLong As WndCallback) As WndCallback
|
| Private PrevWndPRoc As WndCallback
|
| Public Sub RegisterFormForOrientationDetection(ByRef f As
| System.Windows.Forms.Form)
|        PrevWndPRoc = SetWindowLong(f.Handle.ToInt32(), _
|                                    GWL_WNDPROC, _
| BBB->                           AddressOf WndProcWithRotationDetection)
| End Sub
|
| Hi,
|
| I have converted the VB6 code to .NET according to microsoft.
| The compilation is okey, but i get an exception in runtime  on line BBB
| that says:
|
| Invalid function pointer 0xffff0557 was passed into the runtime to be
| converted to a delegate. Passing in invalid function pointers to be
| converted to delegates can cause crashes, corruption or data loss.
|
| What i did wrong ?
|
| Thanks,
| Pavel.
Author
22 Mar 2006 9:33 PM
Mattias Sjögren
Check out the NativeWindow class for a better way to do window
subclassing in maanged code.


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.