cil

Calling varargs method via DynamicMethod

一曲冷凌霜 提交于 2019-12-21 12:21:46
问题 I'm trying to call unmanaged printf-like function using DynamicMethod. At runtime I get a BadImageFormatException:Index not found. (Exception from HRESULT: 0x80131124) Is this a limitation of runtime or my emited code is wrong? public class Program { [DllImport("msvcrt40.dll",CallingConvention = CallingConvention.Cdecl)] public static extern int printf(string format, __arglist); static void Main(string[] args) { var method = new DynamicMethod("printf", typeof(void), new Type[0], true); var il

Why .NET code compiles to MSIL?

纵饮孤独 提交于 2019-12-21 11:47:08
问题 First .NET code compiles to MSIL and then JIT convert it to machine dependent code. Can any one tell me what all benifits get because of the 2 step compilation.Thanks 回答1: There are a few reasons. First and foremost is probably to make it cross platform. If C# or other .NET languages compiled directly to native code, they would have to be recompiled for each platform they run on. With a VM, all code can be kept in an intermediate format, and you only need write a VM implementation for each

Why .NET code compiles to MSIL?

被刻印的时光 ゝ 提交于 2019-12-21 11:47:07
问题 First .NET code compiles to MSIL and then JIT convert it to machine dependent code. Can any one tell me what all benifits get because of the 2 step compilation.Thanks 回答1: There are a few reasons. First and foremost is probably to make it cross platform. If C# or other .NET languages compiled directly to native code, they would have to be recompiled for each platform they run on. With a VM, all code can be kept in an intermediate format, and you only need write a VM implementation for each

Validating .NET Framework Assemblies

守給你的承諾、 提交于 2019-12-21 04:34:07
问题 I just went through our german VB.NET forums and there was something interesting that gives me some kind of headache. It is actually possible to edit the .NET Framework assemblies using ReflexIL or some other IL editor. The only thing you have to bypass is the Strong Name signature of the assembly. After changing the assembly IL, you have to run sn.exe -Vr [assemblyname] to kinda skip the strong name validation. After that you have to clear the cached native images. Just go through the C:

CIL stack exchange instruction

余生颓废 提交于 2019-12-21 04:11:51
问题 Is there a CIL instruction to exchange the first two elements in the stack? 回答1: There is no single instruction exchange. However, using stloc, pop, and ldloc, you should be able to accomplish your exchange. 回答2: No. The only way to swap elements is to pop the top two elements to locals, then push them in reverse order. 回答3: Looking at a list of CIL instructions there doesn't appear to be a single instruction that exchanges the two elements at the top of the stack. You'll have to do it the

Create a catch-all handler for all events and delegates in C#

痞子三分冷 提交于 2019-12-20 23:25:02
问题 I want to create a handler that can be used to handle any event or delegate. Specifically, I want to be able to write code like below: class Invoker { public object Invoke(object[] arg) { // generic handling code } } static void Main() { var p = new Person(); p.AddHandler("Event1", new Invoker().Invoke); } AddHandler is an extension method for object which receive an event name and a delegate of type Func<object[], object> . It should be able to do whatever magic to bind the event (e.g.

Why do I have to do ldarg.0 before calling a field in MSIL?

冷暖自知 提交于 2019-12-20 10:25:12
问题 I want to call a function, with as parameters a string and an Int32 . The string is just a literal, the Int32 should be a field . So I thought it should be something like: .method public hidebysig instance string TestVoid() cil managed { .maxstack 1 .locals init ( [0] string CS$1$0000) L_0000: nop L_0001: ldstr "myString" L_0006: ldfld int32 FirstNamespace.FirstClass::ByteField L_000b: call string [Class1]Class1.TestClass::Functie<int32>(string, int32) L_0010: ret } But this throws the error

What are the best resources for learning CIL (MSIL)

一笑奈何 提交于 2019-12-20 09:23:23
问题 I'm an expert C# 3 / .NET 3.5 programmer looking to start doing some runtime codegen using System.Reflection.Emit.DynamicMethod. I'd love to move up to the next level by becoming intimately familiar with IL. Any pointers (pun intended)? 回答1: The best way to learn it, is to write something you understand, then look at the IL it created. Also, depending on what you are doing, you can use expression trees instead of emitting IL, and then when you compile the expression trees, those smart guys at

What should I pin when working on arrays?

久未见 提交于 2019-12-20 02:46:14
问题 I'm trying to write a DynamicMethod to wrap the cpblk IL opcode . I need to copy chunks of byte arrays and on x64 platforms, this is supposedly the fastest way to do it. Array.Copy and Buffer.BlockCopy both work, but I'd like to explore all options. My goal is to copy managed memory from one byte array to a new managed byte array. My concern is how do I know how to correctly "pin" memory location. I don't want the garbage collector to move the arrays and break everything. SO far it works but

Do method names get compiled into the EXE?

纵然是瞬间 提交于 2019-12-19 19:41:32
问题 Do class, method and variable names get included in the MSIL after compiling a Windows App project into an EXE? For obfuscation - less names, harder to reverse engineer. And for performance - shorter names, faster access. e.g. So if methods ARE called via name: Keep names short , better performance for named-lookup. Keep names cryptic , harder to decompile. 回答1: Yes, they're in the IL - fire up Reflector and you'll see them. If they didn't end up in the IL, you couldn't build against them as