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