By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,221 Members | 1,075 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,221 IT Pros & Developers. It's quick & easy.

C#, WinForms, Magcard reader ActiveX Ocx, instance creation issue

tlhintoq
Expert 2.5K+
P: 3,525
My project involves reading magstripe cards, capturing an ID number, then spinning off to do some other things including taking a photograph. 99% of this is done and good. All early phases of work went well... Until the final assembly and "beefing up" of the project.

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
  1. A first chance exception of type 'System.Windows.Forms.AxHost.InvalidActiveXStateException' occurred in AxInterop.PPSwipe.dll
In the working forms tests there is code that is created by Visual Studio for the OCX that was dropped into the form. This code appears in the designer.cs code along these lines.

Expand|Select|Wrap|Line Numbers
  1. partial class scannerMagtek
  2. {
  3. private void InitializeComponent()
  4. {
  5. this.components = new System.ComponentModel.Container();
  6. System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(scannerMagtek));
  7. this.mySwiper = new AxPPSwipe.AxPortPowerSwipe();
  8. ((System.ComponentModel.ISupportInitialize)(this.mySwiper)).BeginInit();
  9. // 
  10. // mySwiper
  11. // 
  12. this.mySwiper.Enabled = true;
  13. this.mySwiper.Location = new System.Drawing.Point(19, 294);
  14. this.mySwiper.Name = "mySwiper";
  15. this.mySwiper.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("mySwiper.OcxState")));
  16. this.mySwiper.Size = new System.Drawing.Size(73, 50);
  17. this.mySwiper.TabIndex = 8;
  18. ((System.ComponentModel.ISupportInitialize)(this.mySwiper)).EndInit();
  19. }
  20. }
With this in the designer I have no problems assigning comport or settings values and opening the port and reading a card. But as I said, I need to be able to add it programmatically into an inheritted C# class that isnít a form.

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
  1.         public override void ScannerOn()
  2.         {
  3.             try
  4.             {
  5.                 ((System.ComponentModel.ISupportInitialize)(this.mySwiper)).BeginInit();
  6.                 this.mySwiper.Name = "mySwiper";
  7.                 //this.mySwiper.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("mySwiper.OcxState")));
  8.                 // ^^ From designer but there is no such resources in a non-form class, so what *can* I use to make this?
  9.                 mySwiper.OcxState = ((System.Windows.Forms.AxHost.State)(object)mySwiper.OcxState);// Result = null
  10.                 short PortNumber = 3;
  11.                 mySwiper.CommPort = PortNumber;
  12.                 // As soon as I try to assign a property such as the ComPort an exception is raised as follows:
  13.                 // A first chance exception of type 'System.Windows.Forms.AxHost.InvalidActiveXStateException' 
  14.                 // occurred in AxInterop.PPSwipe.dll
  15.  
  16.                 mySwiper.Settings = "9600,n,8,1";// Doesn't get this far because of exception
  17.                 ((System.ComponentModel.ISupportInitialize)(this.mySwiper)).EndInit();
  18.  
  19.                 if (!mySwiper.PortOpen) mySwiper.PortOpen = true;
  20.                 if (!mySwiper.PortOpen) Console.WriteLine("Failed to open MagTek on COM" + PortNumber.ToString());
  21.             }
  22.             catch (Exception err)
  23.             {
  24.                 Console.WriteLine( err.Message);
  25.             }
  26.         }
  27.  
The stumbling block seems to be that when the Ocx is drag/dropped onto a form something happens to properly create the AxHost.ActiveXState object referenced in the PPSwipe.OcxState property, and I can find no way to programmatically create this process or object in a non-forms C# class.

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?
Sep 22 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.