Making BarTender an optional component

I need to add bar code printing to an existing application, and the component we chose to use is BarTender (9.3 SR3). The problem is that BarTender is only installed on a few of the workstations running the application. If we add a reference to the Seagull.BarTender.Print assembly in the GAC, the majority of our installed instances won’t run. So what’s the solution?

1. Copy the following files from the GAC into the project folder (This assumes you have BarTender installed on the development machine). See here for instructions on copying from the GAC. I prefer the last option.  There are quite a few BarTender dlls, but these appear to be the necessary ones.

Seagull.BarTender.Print.dll (GAC_32 folder)
Seagull.Interop.dll (GAC_32 folder)
Interop.BarTender.dll (GAC_MSIL folder)

2.  Add the files to the project and set their properties:

Build Action = None
Copy To Output Directory = Copy always

3. Add a reference to the Seagull.BarTender.Print.dll file that was copied to the project. Use the Browse tab of the Add Reference dialog, not the .NET tab! The Copy Local property can be left False. The project should compile now even if it uses BarTender code.

4. At this point, if the project targets Any CPU you will get an error when building:

Could not load file or assembly ‘Seagull.BarTender.Print, Version=9.40.3.1, Culture=neutral, PublicKeyToken=109ff779a1b4cbc7′ or one of its dependencies. An attempt was made to load a program with an incorrect format.

The project has to target x86. Sorry.

5. Wrap any calls to the BarTender print engine in Try/Catch blocks. If BarTender is not installed, any attempt to actually use the print engine will throw an error. Here is a very simple example that just starts up the print engine and then stops it again:

Dim engine As Seagull.BarTender.Print.Engine
Try
  engine = New Seagull.BarTender.Print.Engine(True)
Catch ex As Exception
  MessageBox.Show(ex.Message)
Finally
  If Not engine Is Nothing Then
    engine.Stop(Seagull.BarTender.Print.SaveOptions.DoNotSaveChanges)
  End If
End Try

6.  Uninstall BarTender and make sure everything degrades nicely.

Note: If the Interop dlls are not included, the error when creating a new Engine will be a FileNotFoundException for one of the Interop files. Also, If you don’t include both Interop files, and you run the code above, you will also get an error when the application is closed:

System.IO.FileNotFoundException was unhandled
Message=”Could not load file or assembly ‘Interop.BarTender, Version=9.40.3.1, Culture=neutral, PublicKeyToken=109ff779a1b4cbc7′ or one of its dependencies. The system cannot find the file specified.”
Source=”Seagull.BarTender.Print”
StackTrace:
at Seagull.BarTender.Print.Engine.Dispose(Boolean bDisposeManaged)
at Seagull.BarTender.Print.Engine.Finalize()

The nasty thing about this is that you can’t catch the error in the application, so Windows does it:

XXXX has stopped working. Windows is checking for a solution to the problem…

This entry was posted in BarTender, VB.net, VB.net 3.5, WinForms. Bookmark the permalink.

Comments are closed.