Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to Late Bind an ActiveX DLL

Status
Not open for further replies.

txmed

Programmer
Mar 23, 2006
65
US
In one project that I am working on, I need to use an ActiveX object that has been built for magnetic card readers. My problem is that some workstations have have this reader installed, and some may not. So I can early reference this object in my code.

I've been trying to find a way to late bind to this DLL, if the computer has it installed. Using Reflection, I've been able to do this. However, when I try to call a method, it gives me a InvalidActiveXStateException error. Can someone please tell me what I am doing wrong?


Code:
Option Strict Off

Public Sub BindDLL()

  Dim pobjAssembly As System.Reflection.Assembly
  Dim pobjAssemblyName As System.Reflection.AssemblyName
  Dim pobjType As Type
  Dim pobjClass As Object = Nothing

  pobjAssemblyName = System.Reflection.AssemblyName.GetAssemblyName("AxInterop.ctlUSBHID.dll")
  pobjAssembly = System.Reflection.Assembly.Load(pobjAssemblyName)

  pobjType = pobjAssembly.GetType("AxctlUSBHID.AxUSBHID")

  pobjClass = Activator.CreateInstance(pobjType)
  [b]pobjClass.PortOpen = True[/b]  [COLOR=red]' <-- Error happens here.[/color]

End Sub

This is the error message that I'm getting:
Exception of type 'System.Windows.Forms.AxHost+InvalidActiveXStateException' was thrown.

 
I apologize. In my first paragraph I meant to say "So I can not early reference this object in my code."

Sorry for the typo.

(txmed)
 
Hello all. I figured out my problem. If anyone is in need in this type of solution, please let me know.

(txmed)
 
Hi txmed,
I'm interested in your solution, since I'm getting the same problem with another OCX.

Thank you in advance,
k0a1a
 
k0a1a,

Sorry for the late reply. Here are the steps that helped solved my problem.

1. First, load the 3rd party DLLs or OCXs as references to your application. VS should copy the files to your application directory.

2. Second, when my app loads, I used the FileSystemObject to see if the appropriate DLLs are found on the computer. If they were, then I set a boolean variable to True, telling me that they were installed. In this case, I named my variable IsMakTekReaderInstalled

3. Later in my app, when I need to use the 3rd party DLLs, I first check to see if the boolean variable I created is set to True. If it is, then I call my own sub procedure that does nothing but adds the 3rd party files as a control reference to my project.

Here's is my code on how I do this:

Code:
    Private Sub LoadMagTekReader()
        
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' Code Segment: If the workstation has the IsMakTekReaderInstalled setting variable is
        ' set to True, then turn the MagTek reader on.
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        If IsMakTekReaderInstalled = True Then

            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            ' Code Segment: This code instatiates a new axHost object, and then adds the object
            ' to this form's collection of controls.
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            Dim pctlMagTekControl As New AxctlUSBHID.AxUSBHID
            Controls.Add(pctlMagTekControl)

            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            ' Code Segment: This code opens the MagTek's special port, and then adds an event
            ' handler to the object's CardDataChanged event.
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            pctlMagTekControl.PortOpen = True
            AddHandler pctlMagTekControl.CardDataChanged, AddressOf MagTek_CardDataChanged

        End If

    End Sub

Notice that I have to manually add an event handler to the control. This is because if I build the event at design-time, but the DLLs or OCXs happen to be missing at run-time, the app will bomb out.

The great thing about this is that if the files are not installed, then this part of my app never gets executed, eliminating the possibility of having the app crash.

Please note that for use-of-reading, I took out the proper error handling. Be sure to use your own where appropriate.

If you need further clarification on this, please let me know.

(txmed)
 
Hello txmed,
thanks a lot for your detailed description!

My problem was different however. I had to instanciate my OCX object inside InitializeComponent() only, then it worked.

Your explanation helped me much to better understand the matter, however; thanks again.

Koala
 
You're welcome. Again, sorry for the late reply.

(txmed)
 
Actually I found the control. Is there any vb tutorial on this control? I am trying to read data from the MSR via vb.net. Any help would greatly be appreciated!
 
lawlegacy,

The USBHID control is from the manufacturers of the card reader. I went to their site and downloaded a sample project (written in VB6.0). Once I reviewed the code, everything went pretty smooth.

At this moment, I do not remember the name of the site. When I do, I will be sure to let you know.

(txmed)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top