Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

In-depth analysis of the principle and source code of .NET license compiler (Lc.exe)

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "in-depth Analysis of the principles and Source Code Analysis of .NET license Compiler (Lc.exe)". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "in-depth analysis of the principles and source code analysis of the .NET license Compiler (Lc.exe)".

When using a third-party class library, you will often see such a Demo license file included in its own demo program.

The copy code is as follows:

Infragistics.Win.Misc.UltraButton, Infragistics2.Win.Misc.v11.1, Version=11.1.20111.2009, Culture=neutral, PublicKeyToken=f8b58b62b52fdf31

Infragistics.Win.Misc.UltraLabel, Infragistics2.Win.Misc.v11.1, Version=11.1.20111.2009, Culture=neutral, PublicKeyToken=f8b58b62b52fdf31

Infragistics.Win.Printing.UltraPrintPreviewDialog, Infragistics2.Win.UltraWinPrintPreviewDialog.v11.1, Version=11.1.20111.2009, Culture=neutral, PublicKeyToken=f8b58b62b52fdf31

Infragistics.Win.UltraWinDataSource.UltraDataSource, Infragistics2.Win.UltraWinDataSource.v11.1, Version=11.1.20111.2009, Culture=neutral, PublicKeyToken=f8b58b62b52fdf31

The format of this file is a text file, but it should be written in accordance with its format requirements:

Control name, assembly full name

First, write a list of controls that need to be authorized as needed, in the format shown above. For example, if HostApp.exe 's application wants to reference the authorization control MyCompany.Samples.LicControl1 in Samples.DLL, you can create a HostAppLic.txt that contains the following. MyCompany.Samples.LicControl1, Samples.DLL .

Then call the following command to create a .clients file named HostApp.exe.licenses. Lc / target:HostApp.exe / complist:hostapplic.txt / i:Samples.DLL / outdir:c:\ bindir

The build embeds the .clients file as a resource in the resource of HostApp.exe. If you are building a C# application, you should use the following command to build the application.

Csc / res:HostApp.exe.licenses / out:HostApp.exe * .cs

The LC.EXE file in the .NET Framework SDK directory is written in the .NET language, and its function is to generate resource files based on the contents of the license file. At the last moment of compilation, the generated resource file is embedded into the execution file by the CSC compiler.

Load LC.EXE with .NET Reflector and begin the journey of source code analysis.

At the entrance of the program, the command line parameters are analyzed first, and the specified functions are performed according to the different parameters. First look at a complete list of parameters. The code is the following three lines

The copy code is as follows:

If (! ProcessArgs (args))

{

Return num

}

MSDN has a complete explanation, copy it below for your reference, in order to reduce the interruption caused by finding MSDN.

/ complist:filename specifies the file name that contains a list of authorized components to be included in the .authorization file. Each component is referenced by its full name, and there is only one component per line. Command line users can specify a separate file for each form in the project. Lc.exe accepts multiple input files and produces a .clients file.

/ h [elp] displays the command syntax and options for the tool.

/ i:module specifies modules that contain the components listed in the file / complist. To specify multiple modules, use multiple / I flags.

/ nologo cancels the display of the Microsoft startup title.

/ outdir:path specifies the directory in which to place the output .exports file.

/ target:targetPE specifies the executable for which the .clients file is generated.

/ v specifies the detailed mode; displays compilation progress information.

/? Displays the command syntax and options for the tool.

The key role of the ProcessArgs method is to analyze the component list and assembly list, as shown in the following code

The copy code is as follows:

If ((! flag3 & & (str2.Length > 7)) & & str2.Substring (0,7) .ToUpper (CultureInfo.InvariantCulture) .Equals ("TARGET:"))

{

TargetPE = str2.Substring (7)

Flag3 = true

}

If ((! flag3 & & (str2.Length > 8)) & & str2.Substring (0,9) .ToUpper (CultureInfo.InvariantCulture) .Equals ("COMPLIST:"))

{

String str3 = str2.Substring (9)

If ((str3! = null) & & (str3.Length > 1))

{

If (compLists = = null)

{

CompLists = new ArrayList ()

}

CompLists.Add (str3)

Flag3 = true

}

}

If ((! flag3 & & (str2.Length > 2)) & & str2.Substring (0,2) .ToUpper (CultureInfo.InvariantCulture) .equals ("I:"))

{

String str4 = str2.Substring (2)

If (str4.Length > 0)

{

If (assemblies = = null)

{

Assemblies = new ArrayList ()

}

Assemblies.Add (str4)

}

Flag3 = true

}

