How to make member functions of an application accessible by a DLL?
-
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.
-
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.
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.
-
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.
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.
-
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.
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?
-
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.
In general you'll also export some kind of deleteObject function to avoid mixing heaps.
Steve
-
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.
-
In general you'll also export some kind of deleteObject function to avoid mixing heaps.
Steve
yep. good catch
-
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?
I meant forget about calling functions from your DLL to your main application.
-
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.
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); ...