Sign in to follow this  
arithma

DLL and EXEs, Verify design

Recommended Posts

In my project, an imported DLL must call some function in the EXE. My solution was to create a function in the DLL that the EXE calls and provides a function pointer through. Is this OK? Is it guaranteed to always work? Is there a better solution?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
That doesn't sound good, I for one wouldn't recommend doing it like that.
Having a DLL rely on an EXE kinda defeats the idea of a DLL.
But if you really wanna do it that way, I guess I could see it working.

Share this post


Link to post
Share on other sites
If you want to be more OOPy, you could use an abstract class (THat's what I do for plugins).

In the header shared between the EXE and DLL:

class IApp
{
public:
IApp() {}
virtual ~IApp() {}

virtual bool DoSomething() = 0;
// Other methods here
};




In the EXE source:

class CApp : public IApp
{
public:
CApp();
virtual ~CApp();

virtual bool DoSomething() {/* Code here */}
};



And then you create a CApp, and pass it to the DLL, which takes it as an IApp.

Share this post


Link to post
Share on other sites
The reason for using DLL is that am not going to develop those. Check out the idea at http://aritma.genteq.net

Share this post


Link to post
Share on other sites
Quote:
Original post by arithma
In my project, an imported DLL must call some function in the EXE.
My solution was to create a function in the DLL that the EXE calls and provides a function pointer through. Is this OK? Is it guaranteed to always work? Is there a better solution?


This is pretty much exactly how callbacks happen in Windows, e.g. your window procedure usually lives in an .exe, that .exe calls into a .dll (user32.dll to be exact), passing a function pointer that points back into the .exe. Later on the .dll calls back into the .exe using that pointer.

Just make sure you explicitly specify your calling convention (cdecl, stdcall, that stuff) and you'll be fine.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this