473,379 Members | 1,201 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Assembling and Executing MSIL from C# Code

Hi,

I'd like to take a string containing MSIL code, assemble it, execute
it, and receive the result all from my running C# application.

So far I've managed to manually create some MSIL code that I can
successfully assemble with ilasm and execute at the DOS prompt, and
I've read up on System.Reflection.Emit and CodeDom, but I don't see how
to do this. It looks like I would have to write a whole
System.CodeDom.Compiler.CodeCompiler; I hope I'm wrong about that,
since it seems that the whole purpose of that class is to generate the
MSIL from a source language, but I've already got the MSIL.
Thanks in advance for any pointers.

Peace,
--Carl

Nov 16 '05 #1
7 2007

<ca***********@gmail.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
Hi,

I'd like to take a string containing MSIL code, assemble it, execute
it, and receive the result all from my running C# application.

So far I've managed to manually create some MSIL code that I can
successfully assemble with ilasm and execute at the DOS prompt, and
I've read up on System.Reflection.Emit and CodeDom, but I don't see how
to do this. It looks like I would have to write a whole
System.CodeDom.Compiler.CodeCompiler; I hope I'm wrong about that,
since it seems that the whole purpose of that class is to generate the
MSIL from a source language, but I've already got the MSIL.
Thanks in advance for any pointers.


I don't believe the framework has an IL parser+assembler class, which is
what you'd need. You might be able to find a third party one or you write a
rudimentary assembler yourself(the SDK includes an il grammar file). The
last option wiould be to launch ilasm from your project and let it assemble
the code.

I've got a class like you are talking about slated on my todo list, but it
will be a while yet.
Nov 16 '05 #2
"ca***********@gmail.com" wrote:
I'd like to take a string containing MSIL code, assemble it, execute
it, and receive the result all from my running C# application.

So far I've managed to manually create some MSIL code that I can
successfully assemble with ilasm and execute at the DOS prompt, and
I've read up on System.Reflection.Emit and CodeDom, but I don't see how
to do this. It looks like I would have to write a whole
System.CodeDom.Compiler.CodeCompiler; I hope I'm wrong about that,
since it seems that the whole purpose of that class is to generate the
MSIL from a source language, but I've already got the MSIL.


Does the CIL [Common Intermediate Language - the now-old replacement
name for MSIL] have to come from a string? You say you're creating
code to pass to ILASM - could the process that creates that code just
call Emit directly?

Emit is reasonably straightforward, but if you have to read a string,
you have to write an assembler, which IS a full-blown compiler, albeit
a very rudimentary one. You have to tokenize, detect bad syntax, &c.

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs
Nov 16 '05 #3
Thanks, Daniel,
I've got a class like you are talking about slated on my todo
list, but it will be a while yet.

I'd sure be interested to see that when you get to it.

Peace,
--Carl

Nov 16 '05 #4
Hi, Jon,
Does the CIL [Common Intermediate Language - the now-old replacement
name for MSIL] have to come from a string? You say you're creating
code to pass to ILASM - could the process that creates that code just
call Emit directly?

Emit is reasonably straightforward, but if you have to read a string,
you have to write an assembler, which IS a full-blown compiler, albeit a very rudimentary one. You have to tokenize, detect bad syntax, &c.


I'm looking at some Emit examples now. I could probably use that, but
I'm trying to start simple. Since generating the CIL [thanks] seemed
simple, for the _very_ simple code I'm starting with, I was trying not
to introduce all of the structure of emitters and code generators and
the like. Expecting to get there eventually, if the project goes
anywhere, but wanting to start out with the simplest thing (something I
could understand immediately) and work my way up.

Thanks,
--Carl

Nov 16 '05 #5

When you create the dynamic assembly, with the AssemblyBuilder, there is
a SaveAndRun method.

Without knowing the nature of the receiving application, it's hard to
say exactly how to set it up, but the answer to your question should be
/yes/ .

ca***********@gmail.com wrote:
Hi,

I'd like to take a string containing MSIL code, assemble it, execute
it, and receive the result all from my running C# application.

