简体   繁体   中英

Need Help for VB6 Code to C# Conversion

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());

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM