Microsoft KB Archive/326651

= How to dynamically add ActiveX controls that require run-time licenses in Visual Basic .NET or in Visual Basic 2005 =

Article ID: 326651

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q326651



For a Microsoft Visual C# .NET version of this article, see 326652.



For a Microsoft Visual Basic 6.0 version of this article, see 190670.

IN THIS TASK
SUMMARY
 * Create a New Visual Basic .NET Project
 * Create ActiveX Interop Assemblies
 * Dynamically Create the Control
 * Add the License to the Control
 * Add the Control to the Form

Troubleshooting REFERENCES



SUMMARY
This step-by-step article describes how to manually add a run-time license to an ActiveX control that is dynamically created in Visual Basic .NET or in Visual Basic 2005. In Visual Basic version 6, you can manually add licenses to the license collection by using the Licenses.Add method. For more information about what License.Add does in Visual Basic 6, see the &quot;References&quot; section of this article.

In Visual Basic .NET or in Visual Basic 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.

back to the top

Create a New Visual Basic .NET or Visual Basic 2005 Project

 * 1) Click Start, point to All Programs, point to Microsoft Visual Studio .NET or Microsoft Visual Studio 2005, and then click Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
 * 2) On the File menu, point to New, and then click Project.
 * 3) In the New Project dialog box, click Visual Basic Projects under Project Types, click Windows Application under Templates, and then click OK.

Note In Visual Studio 2005, click Visual Basic under Project Types.

back to the top

Create ActiveX Interop Assemblies
 Click Start, point to All Programs, point to Microsoft Visual Studio .NET or Microsoft Visual Studio 2005, point to Visual Studio .NET Tools or Visual Studio 2005 Tools, and then click Visual Studio .NET Command Prompt or Visual Studio 2005 Command Prompt. To create the two interop assemblies that you must have, type the following command:

aximp /out:C:\temp\AxMyProject.dll &quot;C:\Windows\System32\MyProject.ocx&quot;

AxMyProject.dll is for AxHost Interop. MyProject.dll is for COM Interop.

back to the top

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.</li> Add a Button control to the form.</li> Double-click the button to find the code for the Button Click Event method.</li>  In the Button Click Event method, paste the following code: Dim myControl As New AxMyProject.AxMyLicensedControl This creates a new instance of the control through AxHost.

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. </li></ol>

back to the top

Add the License to the Control
 Determine and note the run-time license key for the control. For MyLicensedControl, the license key is &quot;gnvlslnjskvlmlgnnimh&quot;.</li>  In the Button Click Event method, paste the following code below Dim myControl As New AxMyProject.AxMyLicensedControl: Dim f As System.Reflection.FieldInfo f = GetType(AxHost).GetField(&quot;licenseKey&quot;, _            Reflection.BindingFlags.NonPublic _             Or Reflection.BindingFlags.Instance) f.SetValue(myControl, &quot;gnvlslnjskvlmlgnnimh&quot;) 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. </li></ol>

back to the top

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: Controls.Add(myControl) myControl.Show Paste this code in the Button Click Event method, after &quot;f.SetValue(myControl, &quot;gnvlslnjskvlmlgnnimh&quot;)&quot;, and before the &quot;End Sub&quot; line.

NOTE: If the license is not added to the control and the license is required, Controls.Add will fail. Controls.Add runs the code that contains that license validation. When you try and run Controls.Add, 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 error message:

An unhandled exception has occurred in your application. You do not have a license to use this ActiveX control.

</li> Press F5 to run the project.</li> Click the button to add your ActiveX Control to the form.</li></ol>

back to the top

Troubleshooting
IMPORTANT: Note the following about this operation: <ul> 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.</li> Make sure that you have the correct license key for your control. Determining the correct license key is specific to the control: For additional information about determining the key, click the article number below to view the article in the Microsoft Knowledge Base:

151771 LICREQST.EXE Requesting a License Key from an Object

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.</li></ul>

back to the top

<div class="references_section">