Hosting Licensed ActiveX Controls in WPF
Recently we had a need to host some ActiveX controls inside a WPF window. Hosting controls in a .NET Windows Forms app is pretty straight-forward, basically register the control, add it to your toolbox in Visual Studio, then drag-n-drop the control on your form. Hosting the control in a WPF window is possible by the use of a WindowsFormsHost. Here is a walkthrough for hosting an ActiveX control in WPF from MSDN.
However we found that some of our controls gave licensing errors which crashed the application. These were our own custom controls that contained licensed third party controls. Everything seemed to work when the controls were called from a VB executable, but when hosting them in WPF, the controls crashed if the developer license wasn’t installed on the computer.
After reading up on Visual Basic Licensing, it seemed that our parent ActiveX control should pass the necessary license information to the third party control at runtime. I took a step back and created a Windows Forms test project. I created a custom test .ocx that contained a licensed third party control and placed it on a Windows Form. I ran my sample project on a test machine and SUCCESS, the control displayed fine – no licensing issues. A quick look at the designer code of my form shows some extra initialization that we were missing in WPF:
To properly initialize the control, you need to call .BeginInit(), set the OcxState, then call.EndInit(). The first and last part sound easy enough, but how do we set the OcxState. When the ActiveX control was added to the Windows Form, Visual Studio added the OcxState as a resource to the form. So open Form1.res in an xml editor, copy the <data>…</data> element that references your <controlName>.OcxState, and add it to your WPF Resources.resx file. In the excerpt below, originally data name=”axUC_Bryan1.OcxState”, but I changed the name to _myCustomControl to match the name of my class member (and because the designer complained it wasn’t a valid identifier).
Copying the resource above is necessary because the AxHost.OcxState class contains the necessary licensing information that gets passed to your control. Even if your control doesn’t require a license, if it contains a licensed third party control, it will need to be initialized properly. So once you have added the appropriate serialized AxHost.OcxState resources to your WPF project, you can create and initialize your control like so: