Can anyone help in coding?

Tanu Singh
@tanu-dSgR2C • Oct 25, 2024

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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Pensu

    @pensu-8tNeGU 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.

  • Neeraj Sharma

    @neeraj-iAaNcG Jul 7, 2013

    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-r2Hoep 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-NsBEdD 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-dSgR2C Jul 7, 2013

    PensuWhy 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.
    PensuWhy 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-dSgR2C Jul 7, 2013

    PensuWhy 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-dSgR2C Jul 7, 2013

    simplycoderIf 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-dSgR2C Jul 7, 2013

    Nick_SharmaYes, 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-NsBEdD 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.
    #-Link-Snipped-#

  • Tanu Singh

    @tanu-dSgR2C Jul 8, 2013

    simplycoderYou 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 😔

  • Tanu Singh

    @tanu-dSgR2C Jul 8, 2013

    Ok is there any way to create a new file with .exe extension through coding??

  • [Prototype]

    @prototype-G9Gn5k Jul 8, 2013

    CodeDOM is what you're looking for. More info: #-Link-Snipped-#

  • sookie

    @sookie-T06sFW Aug 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

  • Nayan Goenka

    @nayan-Dhpt4N 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