需要 VB6 代码到 C# 转换的帮助
Private Sub SetEvents()
WR_mfSetEvent E_EVENT_CODELINE, AddressOf MFS100_OnCodeline
WR_mfSetEvent E_EVENT_DOCUMENTDONE, AddressOf MFS100_OnDocumentDone
WR_mfSetEvent E_EVENT_ERROR, AddressOf MFS100_OnError
WR_mfSetEvent E_EVENT_ALLDONE, AddressOf MFS100_OnAllDone
End Sub
Enum EventEnum
E_EVENT_CODELINE = 0
E_EVENT_RAWIMAGEFRONT
E_EVENT_RAWIMAGEBACK
E_EVENT_RAWMICRBUFFER
E_EVENT_RAWOCRBUFFER
E_EVENT_DOCUMENTDONE
E_EVENT_WARNING
E_EVENT_ERROR
E_EVENT_ALLDONE
End Enum
我如何将此代码转换为 C# ?
更新 1:我在使用 Scan 和 ScanFeeder 时遇到问题。 ScanFeeder 返回 MICR 代码,但我无法处理它。扫描功能出现读写内存错误。
' mflib routines
' ------------------------------------------------------------------------------------
Declare Function mfInit Lib "mflib.dll.stdcall" (ByVal mode As Long, ByVal fileIni As String) As Long
Declare Function mfClose Lib "mflib.dll.stdcall" (ByVal mode As Long) As Long
Declare Function mfTrace Lib "mflib.dll.stdcall" (ByVal level As Long, ByVal msg As String) As Long
Declare Function mfTraceSet Lib "mflib.dll.stdcall" (ByVal mode As Long, ByVal tracelevel As Long, ByVal maxlen As Long, ByVal fullpathname As String) As Long
Declare Function mfSetParam Lib "mflib.dll.stdcall" (ByVal paramCode As Long, ByVal paramValue As String) As Long
Declare Function mfReset Lib "mflib.dll.stdcall" (ByVal mode As Long) As Long
Declare Function mfGetStatus Lib "mflib.dll.stdcall" (ByVal mode As Long, ByVal ret_statusbuffer As String) As Long
Declare Function mfScan Lib "mflib.dll.stdcall" (ByVal mode As Long, ByVal font As Long, ByVal timeout As Long, ByVal ret_codeline As String, ByRef ret_maxchars As Long) As Long
Declare Function mfEject Lib "mflib.dll.stdcall" (ByVal mode As Long, ByVal pocket As Long, ByVal timeout As Long) As Long
Declare Function mfCalib Lib "mflib.dll.stdcall" (ByVal calibID As Long, ByVal mode As Long, ByRef ret_value As Long) As Long
Declare Function mfScanFeeder Lib "mflib.dll.stdcall" (ByVal mode As Long, ByVal font As Long, ByVal timeout As Long) As Long
Declare Function mfSetEvent Lib "mflib.dll.stdcall" (ByVal eventId As Long, ByVal callback As Long) As Long
Declare Function mfGetErrorDescription Lib "mflib.dll.stdcall" (ByVal errorCode As Long, ByVal ret_errorDescription As String) As Long
Declare Function mfIQALoadConfig Lib "mflib.dll.stdcall" (ByVal configfile As String) As Long
Declare Function mfIQASetParameter Lib "mflib.dll.stdcall" (ByVal paramid As Long, ByVal value As String) As Long
Declare Function mfIQATestFile Lib "mflib.dll.stdcall" (ByVal mask As Long, ByVal front As String, ByVal rear As String) As Long
Declare Function mfIQAGetErrorDescription Lib "mflib.dll.stdcall" (ByVal errorid As Long, ByVal desc As String, ByVal maxlen As Long) As Long
Declare Sub mfSort Lib "mflib.dll.stdcall" (ByVal pocket As Long)
Declare Function mfStop Lib "mflib.dll.stdcall" () As Long
更新 2:
Public Function WR_mfSetEvent(eventId As Long, callback As Long) As Long
WriteEvent " event " & GetEventName(eventId)
WR_mfSetEvent = mfSetEvent(eventId, callback)
WriteEventMFSRes WR_mfSetEvent
CScannerForm.lsTest.AddItem ("mfSetEvent : " & WR_mfSetEvent)
End Function
Public Sub WriteEventMFSRes(res As Long)
If res >= 0 Then
If res <> 1 Then WriteEventRes " (" & res & ")"
Else
WriteEventRes " (" & res & "='" & WR_mfGetErrorDescription(res) & "')"
End If
End Sub
更新 2: 这是正确的方法吗?我以这种方式得到回调
MFS100_CodeLineDelegate OnCodeline = new MFS100_CodeLineDelegate(MFS100_OnCodeLine);
Adress = Marshal.GetFunctionPointerForDelegate(OnCodeline);
mfSetEvent(0, Adress.ToInt32());
Private Sub SetEvents()
WR_mfSetEvent E_EVENT_CODELINE, AddressOf MFS100_OnCodeline
WR_mfSetEvent E_EVENT_DOCUMENTDONE, AddressOf MFS100_OnDocumentDone
WR_mfSetEvent E_EVENT_ERROR, AddressOf MFS100_OnError
WR_mfSetEvent E_EVENT_ALLDONE, AddressOf MFS100_OnAllDone
End Sub
Enum EventEnum
E_EVENT_CODELINE = 0
E_EVENT_RAWIMAGEFRONT
E_EVENT_RAWIMAGEBACK
E_EVENT_RAWMICRBUFFER
E_EVENT_RAWOCRBUFFER
E_EVENT_DOCUMENTDONE
E_EVENT_WARNING
E_EVENT_ERROR
E_EVENT_ALLDONE
End Enum
How can i convert this code to C# ?
Update 1: I have problem with Scan and ScanFeeder. ScanFeeder returns an MICR code but i cant handle it. The Scan function gets an read write memory error.
' mflib routines
' ------------------------------------------------------------------------------------
Declare Function mfInit Lib "mflib.dll.stdcall" (ByVal mode As Long, ByVal fileIni As String) As Long
Declare Function mfClose Lib "mflib.dll.stdcall" (ByVal mode As Long) As Long
Declare Function mfTrace Lib "mflib.dll.stdcall" (ByVal level As Long, ByVal msg As String) As Long
Declare Function mfTraceSet Lib "mflib.dll.stdcall" (ByVal mode As Long, ByVal tracelevel As Long, ByVal maxlen As Long, ByVal fullpathname As String) As Long
Declare Function mfSetParam Lib "mflib.dll.stdcall" (ByVal paramCode As Long, ByVal paramValue As String) As Long
Declare Function mfReset Lib "mflib.dll.stdcall" (ByVal mode As Long) As Long
Declare Function mfGetStatus Lib "mflib.dll.stdcall" (ByVal mode As Long, ByVal ret_statusbuffer As String) As Long
Declare Function mfScan Lib "mflib.dll.stdcall" (ByVal mode As Long, ByVal font As Long, ByVal timeout As Long, ByVal ret_codeline As String, ByRef ret_maxchars As Long) As Long
Declare Function mfEject Lib "mflib.dll.stdcall" (ByVal mode As Long, ByVal pocket As Long, ByVal timeout As Long) As Long
Declare Function mfCalib Lib "mflib.dll.stdcall" (ByVal calibID As Long, ByVal mode As Long, ByRef ret_value As Long) As Long
Declare Function mfScanFeeder Lib "mflib.dll.stdcall" (ByVal mode As Long, ByVal font As Long, ByVal timeout As Long) As Long
Declare Function mfSetEvent Lib "mflib.dll.stdcall" (ByVal eventId As Long, ByVal callback As Long) As Long
Declare Function mfGetErrorDescription Lib "mflib.dll.stdcall" (ByVal errorCode As Long, ByVal ret_errorDescription As String) As Long
Declare Function mfIQALoadConfig Lib "mflib.dll.stdcall" (ByVal configfile As String) As Long
Declare Function mfIQASetParameter Lib "mflib.dll.stdcall" (ByVal paramid As Long, ByVal value As String) As Long
Declare Function mfIQATestFile Lib "mflib.dll.stdcall" (ByVal mask As Long, ByVal front As String, ByVal rear As String) As Long
Declare Function mfIQAGetErrorDescription Lib "mflib.dll.stdcall" (ByVal errorid As Long, ByVal desc As String, ByVal maxlen As Long) As Long
Declare Sub mfSort Lib "mflib.dll.stdcall" (ByVal pocket As Long)
Declare Function mfStop Lib "mflib.dll.stdcall" () As Long
Update 2:
Public Function WR_mfSetEvent(eventId As Long, callback As Long) As Long
WriteEvent " event " & GetEventName(eventId)
WR_mfSetEvent = mfSetEvent(eventId, callback)
WriteEventMFSRes WR_mfSetEvent
CScannerForm.lsTest.AddItem ("mfSetEvent : " & WR_mfSetEvent)
End Function
Public Sub WriteEventMFSRes(res As Long)
If res >= 0 Then
If res <> 1 Then WriteEventRes " (" & res & ")"
Else
WriteEventRes " (" & res & "='" & WR_mfGetErrorDescription(res) & "')"
End If
End Sub
Update 2: is this the correct way. I get callback in this way
MFS100_CodeLineDelegate OnCodeline = new MFS100_CodeLineDelegate(MFS100_OnCodeLine);
Adress = Marshal.GetFunctionPointerForDelegate(OnCodeline);
mfSetEvent(0, Adress.ToInt32());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
转换枚举很容易; C# 使用非常相似的语法(添加一些大括号):
SetEvents
子函数将成为返回类型为void
的函数,所有方法调用都必须包装起来放在括号中(在 VB.NET 中也是如此),并且您不需要AddressOf
运算符:您可能必须将这些函数(例如
MFS100_OnCodeline
)实现为 <一个href="http://msdn.microsoft.com/en-us/library/ms173171.aspx" rel="nofollow">代表。 C# 有一个完全不同的语法模型来将函数分配给委托/事件,但是您不会引发 VB 6 中内置的典型事件。这是某种自定义库,因此如果不确切了解它们是如何的,则很难更具体。定义以及他们做什么。Converting the enum is easy; C# uses a very similar syntax (with a few curly braces thrown in):
The
SetEvents
sub would become a function with a return type ofvoid
, all method calls must be wrapped in parentheses (also true in VB.NET), and you don't need theAddressOf
operator:You might have to implement those functions (
MFS100_OnCodeline
, for example) as delegates. C# has an entirely different syntax model for assigning functions to delegates/events, but you're not raising the typical events built into VB 6. This is some kind of custom library, so it's hard to be more specific without knowing exactly how they are defined and what they do.看看 pInvoke。这将使您了解如何访问 DLL 中定义的内容。
我搜索了
WR_mfSetEvent
但什么也没找到 - 这是您正在使用的私有库吗?如果不知道您在做什么,就很难为您提供帮助。Have a look at pInvoke. This will give you an idea of how to access stuff defined in DLLs.
I did a search for
WR_mfSetEvent
but found nothing - is this a private library you're using? Without knowing what you're doing, it will be hard to help you.