📜 ⬆️ ⬇️

Making the library written in .Net understandable for Unmanaged code

Half a year after working in a company that is engaged in programming in MQL under Meta Trader, I encountered such an order: the client needs the program to be written in C # or VB.Net.

The task was to write a program in C # and dll in the same language that connects the application with the MQL trading advisor. On the one hand, it is good for me, since I have been studying C # for more than a year, on the other, it has become incomprehensible, how is this done and is it even possible?
image

It is not a secret for anyone that a library written in C # does not have a section for export in its code, and it is not possible to transfer the name of the function itself. A couple of hours of searching on the Internet did let me understand that the solution exists, and it consists in the following:

About IL heard something there, but I have never seen this miracle in my eyes. Whatever you have to look for literature, which is not so much on this issue, I will describe everything step by step. As a result, we get a library that runs perfectly from any program.

So let's start:
First of all, we will create a regular library in which there will be 2 methods, where the first one displays to everyone the favorite “Hello, World!”, The second one returns the sum of two numbers.
')
Names of functions should be chosen such that it will be easy to find later.
Actually, this is how our library code looks like:

using System; using System.Windows.Forms; namespace Test { public class Class1 { public static void Message_Export(String message) { MessageBox.Show(message); } public static Double Sum_Export(Double a, Double b) { return a + b; } } } 


Please note “Export” in the function name is needed only for more convenient search in the file, which is not mandatory. The public modifier can be both specified and not; for export functions, this does not play a big role, but the methods must be static.

Next, we compile this library, and run the decompiler using the Visual Studio command line.
Start> All Programs> Microsoft Visual Studio 2010> Visual Studio Tools> Command Line Visual Studio (2010). We write ildasm and press Enter.
image

In the program that appears, click File> Open and specify the path to our library
image
and we get a tree of classes and methods of our library

Next File> Dump, leave all settings by default and save our file with a name, for example Test.il to a disk where the file can be easily found, I have this D: \
image

With decompilation finished, in fact nothing heavy. The next step is to open the created Test.il file using any text editor and see the code that at first glance seems completely incomprehensible. There is nothing complicated in it, but we don’t really need to know what is there and where.
First we find the entry ".corflags 0x00000001" which means that the file contains only il-code, and the compiler will simply ignore all attempts to export functions, change it to ".corflags 0x00000002"

In the same section of the file, immediately after writing the .corflags, we need to extract memory for our two functions.

 .vtfixup [1] int32 fromunmanaged at VT_01 .vtfixup [1] int32 fromunmanaged at VT_02 .data VT_01 = int32(0) .data VT_02 = int32(0) 


The final step in editing the code will be the export of the necessary functions.
find the first function

 .method public hidebysig instance void Message_Export(string message) cil managed { //  : 9 (0x9) 


comments can be deleted, but this is not necessary, because the compiler will not add them to the dll.
At the beginning of the function we write an entry of the following form:

 .vtentry 1 : 1 .export [1] as Message 


in fact, under the name Message, this function will be called from the library.

top second

 .method public hidebysig instance float64 Sum_Export(float64 a, float64 b) cil managed { //  : 9 (0x9) 


we insert
 .vtentry 1 : 2 .export [2] as Sum 

respectively.

All we have to do is collect our library. In the command line of the Visual Studio we write the following:
ilasm /OUT:D:\Test.dll D: \ Test.il / dll

If you received the message Operation Completed Sucefully, then everything was done correctly and the library was compiled.
image

Source: https://habr.com/ru/post/137300/


All Articles