After analyzing the components and assemblies, let's move on to the meaning of ResolveEventHandler delegates. If the runtime class loader cannot resolve a reference to an assembly, type, or resource, an appropriate event is raised, giving the callback an opportunity to tell the runtime which assembly, type, or resource the referenced assembly is in. ResolveEventHandler is responsible for returning assemblies that resolve types, assemblies, or resources.

The copy code is as follows:

ResolveEventHandler handler = new ResolveEventHandler (LicenseCompiler.OnAssemblyResolve)

AppDomain.CurrentDomain.AssemblyResolve + = handler

Loop through the list of components analyzed by the first parameter to generate a license for them.

The copy code is as follows:

DesigntimeLicenseContext creationContext = new DesigntimeLicenseContext ()

Foreach (string strin compLists)

{

Key = reader.ReadLine (); hashtable [key] = Type.GetType (key); LicenseManager.CreateWithContext ((Type) hashtable [key], creationContext)

}

Finally, generate the license file and save it to disk, waiting for the CSC compiler to compile it into a resource file and embed it in the assembly.

The copy code is as follows:

String path = null

If (outputDir! = null)

{

Path = outputDir + @ "\" + targetPE.ToLower (CultureInfo.InvariantCulture) + ".clients"

}

Else

{

Path = targetPE.ToLower (CultureInfo.InvariantCulture) + ".clients"

}

Stream o = null

Try

{

O = File.Create (path)

DesigntimeLicenseContextSerializer.Serialize (o, targetPE.ToUpper (CultureInfo.InvariantCulture), creationContext)

}

Finally

{

If (o! = null)

{

O.Flush ()

O.Close ()

}

}

This method is recommended by. NET Framework to protect the component, which is different from the input serial number and RSA signature that we usually discuss.

Take a look at how commercial components apply this technology to protect components.

The copy code is as follows:

Using System

Using System.Web

Using System.Web.UI

Using System.Web.UI.WebControls

Using System.ComponentModel

Namespace ComponentArt.Licensing.Providers

{

# region RedistributableLicenseProvider

Public class RedistributableLicenseProvider: System.ComponentModel.LicenseProvider

{

Const string strAppKey = "This edition of ComponentArt Web.UI is licensed for XYZ application only."

Public override System.ComponentModel.License GetLicense (LicenseContext context, Type type, object instance, bool allowExceptions)

{

If (context.UsageMode = = LicenseUsageMode.Designtime)

{

/ / We are not going to worry about design time Issue a license

Return new ComponentArt.Licensing.Providers.RedistributableLicense (this, "The App")

}

Else

{

String strFoundAppKey

/ / During runtime, we only want this control to run in the application

/ / that it was packaged with.

HttpContext ctx = HttpContext.Current

StrFoundAppKey = (string) ctx.Application ["ComponentArtWebUI_AppKey"]

If (strAppKey = = strFoundAppKey)

Return new ComponentArt.Licensing.Providers.RedistributableLicense (this, "The App")

Else

Return null

}

}

}

# endregion

# region RedistributableLicense Class

Public class RedistributableLicense: System.ComponentModel.License

{

Private ComponentArt.Licensing.Providers.RedistributableLicenseProvider owner

Private string key

Public RedistributableLicense (ComponentArt.Licensing.Providers.RedistributableLicenseProvider owner, string key)

{

This.owner = owner

This.key = key

}

Public override string LicenseKey

{

Get

{

Return key

}

}

Public override void Dispose ()

{

}

}

# endregion

}

First create a type that inherits from the License type, and then create a type that inherits from the LicenseProvider, which is used to issue licenses, including design-time and run-time licenses. As you can see from the above example, there are no restrictions at design time and can be run, but at run time, you must have an ordered column number before it will generate a license object instead of returning null to the .NET Framework type. The whole verification process is completed by. Net.

All you need to do is apply this license protection mechanism as follows:

The copy code is as follows:

[LicenseProvider (typeof (RedistributableLicenseProvider))]

Public class MyControl: Control {

/ / Insert code here.

Protected override void Dispose (bool disposing) {

/ * All components must dispose of the licenses they grant.

* Insert code here to dispose of the license. , /

}

}

The verification code (RedistributableLicenseProvider) licensed by the control is completely separated from the logic of the control itself, and the intellectual property rights of the components are protected by division of labor and cooperation.

At this point, I believe you have a deeper understanding of "in-depth analysis of the principles and source code analysis of the .NET license Compiler (Lc.exe)". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report