How to create an instance of an ActiveX control in C# at runtime?
August 24, 2006 3:41 PM
Subscribe
In C#, how can I
at runtime create an instance of an ActiveX control and then use its methods, properties and events?
I have about 30 ActiveX controls which I'm writing an automated testing environment for. My program will read in an XML file which specifies, for example, that creating an instance of MyActiveXControl and calling the Foo() method will cause a Bar event to be fired. To do this, I need to be able to dynamically create an instance of MyActiveXControl* and call an arbitrary method on it and assign an event handler to an arbitrary event. How?
The difficulty in creating an instance of the control dynamically is that my C# program is managed code whereas the ActiveX control is unmanaged. There isn't an easy equivalent of VB's CreateObject() (that I know of).
* all of the ActiveX controls will be registered on the system, and I'll know where on the hard disk the .ocx is, if that helps.
posted by matthewr to computers & internet (22 comments total)
1 user marked this as a favorite
MyActiveXControl*"Canonical answer: Factory Pattern.
"and call an arbitrary method on it and assign an event handler to an arbitrary event."
Canonical answer: Command Pattern.
(And possibly Interpreter pattern, since you're already dealing with XML).
posted by orthogonality at 3:51 PM on August 24, 2006