In Visual C# .NET or in Visual C# 2005, the licenses collection no longer exists. You must embed the license information in the control.
This article assumes that the control that requires the run-time license is in MyProject.OCX and is named MyLicensedControl. MyProject.OCX is located in C:\Windows\System32.
Create a new Visual C# .NET or Visual C# 2005 project
- Click Start, point to All Programs, point to Microsoft Visual Studio .NET, and then click Microsoft Visual Studio .NET.
Note In Visual Studio 2005, Microsoft Visual Studio .NET is changed to Microsoft Visual Studio 2005.
- On the File menu, point to New, and then click Project.
- In the New Project dialog box, click Visual C# Projects under Project Types, click Windows Application under Templates, and then click OK.
Note In Visual Studio 2005, Visual C# Projects is changed to Visual C#.
Create ActiveX Interop assemblies
- Click Start, point to All Programs, point to Microsoft Visual Studio .NET, point to Visual Studio .NET Tools, and then click Visual Studio .NET Command Prompt.
Note In Visual Studio 2005, Microsoft Visual Studio .NET is changed to Microsoft Visual Studio 2005. Visual Studio .NET Tools is changed to Visual Studio 2005 Tools. Visual Studio .NET Command Prompt is changed to Visual Studio2005 Command Prompt.
- To create the two interop assemblies that you must have, type the following command:aximp /out:C:\temp\AxMyProject.dll "C:\Windows\System32\MyProject.ocx"AxMyProject.dll is for AxHost Interop. MyProject.dll is for COM Interop.
Dynamically create the control
- On the Project menu, click Add Reference.
- Click Browse, and then select the ActiveX Control Interop assemblies that you just created (they should be in C:\Temp).
- Add both AxMyProject.dll and MyProject.dll; adding these files copies them locally.
- Add a Button control to the form.
- Double-click the button to find the code for the Button Click Event method.
- In the Button Click Event method, paste the following code:This creates a new instance of the control through AxHost.
AxMyProject.AxMyLicensedControl myControl = new AxMyProject.AxMyLicensedControl();
Note Do not create a new instance of the control through COM (New MyProject.MyLicensedControl), because this will not work on the form, and you cannot easily add the license.
Add the license to the control
- Determine and note the run-time license key for the control. For MyLicensedControl, the license key is "gnvlslnjskvlmlgnnimh".
- In the Button Click Event method, paste the following code below "AxTestActiveXComponent.AxUserControl1 myControl = new AxTestActiveXComponent.AxUserControl1();"This code inserts the run-time license into the control. It creates an instance of FieldInfo, which is part of the property bag OCXState. This property bag is full of specific ActiveX control information. In this case, you are obtaining the field licenseKey and setting it for myControl. You must do this for each instance of any ActiveX control that requires a run-time license.
System.Reflection.FieldInfo f =
Add the control to the form
- Add the control to the form's Controls collection, and then show the control. To do this, you can use the following code:Paste this code in the Button Click Event method, after "f.SetValue(myControl, "gnvlslnjskvlmlgnnimh");", and before the End Sub.
Note If the license is not added to the control and the license is required, myControl.Show fails. MyControl.Show runs the code that contains that license validation. When you try and run myControl.Show, you see the following error in debug mode:An unhandled exception of type 'System.ComponentModel.LicenseException' occurred in system.windows.forms.dll Additional information: You do not have a license to use this ActiveX control.Or, at run time, you may see the following similar message:An unhandled exception has occurred in your application.
You do not have a license to use this ActiveX control.
- Press F5 to run the project.
- Click the button to add your ActiveX control to the form.
TroubleshootingImportant Note the following about this operation:
- You must add the license to every instance of every ActiveX control that requires a run-time license. For example, if you create a second instance of MyLicensedControl, you have to add the license to that instance also.
- Make sure that you have the correct license key for your control. Determining the correct license key is specific to the control: For more information about determining the key, click the following article number to view the article in the Microsoft Knowledge Base:151771 The Licreqst.exe sample demonstrates how to request the License key of an object in Visual C++Alternatively, you can use Licenses.Add for the control in Visual Basic 6 on a computer that has the license in the registry. Licenses.Add returns the run-time license that it added as a string; you can then use Debug.Print to obtain the license key.
Article ID: 326652 - Last Review: Jul 11, 2008 - Revision: 1