CrazyEngineers
  • Tanu
    Tanu

    MemberJul 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??
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Pensu

    MemberJul 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.
    Are you sure? This action cannot be undone.
    Cancel
  • Neeraj Sharma

    MemberJul 7, 2013

    Yes, the easiest thing to do will be to store that string in a file, then read that file using CS compiler..
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberJul 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
    Are you sure? This action cannot be undone.
    Cancel
  • simplycoder

    MemberJul 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.
    Are you sure? This action cannot be undone.
    Cancel
  • Tanu Singh

    MemberJul 7, 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?
    Are you sure? This action cannot be undone.
    Cancel
  • Tanu Singh

    MemberJul 7, 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?
    Are you sure? This action cannot be undone.
    Cancel
  • Tanu Singh

    MemberJul 7, 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.
    Are you sure? This action cannot be undone.
    Cancel
  • Tanu Singh

    MemberJul 7, 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.
    Are you sure? This action cannot be undone.
    Cancel
  • simplycoder

    MemberJul 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.
    #-Link-Snipped-#
    Are you sure? This action cannot be undone.
    Cancel
  • Tanu Singh

    MemberJul 8, 2013

    simplycoder
    You might want to read this.
    #-Link-Snipped-#
    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 😔
    Are you sure? This action cannot be undone.
    Cancel
  • Tanu Singh

    MemberJul 8, 2013

    Ok is there any way to create a new file with .exe extension through coding??
    Are you sure? This action cannot be undone.
    Cancel
  • [Prototype]

    MemberJul 8, 2013

    CodeDOM is what you're looking for. More info: #-Link-Snipped-#
    Are you sure? This action cannot be undone.
    Cancel
  • sookie

    MemberAug 4, 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
    Are you sure? This action cannot be undone.
    Cancel
  • Nayan Goenka

    MemberAug 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
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register