dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Instantiating objects from user independent code.

  1. #1
    Join Date
    Jul 2017
    Posts
    78

    Instantiating objects from user independent code.

    I want to create a mechanism similar to what java and python does. In java and python, you can instantiate actual code (classes) that are not contained to the original code of your program and run it as it was hard-coded into you program without linking it to your actual program.

    For example lets say I have a program RunPlugin.exe that read's a config.txt file which contains paths to plugins (user defined code). Now lets say that a user creates a plugin MyPlugin by inheriting a special class which I provide (Lets name it PluginClass) in order to create his plugin. Also the user is adding in the config.txt the path of his source code.

    I want somehow my RunPlugin.exe for each source code path into the config.txt file to run the code of the user by instantiating an object of his MyPlugin class which inherits the PluginClass. I don't want the user to edit the source code of the RunPlugin.exe in order to link his code with the .exe program, i want some how to be done automatically by the program only by knowing the path of his source code or better the compiled one (maybe a dll file).

    But as far as I know, you need to edit the source code of the .exe program in order to include the class MyPlugin and instantiate an object of that classe and ofcource recompile the entire executable to link with the .dlls .lib file that the user is providing and for each plugin that a user is making he needs to update again the executable.

    Do you understand what I'm trying to achieve? I hope you do, I'm not sure if i explain what i need to do correctly

    I think this is similar of how an operating system is running the main() function of a program. That program is not part of the OS, neither you link it with the OS, but the OS somehow can call that main function and run the code inside it. How can i achieve that? How can i call a function from my program without link it with the code that has the function which i need to call?

    I found this on the internet and its exactly what i want to create:
    (If a moderator see this better change the title of my thread considering the document below because i'm sure i'm not asking what i was looking for.)

    The fact that most Java programs are written in this fashion is the result of Java's C and C++ heritage: Both C and C++ are statically linked languages, which means that all of the pieces that make up a C or C++ program must be present at the link step. Couple this with the fact that many Java programmers migrated from a C and/or C++ environment, and you can see why this type of programming is the norm. The thing is, in Java, we are not limited by the language in this way. Java, as you will soon see, allows programs to be dynamically extensible -- that is, to dynamically load in and execute new code, including code that wasn't in existence when the program was written. Sound far-fetched? It's not. What's more, I'm betting you use at least one program like this every day.
    Source: https://www.javaworld.com/article/20...lications.html


    Thanks!
    Last edited by babaliaris; November 29th, 2018 at 04:29 PM.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,843

    Re: Instantiating objects from user independent code.

    In C++, you can do this with a COM object or with a DLL.

  3. #3
    Join Date
    Jul 2017
    Posts
    78

    Re: Instantiating objects from user independent code.

    Quote Originally Posted by Arjay View Post
    In C++, you can do this with a COM object or with a DLL.
    Can you post some source where I can learn more about it? Thanks.

  4. #4
    Join Date
    Feb 2017
    Posts
    375

    Re: Instantiating objects from user independent code.

    What you're asking is usually called "dynamic loading". Here's a link to an implementation both for Windows and Linux,

    https://theopnv.com/dynamic-loading/

    A totally different approach (to making a C++ application user programmable) would be to integrate a macro language interpretator inside the application. It's a popular approach with games. I've considered trying it out using the Lua language.
    Last edited by wolle; November 30th, 2018 at 03:55 AM.

  5. #5
    Join Date
    Jul 2017
    Posts
    78

    Re: Instantiating objects from user independent code.

    Quote Originally Posted by wolle View Post
    What you're asking is usually called "dynamic loading". Here's a link to an implementation both for Windows and Linux,

    https://theopnv.com/dynamic-loading/

    A totally different approach (to making a C++ application user programmable) would be to integrate a macro language interpretator inside the application. It's a popular approach with games. I've considered trying it out using the Lua language.
    This is exactly what i was looking for, thanks! So the concept is to load a shared library manually through code and get the symbols (functions) which you want to use, instead of linking the library with the executable when you form it. Right?

  6. #6
    Join Date
    Feb 2017
    Posts
    375

    Re: Instantiating objects from user independent code.

    Quote Originally Posted by babaliaris View Post
    Right?
    Yes that's the idea. But note that it requires some quite advanced programming on your part. You graduate from application programmer to systems programmer. It also requires the users of your application to be quite advanced as well since they become part of the production of the shared libraries that your application will load at runtime.

    You posted a link to how dynamic loading can be done in Java and it probably can be done in a similar way using .NET too. Now if a C++ program is compiled as a C++/CLI program it becomes a .NET program. This would give the C++ program access to all .NET facilities including those necessary to accomplish dynamic loading, at least I think so.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,515

    Re: Instantiating objects from user independent code.

    Yes. For MS, you use LoadLibrary() which specifies the name of a .dll and GetProcAddress() to specify the name of a required function. The returned value from GetProcAddress() is then used as a function call. Both the name of the .dll for LoadLibrary() and the name of the function for GetProcAddress() can be run-time specified parameters.This is all wrapped up in a nice class in the provided link in post #4. As detailed in that link, the code for doing this is completely different for Linux and for MS but the underlying principle of run-time dynamic loading is the same.

    So for MS, the config.txt would contain the names of the .dll/function. Note that this method requires a pre-compiled library - not source code.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.4)

  8. #8
    Join Date
    Jul 2017
    Posts
    78

    Re: Instantiating objects from user independent code.

    Quote Originally Posted by 2kaud View Post
    As detailed in that link, the code for doing this is completely different for Linux and for MS but the underlying principle of run-time dynamic loading is the same..
    It's exactly the same, I tried it, only the names of the system calls are different and a little their usage.

    Combining this thread with the one I was asking about dlls and c++ classes, I thing I just moved to another whole level in programming. I just have one more question about exporting classe dlls with c factory functions (which I'm going to ask to another thread) and for now I'm done with dlls.

    This forum really helped me since I started using it!!!

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)