exposing C# methods to unmanaged - c#

that approach in "possible duplicate" is different to the one in the book. The book's approach is to extend a managed .dll after compilation through a decompile, recompile process. The approach that you think is an exact duplicate is actually a pre compilation technique. I prefer method in the book because it is more AOP in style. Thanks for the link though. I will also explore this avenue
In Expert .NET 2.0 IL Assembler, in Ch 18 pp. 387 Serge Lidin talks about Creative Round-Tripping. He says, "ILAsm allows you to export the managed methods as unmanaged
entry points".
I haven't heard this talked about anywhere else. Is this something that you can do in PostSharp? Are there any downloadable code / script examples of using this technique available?
To clarify, I don't want to use COM or Managed C++. So what's the best way to implement a call to a C# method from C/C++ through thunking?

ildasm, change corflags, add v-table info and export function, ilasm and you're done.
Here's a code project article:
http://www.codeproject.com/Articles/37675/Simple-Method-of-DLL-Export-without-C-CLI
Which is based on:
Dead link:
http://www.csharphelp.com/2007/03/exporting-managed-code-as-unmanaged/
Wayback Machine for dead link:
https://web.archive.org/web/20140213030149/http://www.csharphelp.com/2007/03/exporting-managed-code-as-unmanaged/
Which is based on:
http://www.amazon.com/Inside-Microsoft-NET-IL-Assembler/dp/0735615470 (the first edition of the book you refer to)

Related

Why does ILSpy generate a source even though it was developed with C ++ Windows Application?

Why does ILSpy generate a source even though it was developed with C ++ Windows Application?
https://imgur.com/a/nAAnV
developing it with C ++↑
enter image description here
↑In order not to display like Noah CSGO?
Most probably you've missed the fact, that you can write in C++ and compile it into MSIL assembly and run that as .net managed code.
Go and see - in Microsoft Visual Studio for C++ there's a compiler switch /clr:pure or other flags (msdn: https://msdn.microsoft.com/library/k8d11d4s.aspx).
When you use this mode, you will be able to use special markers to define that specific classes should be compiled as CLR managed classes. If you take care and write everything properly, /clr:pure will emit a normal 100%-managed assembly. I'm tempted to say "everything as if in C#" but that's not true, generated code will be different, but nevertheless, everything will work, metadata will be there, etc. ILSpy will decompile that just like any assembly generated by C#, VB.Net or F#, but probably the code/names/etc will be a bit more mangled.
Sidenote: If you invest in some real decompiler like Reflector, they can often take a C# assembly and decompile it as-if-written-in-managed-C++ and vice versa..
Furthermore, if you don't care about "pure" msil, you can even mix native and managed code. Half of the code in native C++, other half in managed, just take some care regarding pointers and references and cleanups, and compiler will happily emit for you a mixed assembly - here I have no idea if ILSpy will manage to show you anything. Probably it will show you the managed part and show the native part as being there but as not-disassemble-able.
Finally, it's also https://learn.microsoft.com/en-us/dotnet/framework/app-domains/how-to-build-a-multifile-assembly which ILSpy probably also supports.

How is it possible to see C# code after compilation/optimization?

