AddressOf from VB6 to .Net VB

P

pavel.orehov

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.
 
P

pavel.orehov

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
 
?

=?iso-8859-1?Q?Jos=E9_Manuel_Ag=FCero?=

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.


<[email protected]> escribió en el mensaje | 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.
 
M

Mattias Sjögren

Check out the NativeWindow class for a better way to do window
subclassing in maanged code.


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top