Interop ActiveX Scanner Control RRS feed

  • Question

  • I am using WPF and trying to get an activex control to work in a test application. THe activex control is for a scanner made by metrologic..... I keep getting the following error when i try and communicate with the scanner, such as opening it. It instantiates but will not allow communication. Anyway I get the following:

        Exception of type 'System.Windows.Forms.AxHost+InvalidActiveXStateException' was thrown.

    The actual state of the OCX is NOTHING....

    Here is the code: THe code fails on the OPEN method

    Public WithEvents OPOSScanner As New AxOposScanner_1_8_Lib.AxOPOSScanner

    Public Const OposSuccess As Integer = 0

    Public Sub StartScanner()

    ' Attempt to open the device

    If OPOSScanner.Open("test") <> OposSuccess Then

        MsgBox("Failed Open")

        Exit Sub

    End If

    ' Once the device is enabled, allow data to enter

    OPOSScanner.DecodeData = True

    OPOSScanner.DataEventEnabled = True

    ' Attempt to claim this scanner for my use

    If OPOSScanner.ClaimDevice(1000) <> OposSuccess Then

    MsgBox("Failed Claim")

    Exit Sub

    End If

    ' Enable the scanner

    OPOSScanner.DeviceEnabled = True

    If OPOSScanner.ResultCode <> OposSuccess Then

    MsgBox("Failed Device Enable")

    End If

    End Sub

    • Edited by script Friday, September 12, 2008 2:13 AM
    Friday, September 12, 2008 2:12 AM


  • ActiveX controls that are wrapped by AxHost must be put on a form to be properly initialized.  Doesn't look like you're using a form.  If the control doesn't have visible user interface, or you don't care about it, you shouldn't use Aximp.exe but Tlbimp.exe to generate an interop assembly.
    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Wednesday, September 17, 2008 11:50 AM
    Friday, September 12, 2008 2:40 AM