470,843 Members | 1,381 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,843 developers. It's quick & easy.

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

3,525 Expert 2GB
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
  16.                 mySwiper.Settings = "9600,n,8,1";// Doesn't get this far because of exception
  17.                 ((System.ComponentModel.ISupportInitialize)(this.mySwiper)).EndInit();
  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.         }
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
0 2416

Post your reply

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

Similar topics

8 posts views Thread by AnalogKid | last post: by
4 posts views Thread by Jules Winfield | last post: by
20 posts views Thread by Cybertof | last post: by
1 post views Thread by Simon | last post: by
5 posts views Thread by brian.wilson4 | last post: by
8 posts views Thread by Saurabh | last post: by
2 posts views Thread by =?Utf-8?B?VW1lc2huYXRo?= | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.