473,795 Members | 3,157 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

tlhintoq
3,525 Recognized Expert Specialist
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 programmaticall y 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 programmaticall y 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.ActiveXS tate object referenced in the PPSwipe.OcxStat e property, and I can find no way to programmaticall y 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 2659

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

Similar topics

8
4569
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 tried all main combinations of Instancing (Multiuse/Singleuse) and of Threading models (per object/pool). Here's what I found: MultiUse - Thread per Object
4
1306
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, the corresponding PDF file should be displayed. The catch is that the PDF viewer needs to be embedded *inside* my WinForms app, as opposed to being externally launched. The user needs to be able to scroll through the PDF and optionally print it....
5
1853
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 ActiveX control isn't as straight-forward as a CWnd-derived object. It also means that I have to split my code into two projects. Going the other way (using MFC in WinForms) is only possible by exposing MFC objects as ActiveX controls, which...
20
3980
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 CInteropCall encapsulating a call to a visual basic ActiveX DLL function named FillAlloc_ArrayStruct_Double(), which is allocating a big struct array an fills it using a inner loop
1
3150
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 and registered for COM Interop. This I know for sure has no memory leaks etc. as a simple test page can load, use and then dispose of the object without a problem.
2
6903
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 (which is the UI used to adjust the actions taken by, and the schedule of the service), then a privileged user thread should be used in the UI - no service required. But... "A windows service enables the creation of long-running executable
5
3830
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 untrue or you would like to add - please comment - thanks..... Rich Client PROS 1. User experience (* indicates feasible on web with AJAX) - a. Single, unified application experience b. Windows/Office-like look and feel - i.Tabs - drag and drop...
8
1696
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 timeout period elapsed prior to completion of the operation or the server is not responding. I have also set checked the time out of the server to unlimited. I am using the SQLDataReader in my applciation at quite a few palces and make it a point...
2
1928
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 Badge reader when any one swipe the badge, Badge reader has to read the data and I needs to store in a DB. Later my web application gets the data from DB. Hope there could be some API which communicates to Badge reader. Please help me in this...
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10437
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10001
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.