请教,VB6程序转成VB.net报Addressof表达式不能转换为long,因为long不是委托类型

ksaponka_tang 2012-10-11 07:50:16
程序如下:

'-- UNZIP32.DLL Userfunctions Structure
Structure USERFUNCTION
Public UZDLLPrnt As Long ' Pointer To Apps Print Function
Public UZDLLSND As Long ' Pointer To Apps Sound Function
Public UZDLLREPLACE As Long ' Pointer To Apps Replace Function
Public UZDLLPASSWORD As Long ' Pointer To Apps Password Function
Public UZDLLMESSAGE As Long ' Pointer To Apps Message Function
Public UZDLLSERVICE As Long ' Pointer To Apps Service Function (Not Coded!)
Public TotalSizeComp As Long ' Total Size Of Zip Archive
Public TotalSize As Long ' Total Size Of All Files In Archive
Public CompFactor As Long ' Compression Factor
Public NumMembers As Long ' Total Number Of All Files In The Archive
Public cchComment As Integer ' Flag If Archive Has A Comment!
End Structure

Public Function FnPtr(ByVal lp As Long) As Long

FnPtr = lp

End Function

Public Function UZDLLPrnt(ByRef fname As UNZIPCBChar, ByVal x As Long) As Long

Dim s0 As String
Dim xx As Long

'-- Always Put This In Callback Routines!
On Error Resume Next

s0 = ""

'-- Gets The UNZIP32.DLL Message For Displaying.
For xx = 0 To x - 1
If fname.ch(xx) = 0 Then Exit For
s0 = s0 & Chr(fname.ch(xx))
Next

'-- Assign Zip Information
If Mid$(s0, 1, 1) = vbLf Then s0 = vbNewLine ' Damn UNIX :-)

UZDLLPrnt = 0

End Function

Public Function VBUnZip32(ByVal uzdcl As DCLIST, ByVal uZipNames As UNZIPnames, ByVal uNumberFiles As Long, ByVal uNumberXFiles As Long, _
ByVal uExcludeNames As UNZIPnames) As Long

Dim retcode As Long
Dim MsgStr As String

Dim UZUSER As USERFUNCTION
Dim UZVER As UZPVER

Dim _UZDLLPrnt = New delegateUZDLLPrnt(AddressOf UZDLLPrnt)


retcode = 1

'-- Set Callback Addresses
'-- (WARNING!!!) Do Not Change
UZUSER.UZDLLPrnt = FnPtr(AddressOf UZDLLPrnt)
UZUSER.UZDLLSND = 0& '-- Not Supported
UZUSER.UZDLLREPLACE = FnPtr(AddressOf UZDLLRep)
UZUSER.UZDLLPASSWORD = FnPtr(AddressOf UZDLLPass)
UZUSER.UZDLLMESSAGE = FnPtr(AddressOf UZReceiveDLLMessage)
UZUSER.UZDLLSERVICE = FnPtr(AddressOf UZDLLServ)

问题出在highlighted ,问题非常急,请各位帮忙,谢谢。
...全文
370 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ksaponka_tang 2012-10-11
  • 打赏
  • 举报
回复
请各位帮忙呀,在线等,急。谢谢
ksaponka_tang 2012-10-11
  • 打赏
  • 举报
回复
非常感谢 silverradiance 的回复,功能是取得函数指针存到type中。
VB程序如下:
'-- Main UNZIP32.DLL UnZip32 Subroutine
'-- (WARNING!) Do Not Change!
Public Function VBUnZip32(uzdcl As DCLIST, uZipNames As UNZIPnames, uNumberFiles As Long, uNumberXFiles As Long, _
uExcludeNames As UNZIPnames) As Long

Dim retcode As Long
Dim MsgStr As String

Dim UZUSER As USERFUNCTION
Dim UZVER As UZPVER

retcode = 1

'-- Set Callback Addresses
'-- (WARNING!!!) Do Not Change
UZUSER.UZDLLPrnt = FnPtr(AddressOf UZDLLPrnt)
UZUSER.UZDLLSND = 0& '-- Not Supported
UZUSER.UZDLLREPLACE = FnPtr(AddressOf UZDLLRep)
UZUSER.UZDLLPASSWORD = FnPtr(AddressOf UZDLLPass)
UZUSER.UZDLLMESSAGE = FnPtr(AddressOf UZReceiveDLLMessage)
UZUSER.UZDLLSERVICE = FnPtr(AddressOf UZDLLServ)

