Introduction
To compile and run the code, you must have installed Microsoft .NET Framework Beta 2. This is available for download from http://msdn.Microsoft.com.
When I received a copy of Visual Studio .NET Beta 2, I did not manage to find the ILAssemblyLanguageProgrammersReference.doc which was included in beta 1, so I decided to share my experience with other assembly enthusiasts. Other documents for those interested in intermediate language can be found in "C:\Program Files\Microsoft.NET\FrameworkSDK\Tool Developers Guide\docs".
Tips
Typically, you would work with assembler from the command line and using Notepad. To open a command line in any directory, create a registry entry (key) in "HKEY_CLASSES_ROOT\Folder\shell" called "CmdLine
" and add a new entry in CmdLine
called "command
". Change the (Default) to string
value REG_SZ
to "cmd.exe %1
". This allows you to open a command line using the context menu (right click on folder icon and select CmdLine
). It is also handy to create a shortcut to Notepad.exe and ildasm.exe in "C:\Documents and Settings\whatever your name is\SendTo" so that you can send .il files to Notepad and .exe files to the disassembler.
An Example IL File
First, we will write a client and from the client, send a string
to ArgIn.dll (a library we will create below) which will display our string
in a message box. Copy the following, paste it in Notepad and save it as client.il:
.assembly extern mscorlib{}
.assembly extern ArgIn{}
.assembly go{}
.method public static void GO2() cil managed
{
.entrypoint
.maxstack 1
.locals ([0] class [ArgIn]ArgIn.Class1 q)
ldstr "Hello from client"
newobj instance void [ArgIn]ArgIn.Class1::.ctor(string)
stloc.0
ret
}
Do not compile yet. First, we must compile the library. The following is the library that we are going to compile to ArgIn.dll:
.assembly extern mscorlib
{}
.assembly extern System.Windows.Forms
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 )
.ver 1:0:2411:0
}
.assembly ArgIn
{}
.namespace ArgIn
{
.class public auto ansi beforefieldinit Class1
extends [mscorlib]System.Object
{
.method public hidebysig specialname rtspecialname
instance void .ctor(string msg) cil managed
{
ldarg.0
call instance void [mscorlib]System.Object::.ctor()
ldarg.1
ldstr "From dll"
call valuetype [System.Windows.Forms]System.Windows.Forms.DialogResult
[System.Windows.Forms]System.Windows.Forms.MessageBox::Show
(string,string)
pop
ret
}
}
}
Save this as ArgIn.il. To compile, simply type on command line "ilasm ArgIn /dll
".
The section...
.assembly extern System.Windows.Forms
...deserves additional explanation. This is value on my machine, if it does not work on yours, copy and paste the following in Notepad:
using System
using System.Windows.Forms
class try1
{
public static void Main()
{
MessageBox.Show ("Test", "Test app")
}
}
Save it as test.cs and compile from command line typing "csc test.cs". Send test.exe to ildasm
and check what is in the MANIFEST
(double click on it). If it is necessary, copy the correct values and apply it in your code.
Now it is time to compile the client application. ArgIn.dll must be in the same folder as the EXE. From command line, execute "ilasm client.il
" and run client.exe. You will see message box with the message, "Hello from client
" and title "From dll
".
It's that easy!
I hope that you will find that IL assembler is very friendly and write some code on your own. At the end, I wish to thank Microsoft for making .NET platform available to the public, my company Objectronics for providing me with their copy of .NET Framework Beta 2 and finally to my dear friend, Dmitri Vibornov, for his idea and help on coding and preparing this article.
If you have any questions or suggestions, please leave a comment below.
License
This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below. A list of licenses authors might use can be found here.