This step-by-step article describes how to manually add a run-time license to an ActiveX control that is dynamically created in Microsoft Visual C++ .NET or in Microsoft Visual C++ 2005. When a UserControl control contains one or more controls that require run-time licenses, the UserControl control implicitly becomes a licensed control (a control that requires a run-time license). In Microsoft Visual Basic 6.0, you can manually add licenses to the licenses collection by using the Licenses.Add() method. For more information about what the License.Add() method does in Visual Basic 6.0, see the References section of this article.
In Visual C++ .NET, the licenses collection no longer exists. To dynamically add the UserControl control, you must embed the license information in the control before you use the following code:
This article assumes that the control that requires the run-time license is in the MyProject.ocx file and that the control is named MyLicensedControl. MyProject.ocx is located in the C:\Windows\System32 folder. Alternatively, you can use Mscomctl.ocx to display the TreeView control on the form.
Note The aximp part of this command does not generate the COM Interop assembly if it already exists in the Global Assembly Cache (GAC). In the Mscomctl.ocx file, the Mscomctl.dll file already exists in the GAC. Therefore, the Mscomctl.dll file is not created again.Back to the top
Dynamically create the control
In Solution Explorer, right-click the ActiveXLicense project node, and then click Add Reference.
In the Add Reference dialog box, click the COM tab.
Click Browse, and then select the ActiveX Control Interop assemblies that you have created in the Create ActiveX interop assemblies section. These files must be in the C:\Temp folder.
Add both AxMyProject.dll and MyProject.dll. When you add these files, the Visual Studio .NET IDE creates a Temp folder in the project folder and copies the files to this folder.
If you are using the Mscomctl.ocx file, adding only the Axmscomctl.dll file is sufficient.
Add a Button control to the Form1 form.
Double-click button1 to add the button1_Click event handler in the code window.
Add the following code in the button1_Click event handler:
AxMyProject::AxMyLicensedControl* myControl = new AxMyProject::AxMyLicensedControl();
If you are using the Mscomctl.ocx file, add the following code:
Axmscomctl::AxTreeView* myControl = new Axmscomctl::AxTreeView();
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.
System::Reflection::FieldInfo* f = __typeof(AxHost)->GetField(S"licenseKey", static_cast<System::Reflection::BindingFlags> (System::Reflection::BindingFlags::NonPublic|System::Reflection::BindingFlags::Instance));f->SetValue(myControl, S"sopomnvntoqopjolrltjllvj");
Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the name of the project.
Expand Configuration Properties, and then click General.
Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.
For more information about the common language runtime support compiler option, visit the following Microsoft Web site:
Note If you are using the Mscomctl.ocx file, replace the string parameter value in the call to the SetValue function with the key value of TreeView control (9368265E-85FE-11d1-8BE3-0000F8754DA1).
This code inserts the run-time license in the control. It creates an instance of the FieldInfo object. In this case, you are obtaining the licenseKey field and setting the field for the myControl control. You must do this for each instance of any ActiveX control that requires a run-time license.
Add the control to the Controls collection of the form, and then show the control. To do this, add the following code in the button1_Click event handler after the code that you added in step 2 of the Add the license to the control section:
Note If the license is not added to the control and the license is required, the "myControl->Show" line fails. The "myControl->Show" line runs the code that contains license validation. When you try to run the "myControl->Show" line without a valid license in Debug mode, you may receive the following error message:
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 receive an error message that is similar to the following:
An unhandled exception has occurred in your application. You do not have a license to use this ActiveX control.
Press CTRL+SHIFT+B to build the solution.
Press CTRL+F5 to run the program.
Click button1 to add your ActiveX control to the form.
Important 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 the MyLicensedControl control, you have to also add the license to that instance.
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 how to determine the key, click the following article number to view the article in the Microsoft Knowledge Base:
151771 LICREQST.EXE Requesting a license key from an object
Alternatively, you can use the Licenses.Add method for the control in Visual Basic 6.0 program on a computer that has the license in the registry. The Licenses.Add method returns the run-time license that it added as a string; you can then use the Debug.Print method in Visual Basic 6.0 to obtain the license key.