需要 VB6 代码到 C# 转换的帮助

发布于 2024-10-30 05:32:13 字数 3745 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

祁梦 2024-11-06 05:32:13

转换枚举很容易; C# 使用非常相似的语法(添加一些大括号):

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
}

SetEvents 子函数将成为返回类型为 void 的函数,所有方法调用都必须包装起来放在括号中(在 VB.NET 中也是如此),并且您不需要 AddressOf 运算符:

private void SetEvents()
{
    WR_mfSetEvent(E_EVENT_CODELINE, MFS100_OnCodeline);
    WR_mfSetEvent(E_EVENT_DOCUMENTDONE, MFS100_OnDocumentDone);
    WR_mfSetEvent(E_EVENT_ERROR, MFS100_OnError);
    WR_mfSetEvent(E_EVENT_ALLDONE, MFS100_OnAllDone);
}

您可能必须将这些函数(例如 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):

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
}

The SetEvents sub would become a function with a return type of void, all method calls must be wrapped in parentheses (also true in VB.NET), and you don't need the AddressOf operator:

private void SetEvents()
{
    WR_mfSetEvent(E_EVENT_CODELINE, MFS100_OnCodeline);
    WR_mfSetEvent(E_EVENT_DOCUMENTDONE, MFS100_OnDocumentDone);
    WR_mfSetEvent(E_EVENT_ERROR, MFS100_OnError);
    WR_mfSetEvent(E_EVENT_ALLDONE, MFS100_OnAllDone);
}

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.

落墨 2024-11-06 05:32:13

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文