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

  • Pensu
    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.
  • Neeraj Sharma
    Neeraj Sharma
    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
    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
    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
    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
    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
    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
    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
    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 Singh
    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 😔
  • Tanu Singh
    Tanu Singh
    Ok is there any way to create a new file with .exe extension through coding??
  • [Prototype]
    [Prototype]
    CodeDOM is what you're looking for. More info: #-Link-Snipped-#
  • sookie
    sookie
    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
    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

You are reading an archived discussion.

Related Posts

Learning foreign languages is often considered as a plus point in our life, especially when you're joining an international corporate where use of the native language of that country enhances...
Is there any process or idea of converting moving physical load to produce electric energy depending upon the load acting on the device that converts?😐 For example; Lot of vehicles...
Can I get IS 14223 (all parts)?
The only vehicle that I have driven is an old Humber bicycle. So completely incompetent to comment on this: https://biggeekdad.com/2012/01/how-old-people-park-their-car/
It was in January that Facebook introduced its new advanced search feature called the Facebook Graph Search. Making the traditional search bar on all your Facebook pages look and act...