'-- Set UNZIP32.DLL Version Space
'-- (WARNING!!!) Do Not Change
With UZVER
.structlen = Len(UZVER)
.beta = Space$(9) & vbNullChar
.date = Space$(19) & vbNullChar
.zlib = Space$(9) & vbNullChar
End With

'-- Get Version
Call UzpVersion2(UZVER)

'--------------------------------------
'-- You Can Change This For Displaying
'-- The Version Information!
'--------------------------------------
MsgStr$ = "DLL Date: " & szTrim(UZVER.date)
MsgStr$ = MsgStr$ & vbNewLine$ & "Zip Info: " & Hex$(UZVER.zipinfo(1)) & "." & _
Hex$(UZVER.zipinfo(2)) & Hex$(UZVER.zipinfo(3))
MsgStr$ = MsgStr$ & vbNewLine$ & "DLL Version: " & Hex$(UZVER.windll(1)) & "." & _
Hex$(UZVER.windll(2)) & Hex$(UZVER.windll(3))
MsgStr$ = MsgStr$ & vbNewLine$ & "--------------"
'-- End Of Version Information.

'-- Go UnZip The Files! (Do Not Change Below!!!)
'-- This Is The Actual UnZip Routine
retcode = Wiz_SingleEntryUnzip(uNumberFiles, uZipNames, uNumberXFiles, _
uExcludeNames, uzdcl, UZUSER)
'---------------------------------------------------------------

'-- If There Is An Error Display A MsgBox!
'If retcode <> 0 Then MsgBox retcode

VBUnZip32 = retcode

'-- You Can Change This As Needed!
'-- For Compression Information
' MsgStr$ = MsgStr$ & vbNewLine & "Only Shows If uExtractList = 1 List Contents"
' MsgStr$ = MsgStr$ & vbNewLine & "--------------"
' MsgStr$ = MsgStr$ & vbNewLine & "Comment : " & UZUSER.cchComment
' MsgStr$ = MsgStr$ & vbNewLine & "Total Size Comp : " & UZUSER.TotalSizeComp
' MsgStr$ = MsgStr$ & vbNewLine & "Total Size : " & UZUSER.TotalSize
' MsgStr$ = MsgStr$ & vbNewLine & "Compress Factor : %" & UZUSER.CompFactor
' MsgStr$ = MsgStr$ & vbNewLine & "Num Of Members : " & UZUSER.NumMembers
' MsgStr$ = MsgStr$ & vbNewLine & "--------------"
'
' VBUnzFrm.MsgOut.Text = VBUnzFrm.MsgOut.Text & MsgStr$ & vbNewLine$
End Function

'-- Callback For UNZIP32.DLL - Print Message Function
Public Function UZDLLPrnt(ByRef fname As UNZIPCBChar, ByVal x As Long) As Long

Dim s0 As String
Dim xx As Long

'-- Always Put This In Callback Routines!
On Error Resume Next

s0 = ""

'-- Gets The UNZIP32.DLL Message For Displaying.
For xx = 0 To x - 1
If fname.ch(xx) = 0 Then Exit For
s0 = s0 & Chr$(fname.ch(xx))
Next

'-- Assign Zip Information
If Mid$(s0, 1, 1) = vbLf Then s0 = vbNewLine ' Damn UNIX :-)
'uZipInfo = uZipInfo & s0

UZDLLPrnt = 0

End Function

在vb中是ok的,我现在要转成vb.net,因为我对委托不太熟,看了silverradiance的回复还不是很清楚,请给我更详细的代码,非常感谢
  • 打赏
  • 举报
回复
AddressOf 后面需要的参数是个和委托参数返回值一致 的方法


Dim hander As EventHandler = New EventHandler(AddressOf TestFun)


'这里的方法必须和EventHandler 的参数和返回值一致
Public Sub TestFun(ByVal senser As Object, ByVal e As EventArgs)

End Sub

VB.net强命名,类型安全,在安全模式下不能像c语言一样的通过方法指针去条用方法,.net提供了委托来代替它。
不知道你这里是要做什么,看你的代码都是long,为什么要使用AddressOf ?直接赋值不就行了

16,549

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