So far I've managed to manually create some MSIL code that I can
successfully assemble with ilasm and execute at the DOS prompt, and
I've read up on System.Reflection.Emit and CodeDom, but I don't see how
to do this. It looks like I would have to write a whole
System.CodeDom.Compiler.CodeCompiler; I hope I'm wrong about that,
since it seems that the whole purpose of that class is to generate the
MSIL from a source language, but I've already got the MSIL.
Thanks in advance for any pointers.

Peace,
--Carl

Nov 16 '05 #6
Thanks, Eyeawanda,

But I can't find SaveAndRun. Could my version of C# or of the .net
library be out of date? I also couldn't find a reference for
SaveAndRun on the web.

As far as the nature of the receiving application - I am trying to
further explore the notion of "zero button testing", like this:
<http://s92417348.onlinehome.us/software/zbt/>

I want to take very simple functions, starting with this:

one:
~~expect returns 1
~~return 1

and generate and execute code that tests whether the expectation
passes. Here's what I hand-generated for the above:

..method static int32 one() cil managed
~~{
~~.maxstack 1
~~ldc.i4.1
~~ret
~~}

..namespace __TEST
~~{
~~.method static bool __TEST.one() cil managed
~~~~{
~~~~.maxstack 2
~~~~.locals init (int32)
~~~~.try
~~~~~~{
~~~~~~call int32 one()
~~~~~~stloc.0
~~~~~~leave.s CALL_SUCCEEDED
~~~~~~}
~~~~catch [mscorlib] System.Exception
~~~~~~{
~~~~~~pop
~~~~~~leave.s FAIL
~~~~~~}
~~CALL_SUCCEEDED:
~~~~ldloc.0
~~~~ldc.i4.1
~~~~bne.un.s FAIL
~~~~ldc.i4.1
~~~~br.s FINISH
~~FAIL:
~~~~ldc.i4.0
~~FINISH:
~~~~ret
~~~~}
~~}

I want to develop this incrementally, with tests the whole way along.
So I want to be able to test that appropriate CIL is generated, and
that it executes. And (for instance) that if the function itself
(one() in this example) is malformed, the __TEST code nevertheless runs
and still indicates a failure.

That desire for incremental development is scaring me away from the big
Compiler classes - I could of course just stub out everything, but I'd
rather work with smaller pieces.

Thanks,
--Carl

Nov 16 '05 #7
"ca***********@gmail.com" wrote:
I'm looking at some Emit examples now. I could probably use that, but
I'm trying to start simple. Since generating the CIL [thanks] seemed
simple, for the _very_ simple code I'm starting with, I was trying not
to introduce all of the structure of emitters and code generators and
the like. Expecting to get there eventually, if the project goes
anywhere, but wanting to start out with the simplest thing (something I
could understand immediately) and work my way up.


That makes sense. You do need to setup quite a stack of xBuilder
objects before you can Emit the first instruction. Otoh, the second
method or the second class is a lot easier than the first, because it
can use much of the same stack. I got started by copying an example,
and broadened my understanding as I needed to do different things. (I
think "tackle the immediate problem" generally beats "understand every
detail before you start" as a design and problem solving strategy.)

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs
Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Darren Dale | last post by:
Some time ago I asked about executing a python program or script. For windows, I was informed that the .py extension could be added to some list of executable extensions, and then I could just type...
2
by: Alex Fennell | last post by:
Hi All I can't seem to find anything about this but is there a way to get VS.Net to compile into ASCII MSIL rather than a binary assembly? Would I have to use the command line compiler to do this?
1
by: Jon | last post by:
My question is: Can the Garbage Collector (GC) suspended a managed thread while it is executing native code. The reason I am interested in this is that I have: 1) a native thread (N) that only...
0
by: Ben | last post by:
Hello, I've been developing apps in Delphi for years and have just started writing my first big project in c# + ms .net and have some questions about security and untrusted code. I've got an...
3
by: Tony Johansson | last post by:
Hello! I'm reading in a book and it says the following. "MSIL(Microsoft Intermediate Language) generated by all the .NET language compilers. This is a common standard for the binary code...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.