我试图将扫描程序集成到我的代码中,在他们提供的示例解决方案中,他们使用DllImport,Delegate函数,回调程序等等。
这只是我第一次遇到dllImport,委托,回调程序,每次我调试他们给我的解决方案,从初始化开始,然后按钮点击然后突然我总是收到错误" vshost32.exe已经停止工作"
这是我的代码:
从DLL导入的函数:
Private Declare Function rdInit Lib "sb6lib" (ByVal tmo As Integer) As Integer
Private Declare Function rdStart Lib "sb6lib" () As Integer
Private Declare Function rdsConfig Lib "sb6lib" (ByVal Wnd As Integer, ByVal confile As String) As Integer
Private Declare Function rdConfigAPI Lib "sb6lib" (ByVal tmo As Integer, ByVal tmo As Integer) As Integer
使用委托来实施回调程序。
Public Delegate Sub FeederEmptyDelegate(ByVal x As Integer)
Private Declare Function rdOnFeederEmpty Lib "sb6lib" (ByVal lpfeederempty As FeederEmptyDelegate) As Integer
Public Delegate Sub AllDoneDelegate(ByVal x As Integer)
Private Declare Function rdOnAllDone Lib "sb6lib" (ByVal lpx As AllDoneDelegate) As Integer
Delegate Function CodeLineVBDelegate(ByVal lpx As IntPtr) As Integer
Private Declare Function rdOnCodeLineVB Lib "sb6lib" (ByVal lpx As CodeLineVBDelegate) As Integer
Public Delegate Sub ErrorDelegate(ByVal x As Integer)
Private Declare Function rdOnError Lib "sb6lib" (ByVal lpx As ErrorDelegate) As Integer
Public Delegate Function DocumentDoneDelegate(ByVal lpx As Integer) As Integer
Private Declare Function rdOnDocumentDone Lib "sb6lib" (ByVal lpx As DocumentDoneDelegate) As Integer
On Command1_Click Event -Stands for Initialize Button
Public Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
Dim rtnval As Object
' find the RDS and initialize the API
rtnval = rdInit(-1)
List1.Items.Add(("rdInit returns " & rtnval))
If rtnval > -1 Then
rtnval = rdConfigAPI(4101, 0)
rtnval = rdsConfig(Frame1.Handle.ToInt32, ".\\DOCDES0.DES")
List1.Items.Add(("rdsConfig returns " & rtnval))
'Callbacks
rtnval = rdOnFeederEmpty(AddressOf feederempty)
rtnval = rdOnAllDone(AddressOf alldone)
rtnval = rdOnCodeLineVB(AddressOf codeline)
rtnval = rdOnError(AddressOf onerror)
rtnval = rdOnDocumentDone(AddressOf docdone)
Command2.Enabled = True
Command1.Enabled = False
Command3.Enabled = True
ViewButton.Enabled = True
End If
End Sub
在Command2_Click事件上 - 用于开始扫描按钮
Private Sub Command2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command2.Click
Dim rtnval As Object
rtnval = rdStart()
List1.Items.Add(("rdStart returns " & rtnval))
End Sub
扫描后(End Sub)进入回调功能"代码行" :(调试模式)
Private Function codeline(ByVal x As IntPtr) As Integer
' called when a codeline is available
Dim cl As String
cl = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(x) 'conversion necessary as passed argument is a BSTR type string in sb6lib
If Me.InvokeRequired Then
Me.Invoke(New WriteTextBoxDelegate(AddressOf codeline), New Object() {x})
Else
yval.Text = "CL: " & cl
End If
codeline = 1
End Function
然后应用程序停止工作(" vshost32.exe已停止工作")。
sombebody可以告诉我导致应用程序崩溃的原因是什么?谢谢!