-
June 16th, 2009, 11:25 PM
#1
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
-
June 16th, 2009, 11:54 PM
#2
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).
-
June 17th, 2009, 01:58 PM
#3
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
-
June 18th, 2009, 07:01 AM
#4
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).
-
June 19th, 2009, 01:20 PM
#5
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
-
June 19th, 2009, 02:47 PM
#6
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
-
June 20th, 2009, 08:27 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|