将VB6代码升级到VB.NET时遇到错误。错误发生在
AddressOf WindowProc
AddressOf表达式无法转换为“整数”,因为“整数”不是委托类型
我对SetWindowLong
的声明是:
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA"(
ByVal hWnd As Integer,
ByVal nIndex As Integer,
ByVal dwNewLong As Integer) As Integer
变量:
Dim GWL_WNDPROC As Short = -4
Dim hWnd As Integer
WindowProc
的代码:
Function WindowProc(ByVal hw As Integer, ByVal uMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Dim x As Integer
Dim a As String
Dim wp As Short
Dim temp As Object
Dim ReadBuffer(1000) As Byte
'Debug.Print uMsg, wParam, lParam
Select Case uMsg
Case 1025
Debug.Print(VB6.TabLayout(uMsg, wParam, lParam))
Debug.Print(uMsg & " " & wParam & " " & lParam)
e_err = WSAGetAsyncError(lParam)
e_errstr = GetWSAErrorString(e_err)
If e_err <> 0 Then
Debug.Print("Error String returned -> " & e_err & " - " & e_errstr)
Debug.Print("Terminating....")
do_cancel = True
'Exit Function
End If
Select Case lParam
Case FD_READ 'lets check for data
x = recv(mysock, ReadBuffer(0), 1000, 0) 'try to get some
If x > 0 Then 'was there any?
'UPGRADE_ISSUE: Constant vbUnicode was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
'default
'a = StrConv(System.Text.UnicodeEncoding.Unicode.GetString(ReadBuffer), vbUnicode) 'yep, lets change it to stuff we can understand
a = System.Text.UnicodeEncoding.Unicode.GetString(ReadBuffer) 'yep, lets change it to stuff we can understand
Debug.Print(a)
rtncode = Val(Mid(a, 1, 3))
'Debug.Print "Analysing code " & rtncode & "..."
Select Case rtncode
Case 354, 250
Progress = Progress + 1
Debug.Print(">>Progress becomes " & Progress)
Case 220
Debug.Print("Recieved Greenlight")
Green_Light = True
Case 221
Progress = Progress + 1
Debug.Print(">>Progress becomes " & Progress)
Case 550, 551, 552, 553, 554, 451, 452, 500
Debug.Print("There was some error at the server side")
Debug.Print("error code is " & rtncode)
do_cancel = True
End Select
End If
Case FD_CONNECT 'did we connect?
mysock = wParam 'yep, we did! yayay
'Debug.Print WSAGetAsyncError(lParam) & "error code"
'Debug.Print mysock & " - Mysocket Value"
Case FD_CLOSE 'uh oh. they closed the connection
Call closesocket(wp) 'so we need to close
End Select
End Select
'let the msg get through to the form
WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lParam)
End Function
我得到错误的原因是什么?我该如何解决这个问题?
答案 0 :(得分:2)
您是否考虑过覆盖表单的WndProc方法,而不是尝试使用P / Invoke来设置窗口过程?在重写期间可能需要更多的工作,但最终会得到更好的代码。上一个链接的示例:
Protected Overrides Sub WndProc(ByRef m As Message)
' Listen for operating system messages
Select Case (m.Msg)
' The WM_ACTIVATEAPP message occurs when the application
' becomes the active application or becomes inactive.
Case WM_ACTIVATEAPP
' The WParam value identifies what is occurring.
appActive = (m.WParam.ToInt32() <> 0)
' Invalidate to get new text painted.
Me.Invalidate()
End Select
MyBase.WndProc(m)
End Sub
您可能还希望在System.Net.Sockets命名空间中查找当前套接字代码的相应替换。
我还发现了一篇文章".NET Makes Window Subclassing Easy",例如,如果您不拥有您尝试子类化的窗口,那么该文章可能会有用。不管怎么说,不推荐的一种方法是尝试使用SetWindowLong
覆盖Window Proc