Use mono to build lightweight. NET Program runtime

Source: Internet
Author: User

In using mono to run the. NET program across platforms, we have a preliminary understanding of the operating mechanisms of mono and. NET programs. Today I want to talk about "using mono to build lightweight. NET Runtime". Why would I have such an idea? Because mono and. NET can execute IL code, it is a logical idea that I use mono to run as a. NET program. Because. NET program requires the. NET Framework to provide running support, so that when the target device does not have the. NET Framework installed or the. NET Framework version is incorrect, our programs do not run smoothly. Force the user to install. NET Framework will undoubtedly affect the user experience, before Windows XP has not stopped service, domestic software vendors in order to compatible with these users, usually choose C + + language to write native applications, which caused the domestic. NET technology has long been neglected. consider. NET version of compatibility

Before considering using mono as a running time for. NET applications, we first consider compatibility issues with. NET versions. Assuming that we have built an application using the. NET Framework version 3.5, the application will run on a computer with the. NET Framework v3.5 installed, if the. NET Framework is not installed on the target computer v3.5, this application will not function properly. At this point, we can have two solutions: the first is to force users to install the. NET Framework v3.5, whether integrating the framework into the installation package or downloading the installation automatically from the Web when the software is installed, it is obvious that this approach affects the user experience and makes the user's impression of the application much less; the second, try to make the application and the. NET version compatible. We know that the Android program has a minimum API version that allows the application to run on a device that is not less than the API version. Here we choose the idea that, in a. NET program, we can specify the. NET Framework version of the application running through the Supportedruntime node in the application configuration file. For example, the following configuration file ensures that the application runs on a version between the. NET Framework v2.0 to v3.5.

<?xml version= "1.0"?>
<configuration>
    <startup> 
    <supportedruntime version= " v2.0.50727 "/>
    <supportedruntime version=
    v3.0"/> <supportedruntime version= "v3.5"/> </startup>
</configuration>

While this can guarantee application compatibility, the fate of your application is in the hands of the. NET Framework, but what if you don't have the. NET Framework installed on the user's computer? We're going to build mono run time. Mono of the running time

As we said earlier, the mono consists mainly of three parts, the C # compiler (Mcs.exe), the Mono Runtime (Mono.exe), and the underlying class library. Because we are here to make the writing. NET application runs in the Mono runtime, so what we need here is the Mono runtime (Mono.exe) and the underlying class library. We build the following directory structure:

Here are the respective structures and functions of these directories:
* Bin directory: The directory to place the Mono runtime, mainly placed Mono.exe, Mono-2.0.dll, Libgio-2.0-0.dll, Libglib-2.0-0.dll, Libgthread-2.0-0.dll a total of 5 files.
* lib directory: A directory that places mono dependent libraries, primarily placed. NET Library directory (This is an example of 4.0), the GAC Library directory. The GAC Library directory of accessibility, Mono.posix, System, System.Drawing, System.Windows.Forms a total of 5 subdirectories is our development windowsform need to use the dependent library.
* Etc Directory: Place the program we write and its related files, the main program file name is Main.exe.

Well, now we have a very lightweight. NET program running environment (in fact, the entire environment of the size of around 40M), note that the above files can be installed in the installation of mono in its directory to find. According to the information currently learned by the blogger, running the file through mono is mainly a command line and a program called Mono embedding. In particular, the second scheme can embed the runtime directly into the program, we are familiar with the Unity3d engine is to embed the entire script into the C + + program, but this approach is more complex, the temporary blogger has not yet figured out its internal mechanism, so we choose the first option here. But it has to be a command line, forcing ordinary users to use command-line tools is a painful thing to do, just as we are often confused by git. So, we're going to use a program to simulate the order. What the. Use the program to simulate the command line. It can't be simpler to write in C #, okay. Note that we cannot use the functionality in the. NET Framework here, because it is a bootstrapper, and if the bootstrapper needs to rely on. NET, then how can we do this program?

Well, then write C + + native application, it is no need to rely on any. and in C + + simulation command line mainly has winexec, ShellExecute and CreateProcess three kinds of methods, about these three kinds of methods of similarities and differences we can understand, here we choose the simplest winexec. The code is as follows:

#include <Windows.h>

int main (int agrc,char *args[])
{/

    * Execute command *
    /winexec ("Bin\\mono.exe etc\\ Main.exe ", sw_normal);
    return 0;
}

We named the compiled program Launcher.exe, placing the root directory structure of the Mono runtime directory that we defined earlier, which will be exposed to the user as a startup file, and then open the main file Main.exe when the user clicks on the program. OK, now let's verify our thoughts:

As a contrast, we give a screenshot of the normal program's operation:

This way we now basically achieve a separation of the program. NET Framework operation, why say is basic. Because. NET base Class library is present as part of the. NET Framework, that is, it is not the content of the CLR. So most of the base class libraries we use now are mono versions, and if one of the libraries we use does not have a corresponding implementation in mono, then we need to find a way to solve the dependency problem ourselves. Now this program every time the operation will flash out the command line window, although not affect the use, but for a pursuit of perfection is a flaw, how to solve it. The answer is mono embedding. Summary

In this paper, a lightweight. NET program is implemented by Mono, which, to some extent, realizes the. NET program running out of the. NET Framework. The main problem with this scenario now seems to be the question of library dependencies, we now have nearly 40M of the volume of the environment, because we put the common libraries are in the Lib directory, but in the actual operation, these libraries are not fully used, so how to generate the appropriate LIB directory according to the program, is an effective method to solve the volume of the operating environment. It is difficult to solve this problem by hand, because at ordinary times Microsoft has given these libraries to us, it is on our computer, so we have never looked at this problem. Now, when we face this problem, reflection may be a good idea, but the verification of this idea will wait until later.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.