.NET Framework gives the application license (medium)

zhaozj2021-02-11  148

Translation: Zou Jianqiang by kenn scribner

Compile license file

I should mention lc.exe before leaving all license files. LC.EXE or License Compiler, is a tool for assembling .NET Framework, which acquires license file information and encodes as an assembly resource. Since the default implementation of LicfilelicenseProvider does not look for a resource-based license file, this tool is temporarily added by Framework, but it has some interested flexibility. First, it produces a resource information method. Interestingly, it can encode multiple license files into single resources, and allow you to license different license files to multiple controls at once. In my opinion, it also has a relatively large limit, that is, you have to use the assembly link (Al.exe) to compile your compiled modules and resources. This is not a problem for those who are prefers to the command line. Since Visual Studio .NET does not compile or generate multi-module compilation, this is a limit for most people using Visual Studio .Net (although you can use a "embedded resource" build operation to embed resource input Your project file is among.

Speaking of Visual Studio .NET, you will find that if you want to create a license, you can't find automated support, whether it is Visual Studio's 2002 or 2003. So how can I create such a control? The answer is that you simply create such files and collect it and the compiled program. In my personal concern, add this file with "none" Build operation to the project of Visual Studio, do not need it. When this file and other items / solution files are packaged together, this only makes the source code control slightly. Also have to manually copy this file to the assembly execution directory.

Optional license program

I think Microsoft is obligated to provide a default license provider for .NET Framework, and select the implementation similar to the ActiveX control license. I also think that Microsoft does not consider applying such a simple license program to their other software products (or at least any important software products). In any case, why do we provide a license scheme that is easier to assemble, more intelligent, and more difficult to crack? Below is this optional license scheme.

You can issue licenses to your control in any way you like. Perhaps you can design it only on Tuesday, or you can decide whether this control is performed in a program according to the weather forecast of the user in the user's region (for example, get the user's postal code and download the relevant weather forecast from the internet. ). I am not joking. you can do it. This license issues are flexible to you can do this, but this doesn't mean you should do this. I will demonstrate a more practical approach to use the registry license method. Before I describe this program, you should know a truth:

The .NET Windows Forms application is born in System.Windows.Forms.form, which is derived from System.Windows.Forms.Control!

Wait ... What does this mean? Yes, this means that you can issue license authorizations throughout the app to a license for a control. Although I demonstrates the license authorization of the application rather than the control, it can also be applied to both. Let us think about the meaning of this sentence.

Application and control license

Aware that the basic difference between the application and the control license is when you truly care about the license. If you have developed some controls and components in order to make a living, the target market is made up of other software developers. This means that your license solution is necessary to legally check the insertion operation added to the Visual Studio project in Design Time. Because you may want your control library to sell several million copies, you will provide this runtime license for free, or do not have a runtime license. After all, your target market is sold to the app, so they will buy more controls from you. However, the license for the application is different. We have built applications when we design, and we will not think of the design time. Instead, we want to ensure that the user gets the corresponding license information at the appropriate position of run time.

Frame designer takes into account this when they design a license structure. When getLicense () calls, you can check the license request status, whether it is still runtime. These two cases I can demonstrate it to you, but the verification license we have to consider now is that it is runtime, which is because this is the application's license issued different from the control. Therefore, you should realize this is an important difference.

Register-based license

The registry-based license implements a license scheme, which checks the registration keys containing the specified key value. The application itself does not have to write code for implementing a registry key value - this is done by the installer. Since many programs use the registry in a wide variety of forms, this will not become a restriction of development. Our installer will preempt a registration key value in advance, and there is a registration.

For the key value written in the registry, if I am willing to write more better, but for this example, I just write "installed" strings for "INSTALLED". I will leave some better implementations to give you (Hey, I can't give me all the secrets!). The key value below is what I am looking for:

HKEY_CURRENT_USER / SOFTWARE / ACME / HOSTKEYS / 2DE915E1-DF7

1-

3443-9f4D-32259C92CED2

You see the GUID value is a GUID I assigned to my application. Each application sold by Acme Software has its own guid, but by reflection, I can use the same license provider for all applications. I will fill all the ACME Software license key values ​​here, so this button name is "host key". In your own application, you can put this primary key at any suitable place.

Just like you are very simple in Figure 3.

Figure 3. Applications based on registry licenses

If you see this window, this license is legal. On the other hand, if this license is illegal, you will see the exception dialog of Figure 4.

Figure 4. Illegal registry license feedback

In order to make it, we need to create a new app and do some slight changes to the code generated by the design wizard. By the way, although the code is displayed here is C #, but I don't despise the VB programmer, these examples are easier to transform, and I also provide C # and VB.NET in this article. Language.

Before starting, in addition to writing our own license provider, we have to modify the source code four places to the main program. These four places are:

Application class declaration (add some attribute) application class constructor's display () method application "main" method

Since I use Guidattribute, I will add a USING clause. Let's take a look at the class statement:

///

/// Summary description for frmMain./// [GuidAttribute ( "2de915e1-df71-3443-9f4d-32259c92ced2")] [LicenseProvider (typeof (RegistryLicenseProvider))] public class frmMain: System.windows.Forms.form {...}

I added two attributes to this class: GuidAttribute and LicenseProvider. You have seen licenseProvider, and the purpose of another GuidAttribute property is to assign a GUID value to this application class. Usually this property is used for COM interoperability. I reuse it here, simply assign a unique digital identification code to the application class, which is supported by Framework (that is, we can easily determine the type of GUID of class types later. ). I can generate my own properties, but I will lose the built-in frame GUID detection support. I specified GUID values ​​to GuidAttribute must be matched to the GUID value I used in the registry key value. This is very important. The value of my license provider reflects to generate an application registry key value is this value. Because I use the GuidAttribute property, I must add this value into my namespace collection:

Using system.Runtime.InteropServices;

In the constructor of the application class, I fill in the previous code:

Private license _license = NULL;

Public frmmain () {/// Required for Windows form designer support // initializationcomponent ();

// Obtain the license _license = licenseManager.validate (Typeof (frmmain);}

Here I simply call the validate () method of the license manager, there is no special place. However, if the license is illegal, the validate () method can throw an exception. So capturing this exception in some places is a good idea. But also note that I fill a private data member called _License. Some license schemes use this license object (not provided in this example, but in all cases, I will need to handle the license when the application ends. It may be some resources that allocate the license that should be released correctly, so I have to modify the application's Dispose () method:

///

/// clean up any resources being used.///

Protected Override Void Dispose (Bool Disposing) {if (disponents! = null) {components.dispose ();

IF (license! = null) {license.dispose (); license = null;}} base.dispose (disposing);

Here I made a quick inspection on NULL licenses. If the value is not null, I call it Dispose () method, otherwise it calls its reference. Even if I don't use the license license in this case, this operation is still necessary because this license contains resources that must be released. Finally, I modified the app

Main

() Methods, take into account the exception of the license manager:

///

/// the main entry point for the application.///

[Stathread]

Static void

Main

() {// create an instance of the license; try {// this will throw a licenseException if the // license is invalid ... if we get an exception, // "app" Will Remain Null And the run () method // (BELOW) WILL NOT BE EXECUTED ... APP = new frmmain ();} // try catch (Exception ex) {// catch any error, but especial licensing errors ... String Strerr = String.Format ("Error Executing Application: '{0}'", EX.MESSAGE); MessageBox.Show (Strerr, "RegistryLicensedApplication Error", MessageBoxButtons.ok, MessageBoxicon.Error);} // catch

IF (app! = null) Application.run (app);

转载请注明原文地址:https://www.9cbs.com/read-5932.html

New Post(0)