locked
WPF w/ActiveX using Interop RRS feed

  • Question

  • Hi,

     

    I am trying to use an ActiveX control within a WPF application. Specifically, I need to both show the control and access its non-default COM interface. I’m able to do both, just not simultaneously. I’m using VS2008.

     

    Additional Info

    A separate Windows Forms Control Library project is created for the ActiveX control and a reference is added for the COM control. This results in three reference nodes being added to the project in the Solution Explorer: one with the name of the ActiveX control, one with the name of the non-default interface, and one for “stdole”. When the control is dropped from the Toolbox onto the user control, another reference node is added to the project with the name “Ax” followed by the name of the ActiveX control. When compiled, four DLL’s are created in the project sub-directory: one with the project name, one AxInterop.ActiveXControlName, and two Interop’s—one with the ActiveX’s name and one with the non-default interface name.

     

    Using the Object Browser the Interop.ActiveXName can be expanded to reveal the non-default interface and the interface can be access programmatically also. (Expanding either the project control library or the AxInterop entries in the object browser do not show the non-default interface so my assumption is I must add/use the Interop’s in the WPF application.) However when using these DLL’s I’m unable to display the ActiveX control. The code that works otherwise is:

     

    activeXControl.activeXControlClass axControl = new activeXControl.activeXControlClass ();

    WindowsFormsHost host = new WindowsFormsHost();

    //Next line results in ERROR: “Cannot implicitly convert type…to System.Windows.Forms.Control”

    host.Child = axControl;

    gridMain.Children.Add(host);

     

    I’ve tried all sorts of casts without success. Is there another way to get the visual onto the grid while still using the Interop references? Thanks for any help.

     

    Kurt

    Monday, January 7, 2008 9:04 PM

All replies

  • I think that you can follow the following steps to use an ActiveX control.

     

    1. Right click on the Visual Studio Toolbox pane.

    2. Click Choose Items menu on the popuped context menu.

    3. On the Choose Toolbox Items dialog window, switch to COM components tab.

    4. Choose the ActiveX control you want to use and then click OK button.

    5. You will see the ActiveX control is appeared on the Toolbox pane, now you can drag the ActiveX control to your Windows Forms designer.

    6. You can open designer.cs file to see the auto generated code.

     

    Best Regards,

    Wei Zhou

    Wednesday, January 9, 2008 5:59 AM
  • Thanks for the info, let me rephrase:

     

    The ActiveX control is wrapped using a Windows Forms Control Library. Compiling the Control Library creates two DLL’s entitled “Interop.(activex name).dll” and “Interop.(non-default interface name).dll”.

     

    These DLL’s are then referenced in the WPF Application. Instances of the control can be created and properties/methods called, but attempts to show the control visually fail using the techniques I’m aware of: using ElementHost and WindowsFormsHost.

     

    I need to somehow cast the control instance to be compatible with the Child property of one of these hosts (no luck) or find another method to display. Thanks again.

    Wednesday, January 9, 2008 3:17 PM
  • The wrapped ActiveX control is in the AxInterop.*** assembly. The class name of the ActiveX control is Ax***. The class of the ActiveX control is derived System.Windows.Forms.AxHost. You can search your ActiveX control dlls to get the class of the ActiveX control.

     

    Best Regards,

    Wei Zhou

    Thursday, January 10, 2008 7:11 AM
  • Thanks again.

     

    The ActiveX controls to be hosted have multiple COM interfaces defined. What's interesting is if I use the AxInterop and use the "Go To Definition" function from the IDE declaration, the gen'ed code only includes a few methods/properties available in the ActiveX interfaces. If I use the Interop's, all methods/properties are present. The ActiveX controls are Delphi Active Forms by the way.

     

    Thursday, January 10, 2008 10:22 PM