[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: <Ber...@eu...> - 2006-08-24 09:34:15
|
Hi together,
I made a DLL in the following way (simplified example just to explain =
what 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 =
being 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("DLL 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 =
printf ("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 years ago ;-)
Thank you for any tips how to get an instance of a class which is =
defined in a dll, Bernd
--=20
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
|