CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2009
    Posts
    3

    Use Function from text file

    is it possible to use a function in a text file, compile it at runtime and use it in a c++ program
    if so could you please show me an example
    and way to do this would help

    thanks in advance

  2. #2
    Join Date
    Nov 2006
    Posts
    1,611

    Re: Use Function from text file

    Not in the way your question implies.

    What I infer is that you mean to compile C++ code from a text file and then execute the result from within a C++ program. There's no practical way to do that. The primary reason is that it is the operating system's job to keep you from "executing" RAM which is been allocated from the heap or on the stack which was not previously 'blessed' by the operating system as code. There are 'tricks' you can pull, but the act is prone to trouble and quite likely the origin of security vulnerabilities.

    What you can do, however, is to incorporate the facility of another runtime language from within C++. There are libraries that allow you to accept code from users in Python, Java, Ruby or other languages like them, such that the user can supply code for execution from within your application. This is similar to what MS did for Visual Basic for Applications, what Javascript is for web pages, what LISP is for AutoCAD, etc. The subject is too vast and the options too wide to detail here, and I have no links beyond what you would find with Google on the subject. However, the general idea is that you're incorporating an interpreter/runtime in your application that can load code for the runtime from a file.
    Last edited by JVene; June 17th, 2009 at 10:12 AM.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  3. #3
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: Use Function from text file

    If you had access to a compiler, could you invoke the compiler on the text file to produce a DLL, then load the DLL and execute a function from it?
    Old Unix programmers never die, they just mv to /dev/null

  4. #4
    Join Date
    Nov 2006
    Posts
    1,611

    Re: Use Function from text file

    Sure.

    You could even send the text over the web to a server farm for compilation, download the result and load that as a DLL.

    Knowing if it works, if it's reasonably secure, if it contains infinite loops or process crashing memory corrupting bugs - that's another story.

    Frankly, this is one of the few areas where I can see a reason to consider something like .NET mixed language work (and here comes a legion of C# fans ready to pounce on me for disfavoring the Monarch's product). While you'd still need a compiler for C#, it's the kind of language you just might be able to trust for user supplied code.


    That Latin, is it roughly "Don't break it unless you're willing to fix it"?
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  5. #5
    Join Date
    Jun 2009
    Posts
    3

    Re: Use Function from text file

    could you give a code example or a link to an example of how to load a dll at runtime and execute one of its functions
    thanks

  6. #6
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: Use Function from text file

    Take a look at this article and the links underneath, particularly this one.
    Old Unix programmers never die, they just mv to /dev/null

  7. #7
    Join Date
    Sep 2007
    Posts
    62

    Re: Use Function from text file

    ...consider something like .NET mixed language work
    Check out : Wrapping Windows APIs with C++/CLI

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured