Writing assembly code for the .Net platform - c#

I'm a seasoned C# developer who wants, for fun, to write a bit of assembly code. I was wondering if it was easiest simply to write in byte code and somehow use the C# compiler, linker whatever. I'm a bit unsure on how to go about this.
Or maybe there is a decent assembly language out there with a step debugger and other nice things shipped in an environment that I should pick up instead?
I mainly want to write code to do calculations and output to the console window.

You can write IL code and compile it with ILASM

You can write it in MSIL and assemble it via the MSIL assembler (ilasm.exe).
As for a debugger, I've previously used DILE (DotNet IL Editor) to step through IL code, it's a bit out-dated though.
Seems that DILE is still being updated, check out the weekly builds.

you can use assembly language of .net environment which is called CIL
http://en.wikipedia.org/wiki/Common_Intermediate_Language

You can't use the C# compiler to write assembly code. However, you can you Visual Studio "CLR" projects which will compile native C/C++ with inline assembly blocks, which you can write a managed wrapper around so you can invoke via C#. See CLI/C++ Tutorials for more information.
You can also look at masm32 which you can use to write native x86 assembly libraries, then use p/invoke to execute them via C#.
Have fun!

This may be helpful to others
As for editing MSIL, we can dump assembly file to IL file by the ILDASM utility and recompile it to assembly file by the ILASM utility,
The two utilities are included in the .NET SDK.
C:\Program Files\Microsoft Visual Studio 8\VC>ildasm .
Open your assembly e.g. my.dll
After opening the file, select File->Dump. Ensure all checkboxes are selected and then click OK.
You will be prompted to save it as an IL file. I recommend creating a new directory to save as my.IL
Open my.IL by your favourite editing tool (I use Visual Studio .NET 2005/2003).
Edit your my.IL and then save it.
C:\Program Files\Microsoft Visual Studio 8\VC>ilasm /DLL X:\Folder\my.IL

Related

How do you compile C# code using mono embedded in C++?

How do you compile .cs files using C++
I have searched all through Mono's documentation and can't find a way to just compile C# code from the embedded mono runtime in C++. I know how to open a C# .exe assembly file using the embedded mono functions from C++, but I can't seem to find a way to just compile a .cs file to the .exe from C++.
I have also managed to compile the .cs files by calling the mcs.bat file from the CreateProcessA() function that Windows provides, however this does not give me a way to log errors or even check if it succeeded in compilation etc. (It also feels like a hack and not the official solution). The main reason I need to do this is so that I can recompile C# scripts on the fly by detecting when the source code has changed and another subset of conditions.
Does anyone know of a way to properly compile C# files using the embedded Mono runtime? And where to find the documentation for this? Currently I've been using the documentation here: http://docs.go-mono.com/?link=xhtml%3adeploy%2fmono-api-assembly.html which provides enough information for the most part.
Linking Mono in a DLL
Also, if you're familiar with embedding mono, do you know how to use it in a dll? I've managed to successfully link and compile it within a console application, but when I try to compile it as a part of a dynamic library, I get unresolved external symbol errors (specifically functions with the prefix __imp*).
Lastly, I'm using mono to embed C# as a scripting language for my game engine, however I don't know if there is a better (smaller) solution that I can use. If you know of any better solution feel free to leave a recommendation.
The mono runtime is a "Runtime", only for running the code,
but if you have installed the csc command then you can use this:
#include <cstdlib>
int main(){
system("csc yourfile.cs")
return 0;
}

How to recompile IL

