Implementing C# compiler (Online) like compilr.com - c#

Im working on creating a feature in a website, where people cah write C# code in the input box , and it can be compiled and results emitted.
Any resources or suggestions as to how to start with this.

The .Net framework has built in runtime compilers. They are in the System.CodeDom.Compiler namespace.
The other thing to consider is when you are compiling the code what assemblies you link in. If you link in an assembly the code you compile will have full access to that assembly and it will be compiled and running on your server.

Code compiled should be done in a sandbox. You can interface with the compiler through the command line.
http://msdn.microsoft.com/en-us/library/1700bbwd(v=vs.71).aspx

I'm using Microsoft.Build and Microsoft.Build.Framework in my online .Net IDE, Chpokk. Soon, Roslyn will be the way to go (I'm currently using it for Intellisense).

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;
}

error: mscorlib.dll assembly is not found during C++ and C# compile

Similar to this issue, I have MS VC 2013 Express and successfully installed .Net framework. I have a solution with code mixed between c++ and c# (with managed code enabled.)
I want to create a static library from my code. Now I can create a dynamic library without any problem, but I get this error message during compiling my code:
fatal error C1107: could not find assembly 'mscorlib.dll': please
specify the assembly search path using /AI or by setting the LIBPATH
environment variable
I added the lib and its path by hand, and all other options suggested by MS pages, but there is still no solution.
My questions:
Can even I have static lib with mixed code? How I can create static lib only from my C++ code which uses C# code?
(Furthermore, when I change from x86 to x64 in the project properties the Linker menu changes to Librarian, why?)
EDIT:
Finally I could solve this issue. First of all I also think to create only pure static lib is not possible. I changed to dynamic lib. As soon as I have more clarified info considering my question I write here.

How to compile c#/c++ files from Java Application

I am just looking into compilers and I was wondering is it possible to compile both c# and c++ files from a Java Application (e.g. to compile java from a java application you can use the JavaCompiler API). I have looked online for this but all i can find is ways to compile java files from c# and c++ and not the other way around.
If so, what API's can you use for this?
If you know the system commands for compiling and executing .cpp files(don't know much about c#) you might want to check out this. It details how to execute system commands from a Java program. Pass the system commands for compiling the required file in Runtime.getRuntime().exec().
Consider learning how to call ant from Java code and using something like this ant enhancement.
Disclaimer: I don't know anything about this product, but found it by searching for "can ant build c++?"
For C# in Windows: compiler (csc.exe) is part of .Net install on Windows and can be found at well known location (like %windir%\Microsoft.NET\Framework\v3.5 for .Net 3.5). The same place also contains MSBuild.exe that can build project files (*.csproj).
Your code may need to provide locations for referenced libraries if using Csc.exe to compile individual files.

Writing assembly code for the .Net platform

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

How do I allow my .net program to compile C# code while running?

I've seen a couple .net applications that allow you to select a C# .cs source file and the program itself will compile this code and run it.
How is this done?
It probably uses the CSharpCodeProvider class in the Microsoft.CSharp namespace. You would want to look at the following methods to compile code:
CompileAssemblyFromDom
CompileAssemblyFromFile
CompileAssemblyFromSource
CodeDOM is your friend
MSDN has a great series of articles explaining this...
http://msdn.microsoft.com/en-us/library/650ax5cx.aspx
My guess is by explicitly calling the csc.exe compiler. Keep in mind that this and any dependencies would have to be included with your deployment.
You can take a look at CS-Script which interprets C# files. Its free but not open sources though...

Categories