473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

tlhintoq
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
  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
0 2626

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

Similar topics

8
by: AnalogKid | last post by:
Short question: What's the difference between SingleUse and MultiUse ? Long question: I've been writing some sample code to see how different Instancing values and threading models work. I...
4
by: Jules Winfield | last post by:
Good Day, Gentlemen: I've been tasked with creating a screen that allows users to double-click items from list of filenames. Each filename in the list corresponds to a PDF file. On double-click,...
5
by: Dan Smith | last post by:
Right now the only way to use a WinForms control in MFC is to enable COM Interop for the control and use it in MFC as you would any ActiveX control. While this works, COM Interop is a pain, and an...
20
by: Cybertof | last post by:
Hello, Is there a good way to call a big time-consumming function from an ActiveX DLL (interoped) through a thread without blocking the main UI ? Here are the details : I have a class...
1
by: Simon | last post by:
Hi all, I recently posted about an issue I was having which no-one could help with, but I have researched some more and have discovered something very strange... I have a C# dll in the GAC...
2
by: deko | last post by:
When to use a privileged user thread rather than a windows service? That's the question raised in a previous post . It was suggested that if the service needs to interact with a WinForms app...
5
by: brian.wilson4 | last post by:
Our group is currently comparing winforms vs webforms.....app is Corp LAN based - we have control of desktops.....Below is pros and cons list we have come up with - if anything strikes you as...
8
by: Saurabh | last post by:
Hi, I am getting the below mentioned error when I try to update a table Exception: Unable to update Person records Exception : System.Data.SqlClient.SqlException: Timeout expired. The...
2
by: =?Utf-8?B?VW1lc2huYXRo?= | last post by:
Hi, Can any one suggest how can I read Badge information into my web application? I am very new into this. I am expecting entire solution. My requirement is, I have a system which will connect to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.