View Feed
group-icon
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12892 Members
Join this group to post and comment.
Tanu Singh
Tanu Singh • Jul 7, 2013

Can anyone help in coding?

Is it possible to run a piece of code inputted by user??
Say in a c# program a user is asked to enter another c# code and we store that code say in a string(sugggest anything else better to store that code).Now we have to execute that program (inputed by user)?Is there any way??
Pensu
Pensu • Jul 7, 2013
Why don't you store the input in a file and then try to run that file. I haven't worked with C#, but you can give it a try.
Yes, the easiest thing to do will be to store that string in a file, then read that file using CS compiler..
Manish Goyal
Manish Goyal • Jul 7, 2013
There must be some way, in PHP and Javascript there is a function named eval that we can use to execute a string as a code
simplycoder
simplycoder • Jul 7, 2013
If I understand this correctly, then the technique is quiet similar to online compilers.
If you are trying to implement this, then be sure to check all boundary conditions, like time limit to terminate the program by force, checking for non-malicious code and etc.
Tanu Singh
Tanu Singh • Jul 8, 2013
Pensu
Why don't you store the input in a file and then try to run that file. I haven't worked with C#, but you can give it a try.
Pensu
Why don't you store the input in a file and then try to run that file. I haven't worked with C#, but you can give it a try.
Its fine that we store that code in a seperate file but how to execute that code in the same program?
Tanu Singh
Tanu Singh • Jul 8, 2013
Pensu
Why don't you store the input in a file and then try to run that file. I haven't worked with C#, but you can give it a try.
Its fine that we store that code in a seperate file but how to execute that code in the same program?
Tanu Singh
Tanu Singh • Jul 8, 2013
simplycoder
If I understand this correctly, then the technique is quiet similar to online compilers.
If you are trying to implement this, then be sure to check all boundary conditions, like time limit to terminate the program by force, checking for non-malicious code and etc.
I want to execute the inputted code..if that code has errors then the compiler should gives error mes as we normally get error mes when we compile a code.
Tanu Singh
Tanu Singh • Jul 8, 2013
Nick_Sharma
Yes, the easiest thing to do will be to store that string in a file, then read that file using CS compiler..
How to read that file by CS compiler??Actually i have to execute that code.
simplycoder
simplycoder • Jul 8, 2013
tanu!
I want to execute the inputted code..if that code has errors then the compiler should gives error mes as we normally get error mes when we compile a code.
You might want to read this.
https://www.dotnetperls.com/process-start
Tanu Singh
Tanu Singh • Jul 8, 2013
simplycoder
You might want to read this.
https://www.dotnetperls.com/process-start
All that is good but what i want is to work on console app and have to display the inputed code compilation in the same program. Might be i am not able to explain it the correct way 😔
Tanu Singh
Tanu Singh • Jul 8, 2013
Ok is there any way to create a new file with .exe extension through coding??
sookie
sookie • Aug 5, 2013
Are you looking for code like below in Java

Program to be compiled & executed

package myjava;
public class HelloWorld extends java.lang.Object{
    private HelloWorld(){
    }

    /**
     * [USER=14158]param[/USER] args
     * 
     */
    public static void main(String[] args) {
        System.out.println("Hello World executed !!");
    }

}
Program that will execute above program

package myjava;

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class CompileOneProgramFromAnother {

    /**

    * [USER=14158]param[/USER] args

    */

    public static void main(String[] args) {

        try {

            Runtime rt = Runtime.getRuntime();

            Process compileProcess = rt

                    .execute("javac D:\\MyJavaWorkspace\\Test\\src\\myjava\\HelloWorld.java");

            System.out.println("CompileOneProgramFromAnother | Program is compiled successfully !!");

            Process runProcess = rt

                    .execute("java -classpath D:\\MyJavaWorkspace\\Test\\src myjava.HelloWorld");

            InputStreamReader isr = new InputStreamReader(

                    runProcess.getInputStream());

            BufferedReader br = new BufferedReader(isr);

            String line = null;

            System.out.println("********************OUTPUT of HELLO WORLD.JAVA ***************\n");

            while ((line = br.readLine()) != null)

                System.out.println(line);

            System.out.println("\n********************END OUTPUT of HELLO WORLD.JAVA ***************");

            int exitVal = runProcess.waitFor();

            System.out.println("CompileOneProgramFromAnother | Process exitValue: " + exitVal);

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

Output:

CompileOneProgramFromAnother | Program is compiled successfully !!
********************OUTPUT of HELLO WORLD.JAVA ***************

Hello World executed !!

********************END OUTPUT of HELLO WORLD.JAVA ***************
CompileOneProgramFromAnother | Process exitValue: 0
Nayan Goenka
Nayan Goenka • Aug 5, 2013
Well it smells like injections to me. Are you trying to patch the script or make a vulnerable code?

Or if you want to run an external code in the same program, a good option is:

Learn about CSRF. we will do something like that
Now copy the input code on clipboard.
Create a CSRF link for an online compiler and the code to be input to be taken from the clipboard. Run the code there. then copy the output on clipboard and use it accordingly where required.

Another way also suggested by others is to accept the input in a text file. Now create a batch program which runs the code and stores output on clipboard. Then use the output accordingly.

tanu!
How to read that file by CS compiler??Actually i have to execute that code.
Create a batch file. That will help

Share this content on your social channels -