From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

VDM8.0: InvalidActiveXStateException thrown by AxCWIMAQVision.MatchPattern

I am writing a C# application using VS.Net 2003 to analyze images acquired from an IEEE 1394 camera. I am able to successfully build and run samples such as "PatternMatching" that use this same MatchPattern function.  When I run my application, though, I receive the following runtime error:
An unhandled exception of type 'InvalidActiveXStateException' occurred in nationalinstruments.axcwimaqcontrolslib.interop.dll
I have tried other functions that use CWIMAQControls.AxCWIMAQVision and CWIMAQControls.AxCWMachineVision, and I get this same error. That makes me belive there is a larger problem with my code than just my call to the MatchPattern function, but that portion of the code is below (where vision is an initialized AxCWIMAQVision object:
vision.MatchPattern(Image, Template, MatchPatternOptions, PatternMatchReport, SearchRect);
Does anyone have experience with this error? My next option is to start from one of the examples, which would hopefully result in a working program.

My full project is attached (as far as I can tell :Smiley Tongue).

0 Kudos
Message 1 of 15
(6,156 Views)
Hello Brad,

One of the Applications Engineers here informed me that she was already working with you.  I'm happy to hear that you were able to get your program working by starting from a VB .NET pattern matching example and using VB .NET instead of C#.

Allen H.
0 Kudos
Message 2 of 15
(6,152 Views)

hi..

the 'InvalidActiveXStateException' on the title of this thread caught my attention. i am using VS .NET 2003, to develop a simple application :capture image and perform color matching.however, in my LearnColor function call, im getting an unhandled exception , 'InvalidActiveXStateException' occurred in nationalinstruments.axcwimaqcontrolslib.interop.dll..i have no idea wat this means..because i am not using any windows form controls in my application.i m not sure what could be causing this...

i didnt create the application from scratch,i kinda started working from the examples..i know for sure that my grab function is working..so wat could be causing this?

please help...Smiley Sad

thanks

 
0 Kudos
Message 3 of 15
(5,757 Views)
Hello rebecs,
 
Have you tried running the Color Pattern Matching example, found under: C:\Program Files\National Instruments\Vision\Examples\MSVB.NET\2. Functions\, under most installations.  I would be interested to know if this works, without modifications made to it.
 
Regards,
Jasper S
0 Kudos
Message 4 of 15
(5,741 Views)

hi jasper,

yup,the example works fine.i tried it..actually,at first i tried to work from using this example..but then i switched to color matching instead of color pattern matching..and it worked fine when i developed a windows application. wat im trying to do now is call this application(as a dll) from a console application. the program halts at :

IMAQVision.LearnColor(myImage, ColInfo, ViewRect)  and i get the message :  An unhandled exception of type 'InvalidActiveXStateException' occurred in nationalinstruments.axcwimaqcontrolslib.interop.dll

all i can guess is there's some NI Vision control that cant be recognised or handled by a console.but i can figure out wat could it be..Smiley Sad
i will be so grateful for any help ..thank you

0 Kudos
Message 5 of 15
(5,728 Views)
and,while checking out the properties of the image(thru the watch window), i found these 2 errors:
 
BitDepth<error: an exception of type: {System.Runtime.InteropServices.COMException} occurred>
 IsClear <error: an exception of type: {System.Runtime.InteropServices.COMException} occurred> 
 
could this be causing the error? i've been fine combing my code,looking for any tiny details that could be the cause..but so far i have been unsuccessfull..Smiley Sad
i have also been searching about the InvalidActiveXStateException error, but most of the explanations are kinda general..so im still confused..Smiley Sad
0 Kudos
Message 6 of 15
(5,726 Views)
well,something just occurred to me..the AxCWIMAQVision is a control,huh?maybe thats causing the errorSmiley Indifferent
anyway,when i interchange it with CWIMAQVision, i get this error at LearnColor:An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll; Additional information: Catastrophic failure...Smiley Sad
so,anyone has any ideas?im nt sure about the difference between those two controls..maybe if someone can tell me,i can work it out from there..Smiley Wink
thanks..
0 Kudos
Message 7 of 15
(5,711 Views)
Hi rebecs,

I'm not on the IMAQ or Vision teams, but I am on some of our R&D teams that work with .NET and COM quite a bit so perhaps I can provide some insight from the .NET perspective. Hopefully this might help the IMAQ and Vision engineers get a better idea of how to help you. 

For starters, I believe the IMAQ and Vision groups don't have native .NET assemblies and thus you are required to use their ActiveX controls in .NET.  Here is where some of the confusion starts when dealing with COM and .NET together. 

Because Windows Forms can host only Windows Forms controls, Visual Studio automatically creates Windows Forms wrappers for ActiveX controls so you can continue to use them in Visual Studio .NET projects.  For example, when you place a CWIMAQVision ActiveX control on a Windows Form, Visual Studio uses the COM type library to automatically create two DLLs for the entire OCX file (file that can contain one or more several ActiveX controls). One DLL contains a Common Language Runtime (CLR) proxy for the OCX, and the second contains a Windows Form proxy for the OCX.

The CLR proxy DLL is known as the Runtime Callable Wrapper (RCW) and contains a .NET version of the COM type library. It defines all objects and interfaces found in the original COM type library and the classes that implement the interfaces. Visual Studio .NET names this DLL Interop.ID.dll, where ID is the name of the type library. In your case, the name is Interop.CWIMAQControlsLib.dll.  The Interop prefix denotes that the RCW incorporates services from the Interop layer, which controls marshalling and interoperability between COM and the .NET Framework.

The Windows Form proxy DLL provides a .NET control that calls the RCW.  This DLL defines the properties, methods, and events you can set, call, and handle programmatically. Visual Studio names this DLL AxInterop.ID.dll, where ID is the name of the type library.  In your case, the name is AxInterop.CWIMAQControlsLib.dll. The Ax prefix denotes that the wrapper classes inherit from the AxHost class to expose the ActiveX control as a Windows Forms control.

Note. The References section in the Solution Explorer will show names like AxCWIMAQControlsLib and CWIMAQControlsLib.


Continued on the next post.....
Jonathan N.
National Instruments
Message 8 of 15
(5,703 Views)

Continued from previous post....

Now, I have called .NET assemblies that contained COM components in them from a Console Application without problems before so I know it can be done. One thing you have to be careful up is making sure what type of apartment model those COM components are in. Typically, COM runs under the STA model (single threaded apartment) so you would need to put the statement [STATHREAD] right before your Main method is called. Of course, usually you will get an error message saying that the control can't be instantiated because the current thread isn't in a single-threaded apartment.

Now as far as the InvalidStateException, as the name mentions, that exception is thrown when the ActiveX control is referenced while in an invalid state.  Usually this comes from calling the public properties and methods of the ActiveX control before the referenced ActiveX control has been instantiated and initialized.

Based on that, my question to you might be how you are exactly calling the Windows Application from the Console Application? Did you include the
System.Windows.Forms namespace and then using the Application class to instantiate a new form (thus this would instantiate the CWIMAQVision control)?

Continued on next post....

Message Edited by Jonathan N on 05-29-2007 09:40 AM

Jonathan N.
National Instruments
Message 9 of 15
(5,698 Views)

Continued from previous post...

So you would say something like:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace ConsoleApplication1
{
   class Program
  
{
        [
STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.DoEvents();
           
Application.Run(new myWindowsApp.Form1());
        }
   }
}

Or does your program work when calling several methods of your Windows App but then stop when it gets to a method that uses that LearnColor method?

Anyway, just some thoughts. Also, some references I use for .NET/COM interop are Understanding COM Interop, Understanding Classic COM Interoperability With .NET Applications, and MSDN's main Interoperability page.

Hope this helps! Any of your Vision/IMAQ specific questions, I leave to the Vision/IMAQ engineers to answer.

Best Regards,

Jonathan N.
National Instruments
Message 10 of 15
(5,696 Views)