Microsoft KB Archive/816196

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

Article ID: 816196

Article Last Modified on 6/1/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition

-







For a Microsoft Visual Basic .NET version of this article, see 326651.



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

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System::ComponentModel
 * System::Collections
 * System::Windows::Forms
 * System::Data
 * System::Drawing

IN THIS TASK

 * INTRODUCTION
 * Requirements
 * Create a Visual C++ .NET or Visual C++ 2005 project
 * Create ActiveX interop assemblies
 * Dynamically create the control
 * Add the license to the control
 * Add the control to the form
 * Troubleshooting
 * REFERENCES



INTRODUCTION
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: Controls->Add 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.

Back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005
 * Microsoft .NET Framework 1.1 or later

This article assumes that you are familiar with the following topics:
 * Programming with Microsoft Visual C++ .NET or Microsoft Visual C++ 2005
 * Licensing components and controls

Back to the top

Create a Visual C++ .NET project

 * 1) Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005.
 * 2) On the File menu, point to New, and then click Project.
 * 3) Under Project Types, click Visual C++ Projects, and then click Windows Forms Application (.NET) under Templates.

Note In Visual Studio 2005, click Visual C++ under Project Types, and then click Windows Forms Application under Templates.
 * 1) In the Name box, type ActiveXLicense, and then click OK.

By default, a form that is named Form1 is created and opened in Design mode.

Back to the top

Create ActiveX interop assemblies
To create the two interop assemblies that you must have, type the following command at the Visual Studio .NET command prompt:

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

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

If you are using the Mscomctl.ocx file, at the command prompt, type the following command:

aximp /out:C:\temp\AxMSCOMCTL.dll &quot;C:\Windows\System32\MSCOMCTL.ocx&quot;

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

Back to the top

Add the license to the control
<ol> Determine and note the run-time license key for the control. For the MyLicensedControl control, the license key is &quot;sopomnvntoqopjolrltjllvj&quot;</li>  Add the following code in the button1_Click event handler after the code that you added in step 7 of the Dynamically create the control section: System::Reflection::FieldInfo* f = __typeof(AxHost)->GetField(S&quot;licenseKey&quot;,   static_cast<System::Reflection::BindingFlags>    (System::Reflection::BindingFlags::NonPublic|System::Reflection::BindingFlags::Instance));

f->SetValue(myControl, S&quot;sopomnvntoqopjolrltjllvj&quot;); 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: <ol style="list-style-type: lower-alpha;"> Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> 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.</li></ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

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

Back to the top

Add the control to the form
<ol>  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: Controls->Add(myControl); myControl->Show; Note If the license is not added to the control and the license is required, the &quot;myControl->Show&quot; line fails. The &quot;myControl->Show&quot; line runs the code that contains license validation. When you try to run the &quot;myControl->Show&quot; 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.

</li> Press CTRL+SHIFT+B to build the solution.</li> Press CTRL+F5 to run the program.</li> Click button1 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 the MyLicensedControl control, you have to also add the license to that instance.</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 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.</li></ul>

Back to the top

<div class="references_section">