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 design pure C style APIs for a framework which include a core exe and plugin dlls

How to design pure C style APIs for a framework which include a core exe and plugin dlls

Scheduled Pinned Locked Moved C / C++ / MFC
asp-netdesigntutorialquestion
4 Posts 3 Posters 1 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.
  • B Offline
    B Offline
    bestbear
    wrote on last edited by
    #1

    I developed an exe,which would exports some pure C APIs for plugin dlls.

    the code for the main exe is something like bellow:

    class CA{};
    class CB{};
    class CC{};

    class CCore
    {
    private:
    CA* m_pCA;
    CB* m_pCB;
    CC* m_pCC;
    };

    void main()
    {
    CCore* pCore = new CCore;
    while(true)
    {
    pCore->DoSomething();
    }
    delete pCore;
    }

    the code for a plugin is something like this

    class CTestPlugin : public IPlugin
    {
    public:
    virtual void Init()
    {
    //do something to change class CCore's behavior;maybe CA,CB,CC
    }
    virtual void UnInit()
    {
    }
    };

    I do not want to expose the pCore pointer or any other pointers from main exe to any plugin. so I need pure c style apis.
    any ideas?thank you.

    L L 2 Replies Last reply
    0
    • B bestbear

      I developed an exe,which would exports some pure C APIs for plugin dlls.

      the code for the main exe is something like bellow:

      class CA{};
      class CB{};
      class CC{};

      class CCore
      {
      private:
      CA* m_pCA;
      CB* m_pCB;
      CC* m_pCC;
      };

      void main()
      {
      CCore* pCore = new CCore;
      while(true)
      {
      pCore->DoSomething();
      }
      delete pCore;
      }

      the code for a plugin is something like this

      class CTestPlugin : public IPlugin
      {
      public:
      virtual void Init()
      {
      //do something to change class CCore's behavior;maybe CA,CB,CC
      }
      virtual void UnInit()
      {
      }
      };

      I do not want to expose the pCore pointer or any other pointers from main exe to any plugin. so I need pure c style apis.
      any ideas?thank you.

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

      See Exporting C Functions for Use in C or C++ Language Executables[^].

      1 Reply Last reply
      0
      • B bestbear

        I developed an exe,which would exports some pure C APIs for plugin dlls.

        the code for the main exe is something like bellow:

        class CA{};
        class CB{};
        class CC{};

        class CCore
        {
        private:
        CA* m_pCA;
        CB* m_pCB;
        CC* m_pCC;
        };

        void main()
        {
        CCore* pCore = new CCore;
        while(true)
        {
        pCore->DoSomething();
        }
        delete pCore;
        }

        the code for a plugin is something like this

        class CTestPlugin : public IPlugin
        {
        public:
        virtual void Init()
        {
        //do something to change class CCore's behavior;maybe CA,CB,CC
        }
        virtual void UnInit()
        {
        }
        };

        I do not want to expose the pCore pointer or any other pointers from main exe to any plugin. so I need pure c style apis.
        any ideas?thank you.

        L Offline
        L Offline
        leon de boer
        wrote on last edited by
        #3

        Just hold make an internal array of PCore pointers and pass them out an Index number ... call it a handle ... I guess a HPCORE. So they hold onto an index number and when they call a function the pass in the index number and you grab back the PCore pointer out of your array. That is what all the HANDLES are in Windows, they have a number it tells them nothing about what happens internally it just a index to a internal pointer that never comes out. So you code becomes more like Windows API and they can have multiple HPCore handles.

        void main()
        {
        HPCORE hPCore = CreateNewPCore();
        while(true){
        PCoreDoSomething(hPCore);
        }
        DestroyPCore(hPCore);
        }

        EDIT: Actually I am being slow do what windows does exactly pass back a pointer to the position in the internal array rather than the index as the handle. First saves having to use the number to traverse the array to get the pointer (so much much faster). Second you can test the handle being valid if you need because it must lie between PCoreArray[0] ... PCoreArray[Last] of your internal PCore pointer array. It's index position in the array which you only need to know when you dispose to empty the slot is straight forward

        Handle Index = (Handle - &PCoreArray[0])/sizeof(Pointer).

        So a handle is a PCore** .. that is a pointer to a pointer to a PCore but I would just typedef it as size_t and internally typecast it because you can check it via the above. So ...

        typedef size_t HPCORE;

        In vino veritas

        B 1 Reply Last reply
        0
        • L leon de boer

          Just hold make an internal array of PCore pointers and pass them out an Index number ... call it a handle ... I guess a HPCORE. So they hold onto an index number and when they call a function the pass in the index number and you grab back the PCore pointer out of your array. That is what all the HANDLES are in Windows, they have a number it tells them nothing about what happens internally it just a index to a internal pointer that never comes out. So you code becomes more like Windows API and they can have multiple HPCore handles.

          void main()
          {
          HPCORE hPCore = CreateNewPCore();
          while(true){
          PCoreDoSomething(hPCore);
          }
          DestroyPCore(hPCore);
          }

          EDIT: Actually I am being slow do what windows does exactly pass back a pointer to the position in the internal array rather than the index as the handle. First saves having to use the number to traverse the array to get the pointer (so much much faster). Second you can test the handle being valid if you need because it must lie between PCoreArray[0] ... PCoreArray[Last] of your internal PCore pointer array. It's index position in the array which you only need to know when you dispose to empty the slot is straight forward

          Handle Index = (Handle - &PCoreArray[0])/sizeof(Pointer).

          So a handle is a PCore** .. that is a pointer to a pointer to a PCore but I would just typedef it as size_t and internally typecast it because you can check it via the above. So ...

          typedef size_t HPCORE;

          In vino veritas

          B Offline
          B Offline
          bestbear
          wrote on last edited by
          #4

          thank you all very much!!!

          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