So I need to make a very simple change to a DLL file.
I have successfully exported the dll file to IL language with Reflector, and have found the change I need to make (its just changing a URL). So if I make the change in notepad and save it, well thats great & easy, but now I cannot figure out how to build it back into a DLL file. I have tried to export it to a C# project which works, but building it gives me so many errors.
VS doesn't want to open it since exporting to IL doesn't provide a project file.
Just like VisualStudio ships with ildasm, it also ships with ilasm. So you should be able to do:
ilasm /dll myapp.il
Figured it out, works perfectly:
decompile: ildasm file.dll /out:file.il
edit using notepad
recompile: ilasm file.il /DLL /fold /nologo /noautoinherit
.. and of course you can use your favourite decompiler tool such as VS or dotpeek to view the code nicely, then find it in the dirty IL version to edit it.
I used Telerik JustDecompile. from a DLL I can create my project (right click on the assembly / create project c#/vb or IL), I open with VS, I change and I recompile.
Test it !

Is that possible to extract C# source code from Winforms application exe file

Can someone tell me if it is possible to view source code only using exe file. For example I have made project and in debug there is exe file so if I'll send this to someone he will be able to view it's source code(Using hacking tolls or by something)?
The answer is Yes
Your code can be de-compiled and can be seen.
I personally use this one for .net:
http://www.jetbrains.com/decompiler/
But it is not the only one, there are many decompilers exist.
Yep, there's Resharper or ILSpy
Certainly he/she can see your IL code, the code that has been constructed by your compiler. For instance, a tool for doing this is the MSIL Disassembler http://msdn.microsoft.com/en-us/library/f7dy01k1(v=vs.110).aspx. I suppose having the IL language it wouldn't be difficult to get the C# code.
You must use a decompiler as Net Reflector or JetBrains, anyway if your exe is obfuscated also them can not work
You can use the default ILDASM (IL disassembler) from VS command prompt.

Why am I able to debug my assembly?

I am working on an assembly that handles various color transformations. When I load the assembly into a new project to test, if there happens to be an bug in the assembly, Visual Studio opens the offending code from the DLL. I can step through all of the code in the assembly.
I definitely don't want the code to be so easily visible/available. I would like the code to be somewhat "locked" in the assembly.
How can I set the DLL to simply throw some sort of error instead of opening?
Edit
I'm not interested in the code being "safe" and I have no need to obfuscate. This library is being used internally and the code itself is perfectly accessible to tohers. What I don't want is for someone using the library to find themselves suddenly debugging the assembly. If there is a problem, I prefer to have an error thrown instead of the assembly code opening in Visual Studio.
This is happening because you have VS installed on the machine, and because you are deploying the PDB files - you will not get this dialogue box if VS is not installed.
Additionally:
Do not deploy code that has been built in the Debug configuration. These contain additional information that helps with debugging.
Make sure you do not deploy the PDB files with the executables. Same as above, and they are not needed for running the code.
Both these will help, but any assembly would be easily decompiled with reflector, so you may also want to investigate obfuscators to stop other programmers from easily seeing your code.
There is a list of C# obfuscators here : http://www.csharp411.com/net-obfuscators/
What you need is to obfuscate your binaries.
Basically if you want your code to be safe and you dont want your classes are exposed to others, you should definitely need to Obfuscate your code.
To obfuscate your code you can use DotFuscator, it is included with Visual Studio installation.
check my article on it.
http://www.codeproject.com/KB/dotnet/code_security.aspx

How to: compile generated C++ source files?

so I'm writing a VS2008 C# Add-In to automate AspectC++ weaving in C++ projects. I'm generating the C++ source files (now woven with aspects), but I can't figure out how to compile them as part of the pre-build step. Is there a convenient way to specify new source within the IDTExtensibility2, EnvDTE90, or VslangProj90 namespaces? I've tried using the VCProject and VCProjectEngine interfaces as well as marking the files for inclusion programmatically via the 'ExcludedFromBuild = false' flag. No luck.
I noticed that the commercial AspectC++ Add-In bypasses cl.exe by putting a wrapper around it and the ac++.exe aspect compiler. So they must call their own cl.exe which then calls ac++.exe before preparing the generated source files for the real compiler. That seems like a hack to me, is there not a better way? I'm really stumped on this one, any help would be appreciated.
Why not just include the generated file into the project that you then build?

Categories