Article View: borland.public.cppbuilder.nativeapi
Article #10417Re: Creating multiple instances a class exported from a dynamically loaded DLL ?
From: Remy Lebeau (TeamB)
Date: Wed, 28 Apr 2004 17:14
Date: Wed, 28 Apr 2004 17:14
89 lines
3144 bytes
3144 bytes
<Tian> wrote in message news:408fb46a$1@newsgroups.borland.com...
> In my application how can I create MULTIPLE instances of
> a class exported from a DYNAMICALLY loaded (not adding
> the MyDLL.lib to the project) DLL ?
If you are dynamically loadinig the DLL, then the only way is to have the
DLL export additional functions that the application can call to create and
free instances of the class. There is no point in exporting the class in
the first place unless you us static linking.
For example:
--- MyDLL.h ---
#ifdef __dll__
#define IMPEXP __declspec(dllexport)
#else
#define IMPEXP __declspec(dllimport)
#endif // #ifdef __dll__
class TMyClass
{
//...
};
#ifdef __cplusplus
extern "C" {
#endif
TMyClass* IMPEXP __stdcall CreateMyClass();
void IMPEXP __stdcall FreeMyClass(TMyClass *cls);
#ifdef __cplusplus
}
#endif
--- MyDLL.cpp ---
#define __dll__
#include "MyDLL.h"
TMyClass* __stdcall CreateMyClass()
{
return new TMyClass;
}
void __stdcall FreeMyClass(TMyClass *cls)
{
delete cls;
}
--- MyApplication.cpp ---
#include <MyDLL.h>
typedef TMyClass* __stdcall (*LPF_CMC)();
typedef void __stdcall (*LPF_FMC)(TMyClass*);
void __fastcall TForm1::Button1Click(TObject *Sender)
{
HINSTANCE DLLHnd = DLLHnd = LoadLibrary("MyDLL.dll");
if( DLLHnd != NULL )
{
LPF_CMC lpCreateMyClass = (LPF_CMC) GetProcAddress(DLLHnd,
"CreateMyClass");
LPF_FMC lpFreeMyClass = (LPF_FMC) GetProcAddress(DLLHnd,
"FreeMyClass");
if( (lpCreateMyClass != NULL) && (lpFreeMyClass != NULL) )
{
TMyClass *A = lpCreateMyClass();
TMyClass *B = lpCreateMyClass();
TMyClass *C = lpCreateMyClass();
//...
lpFreeMyClass(A);
lpFreeMyClass(B);
lpFreeMyClass(C);
}
FreeLibrary(DLLHnd);
}
}
Gambit
Technical Details
Message-ID:
<408fe7dd$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<408fb46a$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,16595e758f137c2d X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2004-04-28 10:40:41 PST From: "Remy Lebeau \(TeamB\)" <gambit47.no.spam@no.spam.yahoo.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <408fb46a$1@newsgroups.borland.com> Subject: Re: Creating multiple instances a class exported from a dynamically loaded DLL ? Date: Wed, 28 Apr 2004 10:14:26 -0700 Lines: 91 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 67.22.138.253 Message-ID: <408fe7dd$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1083172829 67.22.138.253 (28 Apr 2004 10:20:29 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail