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

Article translations Article translations
Article ID: 816196 - View products that this article applies to.
Expand all | Collapse all

On This Page

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.

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

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.
  4. 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.

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 "C:\Windows\System32\MyProject.ocx"


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 "C:\Windows\System32\MSCOMCTL.ocx"


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.

Dynamically create the control

  1. In Solution Explorer, right-click the ActiveXLicense project node, and then click Add Reference.
  2. In the Add Reference dialog box, click the COM tab.
  3. 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.
  4. 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.
  5. Add a Button control to the Form1 form.
  6. Double-click button1 to add the button1_Click event handler in the code window.
  7. 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.

Add the license to the control

  1. Determine and note the run-time license key for the control. For the MyLicensedControl control, the license key is "sopomnvntoqopjolrltjllvj"
  2. 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"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:
    1. Click Project, and then click <ProjectName> Properties.

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

Add the control to the form

  1. 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 "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.
  2. Press CTRL+SHIFT+B to build the solution.
  3. Press CTRL+F5 to run the program.
  4. Click button1 to add your ActiveX control to the form.

Troubleshooting

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.

REFERENCES

For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
241126 INFO: Dynamically add UserControls that require run-time licenses
188577 HOWTO: What is the licenses collection used for?
190670 HOWTO: Dynamically add controls to a form with Visual Basic 6.0
For more information about licensing ActiveX controls, visit the following Microsoft Developer Network (MSDN) Web sites:
http://msdn2.microsoft.com/en-us/library/191es2w5(vs.71).aspx
http://msdn2.microsoft.com/en-us/library/aa268985(VS.60).aspx

Properties

Article ID: 816196 - Last Review: June 1, 2007 - Revision: 2.3
APPLIES TO
  • Microsoft .NET Framework 1.1
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
Keywords: 
kbwindowsforms kbforms kbdynamic kbdll kbctrl kbcontrol kbcollections kbactivexevents kblicensing kbhowtomaster KB816196

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com