Calling methods of the main program from run time loaded Dll
Question: I have a (big and complex) C++ program with its own classes and
methods.(obviously)

I would like to load at runtime a dll. From the main program i want to call a
function inside the dll passing a class instance , and this function will use
the method of this class.

In pseudo code

Main program:

    
    
     class MyClass{
           myattr1();
           myattr2();
           mymethod1();
           mymethod2();
     }
    
     void main(){
          MyClass* object = &(new MyClass())
          handle_of_the_dll = *some function that load the dll*
          dllfunc = getfunc(handle_of_the_dll, "interesting_function")
          dllfunc(object)
          [...etc...]
    

and the dll

    
    
    #ifdef __cplusplus
    extern C {
    #endif
    #ifdef BUILD_DLL
    #define EXPORT __declspec(dllexport)
    #else
    #define EXPORT __declspec(dllimport)
    #endif
    
    #include all the needed headers from main program, where the class is defined
    
    EXPORT void  interesting_functionn(MyClass object){
      object.mymethod1();
    }
    #ifdef __cplusplus
    }
    #endif
    

Is this valid/doable/right?

# EDIT:

i know that this would be a poor design for a general program, but this method
is intended to give the users the ability to use their custom dll that can
access the api of the main program. Something like Cpython modules


Answer: It is valid and doable. Generate a sample DLL in visual C++ and the code will
have all the bases covered. Replace the static linking in the client with
`LoadLibrary` and `GetProcAdress`.

Name mangling could be an issue,
[DependencyWalker](http://www.dependencywalker.com/) will help you there.

Keep a low profile with emory management. For example, if you use a memory
manager on one side but not on the other, your begging for trouble.

