robbid
asked on
Problem with accessing protected memory after creating delegate in VB.NET 2005
I have taken some sample code from an application written in VB6. I used the upgrade wizard to upgrade it to VB 2005. It told me I needed to add a delegate for AddressOf WindowProc.
I followed the example from the help file and got the program to compile. It works fine for a while, then after a while (the time varies) I receive an error that it "Attempted to read or write protected memory." Text from error is below the code.
I don't understand what is happening. I'm a novice programmer and this doesn't make sense to me.
What the program is doing (as I understand it) is that it uses a .dll to query a server. This server sends back packets to this program asynchronously. The program then receives the data packet and determines what to do with it from there.
Any help you can give me will be greatly appreciated.
'========================= ========== ========== =====
'++++++++++++++Excerpt from Module1:
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA"(ByVal lpPrevWndFunc As Integer, ByVal hwnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
'----------------The following 3 lines were modified from the initial build to accomodate a delegate:
'Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Integer, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Integer, ByVal nIndex As Integer, ByVal dwNewLong As SubClassProcDelegate) As Integer
Delegate Function SubClassProcDelegate(ByVal hw As Integer, ByVal uMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
'----------------
Public Const GWL_WNDPROC As Short = -4
Public lpPrevWndProc As Integer
Public gHW As Integer
'-----------------
Function WindowProc(ByVal hw As Integer, ByVal uMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
'---------------
Dim intErrCode As Short
Dim strTime As New VB6.FixedLengthString(40)
If (uMsg = WM_RTB_MESSAGE) Then
' We are receiving a message.
' wParam tells us what it is. We will display some
' message according to it.
Select Case wParam
Case 1
HelloVB.lblStatus.Text = "Trying primary"
Case 2
HelloVB.lblStatus.Text = "Trying secondary"
Case 3
HelloVB.lblStatus.Text = "Logon Completed"
Case 4
'HelloVB.ShowRTBErrorMessa ge
Case 5
' lParam is the DataPacket ID.
' Get the time out of the data packet.
intErrCode = RTBDataGetString(lParam, 0, 0, strTime.Value, 40)
HelloVB.lblData.Text = "Hello World, it's " & strTime.Value & "!"
' Free the data packet
intErrCode = RTBFreePacket(lParam)
End Select
End If
'-------------
WindowProc = CallWindowProc(lpPrevWndPr oc, hw, uMsg, wParam, lParam)
'-------------
End Function
'========================= ========== ========== =====
'+++++++++++++Excerpt from Main Class Form
'UPGRADE_WARNING: Add a delegate for AddressOf WindowProc Click for more: 'ms-help://MS.VSCC.v80/dv_ commoner/l ocal/redir ect.htm?ke yword="E9E 157F7-EF0C -4016-87B7 -7D7FBBC6E E08"'
'-----------
lpPrevWndProc = SetWindowLong(Me.Handle.To Int32, GWL_WNDPROC, AddressOf WindowProc)
'-----------' Create the query
'========================= ========== ========== =====
Error information
************** Exception Text **************
System.AccessViolationExce ption: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Windows.Forms.Unsaf eNativeMet hods.CallW indowProc( IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Nativ eWindow.De fWndProc(M essage& m)
at System.Windows.Forms.Form. DefWndProc (Message& m)
at System.Windows.Forms.Contr ol.WndProc (Message& m)
at System.Windows.Forms.Scrol lableContr ol.WndProc (Message& m)
at System.Windows.Forms.Conta inerContro l.WndProc( Message& m)
at System.Windows.Forms.Form. WmSysComma nd(Message & m)
at System.Windows.Forms.Form. WndProc(Me ssage& m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.OnMessa ge(Message & m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.WndProc (Message& m)
at System.Windows.Forms.Nativ eWindow.Ca llback(Int Ptr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
I followed the example from the help file and got the program to compile. It works fine for a while, then after a while (the time varies) I receive an error that it "Attempted to read or write protected memory." Text from error is below the code.
I don't understand what is happening. I'm a novice programmer and this doesn't make sense to me.
What the program is doing (as I understand it) is that it uses a .dll to query a server. This server sends back packets to this program asynchronously. The program then receives the data packet and determines what to do with it from there.
Any help you can give me will be greatly appreciated.
'=========================
'++++++++++++++Excerpt from Module1:
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA"(ByVal lpPrevWndFunc As Integer, ByVal hwnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
'----------------The following 3 lines were modified from the initial build to accomodate a delegate:
'Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Integer, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Integer, ByVal nIndex As Integer, ByVal dwNewLong As SubClassProcDelegate) As Integer
Delegate Function SubClassProcDelegate(ByVal
'----------------
Public Const GWL_WNDPROC As Short = -4
Public lpPrevWndProc As Integer
Public gHW As Integer
'-----------------
Function WindowProc(ByVal hw As Integer, ByVal uMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
'---------------
Dim intErrCode As Short
Dim strTime As New VB6.FixedLengthString(40)
If (uMsg = WM_RTB_MESSAGE) Then
' We are receiving a message.
' wParam tells us what it is. We will display some
' message according to it.
Select Case wParam
Case 1
HelloVB.lblStatus.Text = "Trying primary"
Case 2
HelloVB.lblStatus.Text = "Trying secondary"
Case 3
HelloVB.lblStatus.Text = "Logon Completed"
Case 4
'HelloVB.ShowRTBErrorMessa
Case 5
' lParam is the DataPacket ID.
' Get the time out of the data packet.
intErrCode = RTBDataGetString(lParam, 0, 0, strTime.Value, 40)
HelloVB.lblData.Text = "Hello World, it's " & strTime.Value & "!"
' Free the data packet
intErrCode = RTBFreePacket(lParam)
End Select
End If
'-------------
WindowProc = CallWindowProc(lpPrevWndPr
'-------------
End Function
'=========================
'+++++++++++++Excerpt from Main Class Form
'UPGRADE_WARNING: Add a delegate for AddressOf WindowProc Click for more: 'ms-help://MS.VSCC.v80/dv_
'-----------
lpPrevWndProc = SetWindowLong(Me.Handle.To
'-----------' Create the query
'=========================
Error information
************** Exception Text **************
System.AccessViolationExce
at System.Windows.Forms.Unsaf
at System.Windows.Forms.Nativ
at System.Windows.Forms.Form.
at System.Windows.Forms.Contr
at System.Windows.Forms.Scrol
at System.Windows.Forms.Conta
at System.Windows.Forms.Form.
at System.Windows.Forms.Form.
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Nativ
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks,
This article helps me understand the process a little better. I still don't understand why the delegate is being collected, though. I think I might try re-writing the code myself (using information from the article you provided and some others) and see what happens.
This article helps me understand the process a little better. I still don't understand why the delegate is being collected, though. I think I might try re-writing the code myself (using information from the article you provided and some others) and see what happens.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
A callback was made on a garbage collected delegate of type 'Project1!Project1.Module1