Re: Can Dll import functions from application?
"kathy" <yqin_99@yahoo.com> ha scritto nel messaggio
news:1187018434.053432.133670@r34g2000hsd.googlegroups.com...
I know usually application can call the functions exported from Dll. I
am curious can dll import functions from application?
If not, what is the way for application - dll 2 way communication?
Right now, it looks application activate the communication, dll can
not.
As others wrote, you can use "call-backs".
Call-backs can be implemented as pointers to functions, as others suggested,
but you may also use "call-back *interfaces*", if you want a more
object-oriented design (and IMHO a better type-safety, better than using the
type-*unsafe* LPVOID).
For example, you might define a call-back interface that the EXE will
implement.
The DLL will call the call-back interface methods to issue command to the
EXE.
e.g.:
// Callback interface to say "Hello".
// If the DLL wants to issue a say-hello command to
// the .EXE, it can call a method of this interface
class ISayHello
{
public:
virtual ~ISayHello();
// Implemented by the call-back object
virtual void SayHelloMessage() = 0;
... other methods if required
};
The DLL could export an "initialization" function, where the EXE can pass a
pointer to the call-back object, e.g.:
// Exported by the DLL.
// The EXE calls this function to register its callback object.
void DllRegisterHelloCallback( ISayHello * pHelloCallback );
The .EXE would implement the ISayHello callback interface, e.g.:
// CSayHello class implements ISayHello interface,
// and is defined in the EXE:
class CSayHello : public ISayHello
{
public:
CSayHello();
~CSayHello();
void SayHelloMessage();
...
};
void CSayHello::SayHelloMessage()
{
// ...Example:
CString msg;
msg = _T("Hello from ");
msg += m_name;
msg += _T(" !!");
// Hello from <name> !!
AfxMessageBox( msg );
}
And the EXE "registers" the call-back for the DLL:
// In the EXE:
CSayHello sayHello;
DllRegisterHelloCallback( &sayHello );
Imagine in the DLL there is a global pointer to the callback:
//
// In the DLL:
//
ISayHello * g_pSayHello;
void DllRegisterHelloCallback( ISayHello * pHelloCallback )
{
// Save the EXE callback, so we (the DLL) can use it later.
g_pSayHello = pHelloCallback;
}
When the DLL needs to call the SayHello provided by the EXE, the DLL will
use the callback interface, like so:
g_pSayHello->SayHelloMessage();
So, in this way, the DLL is requesting an action provided by the EXE.
So, to summarize:
1. The DLL provides:
1a. The definition of a call-back interface
1b. A function to register the call-back
2. The EXE provides:
2a. An implementation of the call-back interface
2b. Registers its call-back, calling the DLL exported registration
function
BTW: You might want (wisely) your DLL to have a COM-based object-oriented
interface, and in that case ISayHello could also be a COM interface, derived
from the usual IUnknown.
Giovanni