I was reading about the yield keyword when I came across a sample chapter from C# in Depth: http://csharpindepth.com/Articles/Chapter6/IteratorBlockImplementation.aspx.
The first block of code utilizes the yield keyword to make a simple iterator. But, the second block of code shows this code after the compiler has had its way with it. Among other things, it has exploded the yield statement into a state machine.
Several other examples of code being modified by the compiler is evident on the page.
My question is: Was the author actually able to access the code after compilation, or did he infer what it was going to look like?
You can have a look using Reflector, that's probably your best bet:
http://reflector.red-gate.com
The author himself mentioned:
Obviously the compiler doesn't actually produce C#, but I've used Reflector to decompile the code as C#.
in the same paragraph, titled High level overview: what's the pattern?
Probably both. It's quite easy to reverse-engineer compiled assemblies using Reflector. And the C# language spec, which defines how various syntactic-sugary things are compiled, is a public document. The author could have used either approach, or a mixture of the two.
Check out ildasm to take a look at the compiled IL.
(Really, it's good fun once you get your eye in)
.NET CLR actually has a form of assembly called MSIL, along with an assembler and dissembler. So yes, you can actually compile the code, then see the exact compiled CLR instructions.
https://web.archive.org/web/20211020103545/https://www.4guysfromrolla.com/articles/080404-1.aspx
dotPeek by JetBrains
https://www.jetbrains.com/decompiler/
It's free and easy to use :)

Forward error correction in .NET

Are there any libraries (paid or free, though free is obviously better) that provide forward error correction for .NET applications? I've tried looking through the source code of some of the open source C / C++ libraries, but quite frankly, the math is confusing and my inability to read other peoples' C code with any reasonable level of clarity is a major road block.
If you have free C++ libraries why don't you try to build CLI wrapper around them?
Forward Error Correction is for instance used in media streaming.
A quick google search on "forward error correction" does not reveal much, but you could go another way:
Compile the C/C++ implementation (for instance from the list on Christian Schuler's Forward Error Correction (FEC) Page) of your choice into a DLL, then use P/Invoke to call functions in that DLL.
Another option that might fit you is to use the .NET interface to the Windows Media Services 9 series.
It contains the IWMSPublishingPoint interface that has a EnableFEC property.
--jeroen
Have you looked at the PAR2 format specification? PAR2 files provide raid-like parity for downloaded files (mostly popular on usenet binary groups). Although PAR2 is probably the wrong granularity for you, you should be able to change that once you know how it works.
I found a free library on github (made by antiduh):
https://github.com/antiduh/ErrorCorrection
As it said:
A library to implement Reed-Solomon encoding. Reed Solomon is a method of encoding data with extra error correction information built in, so that errors in received data can be corrected without having to retransmit the data; this technique is also known as Forward Error Correction (FEC).

How to become an MSIL pro?

I have spent hours on a debugging problem only to have a more experienced guy look at the IL (something like 00400089 mov dword ptr [ebp-8],edx ) and point out the problem. Honestly, this looks like Hebrew to me - I have no idea what the heck it's saying.
Where can I learn more about this stuff and impress everyone around me? My goal is to read stuff like the following and make a comment like: Yeh, you are having a race condition.
.maxstack 2
.entrypoint
.locals init (valuetype [MathLib]HangamaHouse.MathClass mclass)
ldloca mclass
ldc.i4 5
That is not MSIL, it is assembly langauge 80x86.
To get great at IL, start with this fantastic article: Introduction to IL Assembly Language. Although it says "introduction", it's everything you need to start getting comfortable.
The other part of what you need is practice and lots of it. Use .NET Reflector and start looking at code disassembled into IL. (Tip: when you go to download it, you don't have to provide a real email.) Also, play with the Reflexil plugin in Reflector. Here's a good starting point for that: Assembly Manipulation and C# / VB.NET Code Injection.
Not necessary but a bonus: Reflexil is open source. You can get the source here.
I can give you an answer that runs both ways.
On the one hand, there's nothing like good assembly language skills to teach you how a computer really operates. MSIL is, to some extent, an assembly-like language. On the down side, there are very few opportunities to do this kind of development any more.
On the other hand, resorting to looking at the MSIL to fix a problem is not necessarily the most direct or educational way to understand a problem. In five years of .NET programming, I've never felt the need to go there. Only once has a co-worker (who had worked at Microsoft on compiler testing) gone there with a problem that I was trying to solve, and in the end, his answer was misleading, because the real issue was based in CLR design and constraints. Better knowledge of the CLR and C# would have led to a better understanding and a real solution.
(In case you're wondering, the problem was that I wanted to use "as" for safe casting with a generic. "as" didn't work, but "is" did. My co-worker noted that "is" and "as" use the same MSIL. The real problem is that "as" only works for casting classes, and without the proper constraint on the generic declaration, C# doesn't know whether your generic type will be a class. In fact, the types I was using with generics were value types; "as" couldn't work for those at all.)
Rather than going for MSIL skills, I highly recommend Jeffrey Richter's book CLR via C#. Even after years of digging hard at into C#, this book is still full of revelations - I learn something from every page.
Can’t say I’m an IL “pro”, but I managed to teach myself pretty much all of IL by doing the following:
Write a very short (two or three line) C# program that you are curious about how to write in IL.
Compile the program.
Open the compiled EXE in .NET Reflector.
Look at the IL code for the method in Reflector.
Hover your mouse over an IL opcode (e.g. “ldloc”). There is a tooltip describing each IL instruction.

Scriptable Windows Disassembler [non cygwin]

I'm currently trying to implement something that combines reverse engineering and graph theory. Therefore I'd like to disassemble PE binaries. There're some very sophisticated tools to do so, like IDA or w32dasm. Latter seems to be dead.
IDA is not scriptable - as far as I know.
The reason why I want a scriptable disassembler is, that I implement my program in C#. It gets a binary, and therefore it has to get the opcode somehow. I think I need to call some helping program with arguments. IDA cannot be called without GUI. It doesn't offer real cmdline options.
Any ideas?
Thanks,
wishi
IDA has a built-in scripting language called IDC. Lots of examples here. Also, IDA can be called without a GUI - consult the documentation for idaw.exe.
IDA can be scripted with Python. Version 5.5 even comes bundled with idapython.
[dumpbin /disasm](http://msdn.microsoft.com/en-us/library/xtf7fdaz(VS.71).aspx) should do the trick. You could also script CDB to do it.

Categories

Resources