Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Dynamically Creating code and executing it

Dynamically Creating code and executing it

Scheduled Pinned Locked Moved C / C++ / MFC
c++pythontutorial
10 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    slacker
    wrote on last edited by
    #1

    Does anyone know of a way (I would guess it would use macro's) to execute a string which represents a line of code I want to execute. example: CString strCode = _T("int nVariable = 3;"); -And now execute it. I know it could be done with interpreted languages like python, but I don't know about C++. -JD

    R R B R C 7 Replies Last reply
    0
    • S slacker

      Does anyone know of a way (I would guess it would use macro's) to execute a string which represents a line of code I want to execute. example: CString strCode = _T("int nVariable = 3;"); -And now execute it. I know it could be done with interpreted languages like python, but I don't know about C++. -JD

      R Offline
      R Offline
      Rickard Andersson20
      wrote on last edited by
      #2

      What do you mean "execute it"? Show it in a dialog box? Show the string in a dialog box (MFC): AfxMessageBox(strCode); or Win32 ::MessageBox(hWnd,strCode,"Title", MB_OK); ------------------------------ ©0d3 ©®4©k3® - That's me! :) ------------------------------

      1 Reply Last reply
      0
      • S slacker

        Does anyone know of a way (I would guess it would use macro's) to execute a string which represents a line of code I want to execute. example: CString strCode = _T("int nVariable = 3;"); -And now execute it. I know it could be done with interpreted languages like python, but I don't know about C++. -JD

        R Offline
        R Offline
        Ravi Bhavnani
        wrote on last edited by
        #3

        I don't see how it could be done in a compiled language like C++. However, if you can move your dynamic code to a different language (eg: Tcl), you could load an interpreter from within your C/C++ code and execute the code within the interpeter (eg: by using TclEval()). /ravi "There is always one more bug..." ravib@ravib.com http://www.ravib.com

        1 Reply Last reply
        0
        • S slacker

          Does anyone know of a way (I would guess it would use macro's) to execute a string which represents a line of code I want to execute. example: CString strCode = _T("int nVariable = 3;"); -And now execute it. I know it could be done with interpreted languages like python, but I don't know about C++. -JD

          B Offline
          B Offline
          Brian V Shifrin
          wrote on last edited by
          #4

          You need to implement WSH host scripting. Then you should be able to use VBScript/Javascript to execute code, fire events etc... See http://www.codeguru.com/atl/ATL\_ScriptHost.shtml

          1 Reply Last reply
          0
          • S slacker

            Does anyone know of a way (I would guess it would use macro's) to execute a string which represents a line of code I want to execute. example: CString strCode = _T("int nVariable = 3;"); -And now execute it. I know it could be done with interpreted languages like python, but I don't know about C++. -JD

            R Offline
            R Offline
            Rick York
            wrote on last edited by
            #5

            It can be done but it is a HUGE amount of work and, in most cases, not worth the effort. I implemented an on-the-fly machine code generator for the script engine in my company's product and it was not an easy task at all. This is similar to what JIT compilers for Java do. I actually know very little about assembly code. I just stared at a whole bunch of .cod files emitted by VC++ and sorted out how it works. Curiously, I found that VC++ only uses three data registers to do everything. Another curiosity is that the this pointer for C++ methods is not passed on the stack. It is loaded into ECX prior to the call. Anyway, What it amounts to is you build a block of bytes of data that are machine code and set a pointer to a function to the address of the block and (*execute_it). I think an interpreted language is probably the easiest way to go about this and there are many of them.

            L 1 Reply Last reply
            0
            • S slacker

              Does anyone know of a way (I would guess it would use macro's) to execute a string which represents a line of code I want to execute. example: CString strCode = _T("int nVariable = 3;"); -And now execute it. I know it could be done with interpreted languages like python, but I don't know about C++. -JD

              C Offline
              C Offline
              Carlos Antollini
              wrote on last edited by
              #6

              You need an interpreter of commands, I made a interpreter for FTP a couple of years ago, it's the only solution... Best Regards... Carlos Antollini. Sonork ID 100.10529 cantollini

              1 Reply Last reply
              0
              • R Rick York

                It can be done but it is a HUGE amount of work and, in most cases, not worth the effort. I implemented an on-the-fly machine code generator for the script engine in my company's product and it was not an easy task at all. This is similar to what JIT compilers for Java do. I actually know very little about assembly code. I just stared at a whole bunch of .cod files emitted by VC++ and sorted out how it works. Curiously, I found that VC++ only uses three data registers to do everything. Another curiosity is that the this pointer for C++ methods is not passed on the stack. It is loaded into ECX prior to the call. Anyway, What it amounts to is you build a block of bytes of data that are machine code and set a pointer to a function to the address of the block and (*execute_it). I think an interpreted language is probably the easiest way to go about this and there are many of them.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                > Another curiosity is that the this pointer for C++ methods > is not passed on the stack. It is loaded into ECX prior to > the call.    Just an FYI: IIRC, that is the "thiscall" calling convention.    Peace! -=- James.

                R 1 Reply Last reply
                0
                • L Lost User

                  > Another curiosity is that the this pointer for C++ methods > is not passed on the stack. It is loaded into ECX prior to > the call.    Just an FYI: IIRC, that is the "thiscall" calling convention.    Peace! -=- James.

                  R Offline
                  R Offline
                  Rick York
                  wrote on last edited by
                  #8

                  Yes, I know it is the thiscall convention. What is amusing is that the VC++ docs describe this correctly in one place and incorrectly in another. Here is the wrong description : This is the default calling convention used by C++ member functions that do not use variable arguments. The callee cleans the stack, so the compiler makes vararg functions __cdecl, and pushes the this pointer on the stack last. I have seen many people refer to this being pushed on the stack and that is not what VC does. There may be some compilers that do though. I don't know for sure.

                  1 Reply Last reply
                  0
                  • S slacker

                    Does anyone know of a way (I would guess it would use macro's) to execute a string which represents a line of code I want to execute. example: CString strCode = _T("int nVariable = 3;"); -And now execute it. I know it could be done with interpreted languages like python, but I don't know about C++. -JD

                    R Offline
                    R Offline
                    Roger Wright new
                    wrote on last edited by
                    #9

                    What fun! I designed and programmed ATE to test microcircuits, functional and in-circuit, and a cute little HP9825 desktop calculator ran the whole thing. You typed code into it's 64K memory, then saved the program on an 8" floppy disk. It's native language was hpl, which quickly expired because nobody liked a funny looking language (it used lower case). I liked it because one of the storable functions was the <Store> key. Once stored, the characters were treated as a command. That let me write programs that modified their behavior to adapt to the environment (and I mean physical - EMI, temp, etc.) and increase the accuracy of measurements on the circuit. It drove the QC people nuts to have programs around that kept changing themselves:cool: Let's bring back hpl!!!!

                    1 Reply Last reply
                    0
                    • S slacker

                      Does anyone know of a way (I would guess it would use macro's) to execute a string which represents a line of code I want to execute. example: CString strCode = _T("int nVariable = 3;"); -And now execute it. I know it could be done with interpreted languages like python, but I don't know about C++. -JD

                      T Offline
                      T Offline
                      Todd Smith
                      wrote on last edited by
                      #10

                      You have to embed an interpreter in your program. There are plenty out there that are free and can be integrated into a C++ program. http://www.lua.org/

                      Todd Smith

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups