The magcard reader is made by Magtek. They provide an ActiveX component as the sole means of communication with this RS-232 serial port device. It works great as long as it is being drag/dropped onto a windows form, as I had done it earlier stages.
However in the full-fledged application the C# class for doing the reading isn’t a Windows.Forms form. It is a class that is inherited from a ScannerGeneric.cs class. From the generic scanner class we have spawned ScannerBarcode.cs, ScannerRFID.cs, ScannerKeypad and so on. Reading from an RS-232 laser barcode scanner, or from an RFID scanner or from a MagTek card reader should all look transparent to the rest of the application for maximum versatility. This is where things go bad. I can't find a way to programmatically create an instance of the ActiveX Ocx and create an AxHost state that will work. Its this one point that is hanging me up.
Expand|Select|Wrap|Line Numbers
- A first chance exception of type 'System.Windows.Forms.AxHost.InvalidActiveXStateException' occurred in AxInterop.PPSwipe.dll
Expand|Select|Wrap|Line Numbers
- partial class scannerMagtek
- {
- private void InitializeComponent()
- {
- this.components = new System.ComponentModel.Container();
- System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(scannerMagtek));
- this.mySwiper = new AxPPSwipe.AxPortPowerSwipe();
- ((System.ComponentModel.ISupportInitialize)(this.mySwiper)).BeginInit();
- //
- // mySwiper
- //
- this.mySwiper.Enabled = true;
- this.mySwiper.Location = new System.Drawing.Point(19, 294);
- this.mySwiper.Name = "mySwiper";
- this.mySwiper.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("mySwiper.OcxState")));
- this.mySwiper.Size = new System.Drawing.Size(73, 50);
- this.mySwiper.TabIndex = 8;
- ((System.ComponentModel.ISupportInitialize)(this.mySwiper)).EndInit();
- }
- }
In trying to replicate the working code from the designer.cs file I have gone through numerous variations on the below. The problem is always in trying to set the OcxState property.
Expand|Select|Wrap|Line Numbers
- public override void ScannerOn()
- {
- try
- {
- ((System.ComponentModel.ISupportInitialize)(this.mySwiper)).BeginInit();
- this.mySwiper.Name = "mySwiper";
- //this.mySwiper.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("mySwiper.OcxState")));
- // ^^ From designer but there is no such resources in a non-form class, so what *can* I use to make this?
- mySwiper.OcxState = ((System.Windows.Forms.AxHost.State)(object)mySwiper.OcxState);// Result = null
- short PortNumber = 3;
- mySwiper.CommPort = PortNumber;
- // As soon as I try to assign a property such as the ComPort an exception is raised as follows:
- // A first chance exception of type 'System.Windows.Forms.AxHost.InvalidActiveXStateException'
- // occurred in AxInterop.PPSwipe.dll
- mySwiper.Settings = "9600,n,8,1";// Doesn't get this far because of exception
- ((System.ComponentModel.ISupportInitialize)(this.mySwiper)).EndInit();
- if (!mySwiper.PortOpen) mySwiper.PortOpen = true;
- if (!mySwiper.PortOpen) Console.WriteLine("Failed to open MagTek on COM" + PortNumber.ToString());
- }
- catch (Exception err)
- {
- Console.WriteLine( err.Message);
- }
- }
I've even tried to hand code a 'working' OcxState off the forms implimentation, but that failed to. I suspect it is dynamic in its creation so the buffer content changes with each instance. But that's a guess.
Has anyone else ever had a similar situation when using some kind of ActiveX component in such a manner?