Re: [Dev-C++] DLL: Howto make an instance of a class defined in a DLL
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2006-08-24 11:22:37
|
The easiest way to instantiate classes defined in a DLL is to give the DLL
file a factory function, i.e. a function that returns new DllClass(xxx);
The naming of exported symbols is called name mangling. All C++ compilers
uses name mangling (but not identical) to facilitate type-safe linking.
The symbols are decorated with a number of extra characters that tells
data types, return values, parameters, calling convention, code generation
etc.
In your example code, you are using malloc. C++ program should normally
use the new operator instead of malloc.
/Per W
On Thu, 24 Aug 2006, M=FCnt, Bernd wrote:
> Hi together,
> I made a DLL in the following way (simplified example just to explain wha=
t is the problem):
> dll.h:
> #ifndef _DLL_H_
> #define _DLL_H_
>
> #if BUILDING_DLL
> # define DLLIMPORT __declspec (dllexport)
> #else /* Not BUILDING_DLL */
> # define DLLIMPORT __declspec (dllimport)
> #endif /* Not BUILDING_DLL */
>
> class DLLIMPORT DllClass
> {
> public:
> DllClass();
> virtual ~DllClass(void);
> void testVoid();
> };
> -------------------------------------------------
> dll.cpp:
>
> #include "dll.h"
> #include <windows.h>
> #include <iostream>
>
> DllClass::DllClass() {}
> DllClass::~DllClass () {}
> void DllClass::test() {
> printf("testFunction called!\n");
> }
>
> BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */=
,
> DWORD reason /* Reason this function is bei=
ng called. */ ,
> LPVOID reserved /* Not used. */ ) {
> return true;
> }
> -------------------------------------------------
>
> So far, so good, I could compile it with gcc and got a dll.dll as output.
> dumpbin /exports dll.dll gives the following output:
> ordinal hint RVA name
>
> 1 0 000011D0 _Z5testFv
> 2 1 000011F0 _ZN8DllClass4testEv
> 4 2 00001250 _ZN8DllClassC1Ev
> 5 3 00001230 _ZN8DllClassC2Ev
> 6 4 00001290 _ZN8DllClassD0Ev
> 7 5 00001280 _ZN8DllClassD1Ev
> 8 6 00001270 _ZN8DllClassD2Ev
> 9 7 00041C9C _ZTV8DllClass
>
> So, the Class and ist included void are exported.
> [Question beside: Why have the symbols such cryptic names. Do I have any =
influence to rename them just to "DllClass" etc.?]
>
> No I made a host application:
> main.cpp:
> -------------------------------
> #include <cstdlib>
> #include <iostream>
> #include <windows.h>
> #include "dll.h"
>
> using namespace std;
>
> int main(int argc, char *argv[]) {
> HMODULE hMod =3D LoadLibrary("dll.dll");
> if (hMod=3D=3DNULL) {printf("DLL NOT LOADED\n"); return 1} else printf("D=
LL LOADED\n");
>
> DllClass *pDllClass =3D (DllClass *) malloc (sizeof (DllClass));
> if (NULL =3D=3D pDllClass) {printf ("Memory allocation failed\n");return =
1;} else printf("Memory allocated\n");
>
> typedef void (WINAPI * PCTOR) ();
> PCTOR pDLLClass =3D (PCTOR) GetProcAddress (hMod, "_ZTV8DllClass");
> if (NULL =3D=3D pDLLClass) {printf ("GetProcAddress failed\n");} else pr=
intf ("DLL ProcAddress success\n");
> -------------------------------------
> But now? How to make an instance of this class?
> I found this hint:
> https://secure.codeproject.com/dll/classesexportedusingLL.asp
> __asm { MOV ECX, pDLLClass }
> pDLLClass();
>
> But the assember syntax is for Visual C++, not working with gcc.
> I tried:
> asm("MOV ECX, pDLLClass");
> But this does not compile:
> "too many memory references for `mov'"
>
> Sorry, I've got no experience in programming assembler on a X86 machine, =
the last time I did assembler programming was on a ZX81 von VC 64 some 20 y=
ears ago ;-)
>
> Thank you for any tips how to get an instance of a class which is defined=
in a dll, Bernd
>
> --
> Bernd M=FCnt Durchwahl: 030/69032-509
> euroscript Deutschland GmbH Zentrale: 030/69032-300
> Abteilung IT-Management Fax: 030/69032-505
> Alt-Moabit 91 Mail: Ber...@eu...
> 10559 Berlin Web: http://www.euroscript.de
>
> -------------------------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job ea=
sier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronim=
o
> http://sel.as-us.falkag.net/sel?cmd=3Dlnk&kid=3D120709&bid=3D263057&dat=
=3D121642
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|