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. How to make member functions of an application accessible by a DLL?

How to make member functions of an application accessible by a DLL?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++helptutorial
9 Posts 6 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.
  • A Offline
    A Offline
    Arris74
    wrote on last edited by
    #1

    Hello, If am correct a DLL can access member functions of an application (.EXE)if the implementation is done within the header file (.h) and not within the .cpp. Something like inline functions. Doing so may oblige us to make the source code visible since the header must be supplied with the application. So my question is how to do to avoid this and to make visible only the declaration of the functions. Many thanks for your kind help.

    C A L M 4 Replies Last reply
    0
    • A Arris74

      Hello, If am correct a DLL can access member functions of an application (.EXE)if the implementation is done within the header file (.h) and not within the .cpp. Something like inline functions. Doing so may oblige us to make the source code visible since the header must be supplied with the application. So my question is how to do to avoid this and to make visible only the declaration of the functions. Many thanks for your kind help.

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      one way to handle this is with what's known as the "PIMPL" pattern. PIMPL[^] there are many many ways to implement it. one way is with an abstract base class. share the abstract base class header, which defines the public interface, and includes a "create" function. then your implementation inherits from that base class. exported .h

      class baseClass
      {
      virtual int doCalc() = 0;
      };
      extern baseClass * createDerived();

      private .h

      class derivedClass : public baseClass
      {
      virtual int doCalc();
      };

      .cpp

      baseClass * createDerived()
      {
      return (baseClass *) new derivedClass();
      }
      int derivedClass::doCalc()
      {
      return 9;
      }

      or, with more of a C interface, using a 'handle' paradigm: .h

      typedef void * myClassHandle;
      extern myClassHandle createObject();
      extern int doCalc(myClassHandle hnd);

      .cpp

      myClassHandle createObject()
      {
      return (myClassHandle) new myClass();
      }
      int doCalc(myClassHandle hnd)
      {
      myClass * p = (myClass*)hnd;
      return p->doCalc();
      }

      etc.. basically, you put a layer between your class and the caller.

      image processing toolkits | batch image processing

      S 1 Reply Last reply
      0
      • A Arris74

        Hello, If am correct a DLL can access member functions of an application (.EXE)if the implementation is done within the header file (.h) and not within the .cpp. Something like inline functions. Doing so may oblige us to make the source code visible since the header must be supplied with the application. So my question is how to do to avoid this and to make visible only the declaration of the functions. Many thanks for your kind help.

        A Offline
        A Offline
        Albert Holguin
        wrote on last edited by
        #3

        I've already given you a solution... if you only want to pass data back and forth, use data structures instead of passing pointers to classes.

        A 1 Reply Last reply
        0
        • A Albert Holguin

          I've already given you a solution... if you only want to pass data back and forth, use data structures instead of passing pointers to classes.

          A Offline
          A Offline
          Arris74
          wrote on last edited by
          #4

          Albert, Thank you very much your help. Actually I also want the DLL calls the application's functions. So the idea was to pass a reference or a pointer of the class containing the functions. and to call the functions with that object from within the DLL. I do not know well how work structures. Do you mean that I can do so with a structure without being obliged to put the implementation of the functions in the header file?

          A 1 Reply Last reply
          0
          • C Chris Losinger

            one way to handle this is with what's known as the "PIMPL" pattern. PIMPL[^] there are many many ways to implement it. one way is with an abstract base class. share the abstract base class header, which defines the public interface, and includes a "create" function. then your implementation inherits from that base class. exported .h

            class baseClass
            {
            virtual int doCalc() = 0;
            };
            extern baseClass * createDerived();

            private .h

            class derivedClass : public baseClass
            {
            virtual int doCalc();
            };

            .cpp

            baseClass * createDerived()
            {
            return (baseClass *) new derivedClass();
            }
            int derivedClass::doCalc()
            {
            return 9;
            }

            or, with more of a C interface, using a 'handle' paradigm: .h

            typedef void * myClassHandle;
            extern myClassHandle createObject();
            extern int doCalc(myClassHandle hnd);

            .cpp

            myClassHandle createObject()
            {
            return (myClassHandle) new myClass();
            }
            int doCalc(myClassHandle hnd)
            {
            myClass * p = (myClass*)hnd;
            return p->doCalc();
            }

            etc.. basically, you put a layer between your class and the caller.

            image processing toolkits | batch image processing

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #5

            In general you'll also export some kind of deleteObject function to avoid mixing heaps.

            Steve

            C 1 Reply Last reply
            0
            • A Arris74

              Hello, If am correct a DLL can access member functions of an application (.EXE)if the implementation is done within the header file (.h) and not within the .cpp. Something like inline functions. Doing so may oblige us to make the source code visible since the header must be supplied with the application. So my question is how to do to avoid this and to make visible only the declaration of the functions. Many thanks for your kind help.

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

              There is no point of having a DLL if it needs to call back into the application in this way. You would be better just adding the source functions into your application project.

              The best things in life are not things.

              1 Reply Last reply
              0
              • S Stephen Hewitt

                In general you'll also export some kind of deleteObject function to avoid mixing heaps.

                Steve

                C Offline
                C Offline
                Chris Losinger
                wrote on last edited by
                #7

                yep. good catch

                image processing toolkits | batch image processing

                1 Reply Last reply
                0
                • A Arris74

                  Albert, Thank you very much your help. Actually I also want the DLL calls the application's functions. So the idea was to pass a reference or a pointer of the class containing the functions. and to call the functions with that object from within the DLL. I do not know well how work structures. Do you mean that I can do so with a structure without being obliged to put the implementation of the functions in the header file?

                  A Offline
                  A Offline
                  Albert Holguin
                  wrote on last edited by
                  #8

                  I meant forget about calling functions from your DLL to your main application.

                  1 Reply Last reply
                  0
                  • A Arris74

                    Hello, If am correct a DLL can access member functions of an application (.EXE)if the implementation is done within the header file (.h) and not within the .cpp. Something like inline functions. Doing so may oblige us to make the source code visible since the header must be supplied with the application. So my question is how to do to avoid this and to make visible only the declaration of the functions. Many thanks for your kind help.

                    M Offline
                    M Offline
                    MANISH RASTOGI
                    wrote on last edited by
                    #9

                    Export function from exe and import in dll. Below example is taken from http://www.codeguru.com/cpp/w-p/dll/article.php/c3649 In the EXE: // Do exactly as you would export a DLL... extern "C" { EXPORT void ExeFn(char * lpszMessage) { MessageBox(NULL,lpszMessage,"From Exe",MB_OK); } } In the DLL: ... // Get the handle of the EXE that loaded us. FnPtrT FnPtr = (FnPtrT)::GetProcAddress(GetModuleHandle(NULL), "ExeFn"); if(FnPtr) (*FnPtr)("Message From The DLL"); else MessageBox(NULL,"It Did Not work :(","From DLL",MB_OK); ...

                    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