470,571 Members | 2,358 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,571 developers. It's quick & easy.

Howto make an instance of a class defined in a DLL

Hi together,
I made a DLL in the following way (simplified example just to explain
what is the problem) using devcpp and gcc compiler on Windows XP:
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 = LoadLibrary("dll.dll");
if (hMod==NULL) {printf("DLL NOT LOADED\n"); return 1} else printf("DLL
LOADED\n");

DllClass *pDllClass = (DllClass *) malloc (sizeof (DllClass));
if (NULL == pDllClass) {printf ("Memory allocation failed\n");return 1;}
else printf("Memory allocated\n");

typedef void (WINAPI * PCTOR) ();
PCTOR pDLLClass = (PCTOR) GetProcAddress (hMod, "_ZTV8DllClass");
if (NULL == 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/c...tedusingLL.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

--
Bernd MŁnt Durchwahl: 030/69032-509
euroscript Deutschland GmbH Zentrale: 030/69032-300
Abteilung IT-Management Fax: 030/69032-505
Alt-Moabit 91 Mail: Be*********@euroscript.de
10559 Berlin Web: http://www.euroscript.de

--
BM Computer-Services, Bergmannstr. 66, 10961 Berlin
Webdesign, Internet, Layout und Grafik
Tel.: 030/20649400, mobil 0175/7419517, Fax: 030/20649401
Web: http://www.bmservices.de, eMail: ko*****@bmservices.de
Aug 24 '06 #1
4 5119
[snip]
But now? How to make an instance of this class?
[snip]

Your problem will be much better addressed in other dedicated newsgroups
such as microsoft.public.vc.*

There are a number of solutions in my mind but bothering assembly is the
least you should try. So don't do it.

You can add another function in your DLL that instantiates the class and
returns a pointer to the instance. Make sure you use extern "C". In the
client code all you have to do is to call such a factory function to get
a pointer to an instance.

You probably need to provide another function for memory reclamation.

Ben
Aug 24 '06 #2
benben schrieb:
Your problem will be much better addressed in other dedicated newsgroups
such as microsoft.public.vc.*
No. I wrote that I am using devcpp with gcc on windows. The only example
I found was for MSVC.
There are a number of solutions in my mind but bothering assembly is the
least you should try. So don't do it.
That's my opion too.
You can add another function in your DLL that instantiates the class and
returns a pointer to the instance. Make sure you use extern "C". In the
client code all you have to do is to call such a factory function to get
a pointer to an instance.
Ok, I did it that way:
dll.h:
extern "C" DLLIMPORT DllClass* createDllClass();

dll.cpp:
DllClass* createDllClass() {
return new DllClass;
}

In the program using the dll:
typedef DllClass* (*PCreate) ();
PCreate createDC=(PCreate)::GetProcAddress (hMod,"createDllClass");
DllClass *pD=createDC();
if (cD) printf("Pointer\n");

That's working, I got a pointer to the newly created instance of the class.

And then further testing:
printf("var: %d\n",pD->var);

That's working, too. "var" is declared as an public integer var in the
class.

But now:
pD->test();

This does compile but the linker complains about an unresolved symbol to
the void test.
OK, as far as I understand: the library is loaded at run time and then
this symbol will be there. But not at link time.
As far as I know, gcc is not able to create something like a static
library ".lib" to use at link time.
What to do?

I also tried:
void (FAR __stdcall *(test))()=NULL;
test = (void (__stdcall *)())GetProcAddress (hMod,"_ZN8DllClass5testEv");
test();

But this is obviously not the pointer to the function inside the
instance. This is something like a pointer to a static member of my test
class similar to DllClass::test(); And this will not work. If there is
something like "var+=10;" inside the function test, the program crashes
becauce there is no memory allocated.

You probably need to provide another function for memory reclamation.
How? Can you give me an example?

Greetings, Bernd

--
BM Computer-Services, Bergmannstr. 66, 10961 Berlin
Webdesign, Internet, Layout und Grafik
Tel.: 030/20649400, mobil 0175/7419517, Fax: 030/20649401
Web: http://www.bmservices.de, eMail: ko*****@bmservices.de
Aug 25 '06 #3
Bernd Muent wrote:
benben schrieb:
>Your problem will be much better addressed in other dedicated
newsgroups such as microsoft.public.vc.*

No. I wrote that I am using devcpp with gcc on windows. The only example
I found was for MSVC.
You're still OT here. Try gnu.g++.help
Aug 25 '06 #4
Bernd Muent wrote:
benben schrieb:
>Your problem will be much better addressed in other dedicated
newsgroups such as microsoft.public.vc.*

No. I wrote that I am using devcpp with gcc on windows. The only example
I found was for MSVC.
Your argument is equivalent to:

"I went to the butcher, looking for fresh apples. They told me to go to
the fishmonger. The fishmonger didn't have apples, so I came back to
the butcher."

Try gnu.g++.help.
Aug 25 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Robert Ferrell | last post: by
11 posts views Thread by Helmut Jarausch | last post: by
8 posts views Thread by Topper | last post: by
13 posts views Thread by Ben Voigt [C++ MVP] | last post: by
10 posts views Thread by Steven W. Orr | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.