Visual Basic与Twain应用程序

时间:2017-05-23 03:40:15

标签: vb.net visual-studio-2015

你好我对vb有一点了解,所以请帮帮我。当我运行可以访问我的扫描仪的程序时,它有一个错误" AccessViolationExeption未处理" "尝试读取或写入受保护的内存。这通常表明其他内存已损坏。"

这是我的代码:

   Public Sub CloseSource()
        Try
            Dim rc As TwRC
            If Not Equals(SelectedDataSource.Id, IntPtr.Zero) Then
                Dim guif As TwUserInterface = New TwUserInterface()

                ' tell the data source to disable its user interface
                rc = DS_Userif(appid, SelectedDataSource, TwDG.TwDG_Control, TwDAT.TwDAT_UserInterface, TwMSG.TwMSG_DisableDS, guif)

                ' tell the device manager to release the source
                rc = DSM_Ident(appid, IntPtr.Zero, TwDG.TwDG_Control, TwDAT.TwDAT_Identity, TwMSG.TwMSG_CloseDS, SelectedDataSource)

                ' set the pointer to zero
                SelectedDataSource.Id = IntPtr.Zero
            End If
        Catch ex As Exception

        End Try

    End Sub

2 个答案:

答案 0 :(得分:1)

<StructLayout(LayoutKind.Sequential, Pack:=2)> Public Class TW_USERINTERFACE
    Public ShowUI As Short ' TW_BOOL
    Public ModalUI As Short ' TW_BOOL
    Public hParent As UInt32 ' TW_HANDLE
End Class
不幸的是,bool是4bytes 所以我们使用short(0 = false 1 = true)

答案 1 :(得分:-1)

从您的消息和代码中我了解到,关闭扫描仪时会出现异常。我认为你有例外,因为扫描仪还没有准备好关闭。请确保扫描仪处于正确状态并可以关闭。

此致,亚历山大