Article View: borland.public.cppbuilder.nativeapi
Article #8681Re: Instantiating and calling a class contained in a DLL
From: Dennis Jones
Date: Fri, 24 Sep 2004 23:05
Date: Fri, 24 Sep 2004 23:05
45 lines
2190 bytes
2190 bytes
<Roger Warren> wrote in message news:4154a05c$1@newsgroups.borland.com...
> Hello,
>
> I have the following class defined in TestDLLClass.h:
>
> class TestDLLClass
> {
> public:
> TestDLLClass();
> virtual ~TestDLLClass();
> virtual void TestMethod(AnsiString &s);
>
> };
To make a class exportable from a DLL, add a line like the following just
*before* the above class declaration:
class PACKAGE TestDLLClass;
class TestDLLClass
{
...
};
> The host executable uses the following to try and use the instance of
> TestDLLClass created with the dll's CreateInstanceOfTestDLLClass
> function:
>
> void __fastcall TfrmMain::Button1Click(TObject *Sender)
> {
> HANDLE h = LoadLibrary("../DLL/somedll.dll");
> TESTDLLCLASS CreateInstanceOfTestDLLClass =
> (TESTDLLCLASS)GetProcAddress(h, "_CreateInstanceOfTestDLLClass");
> TestDLLClass *ptrDLLClass = CreateInstanceOfTestDLLClass();
> ptrDLLClass->TestMethod("Hello");
> delete ptrDLLClass; <== *** DON'T DO THIS ***!!!
> FreeLibrary(h);
> }
Just as you export a function to create an instance of the class, so must
you also export a function to destroy an instance of the class. The DLL
must be responsible for allocating AND deleting the class instances.
- Dennis
Technical Details
Message-ID:
<4154a841$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.frii.net!newsfeed.frii.net!news.compaq.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<4154a05c$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,e81079fc94e5a83,start X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Dennis Jones" <nospam@bogus.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <4154a05c$1@newsgroups.borland.com> Subject: Re: Instantiating and calling a class contained in a DLL Date: Fri, 24 Sep 2004 16:05:36 -0700 Lines: 47 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1437 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441 NNTP-Posting-Host: 204.200.27.7 Message-ID: <4154a841$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1096067137 204.200.27.7 (24 Sep 2004 16:05:37 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.frii.net!newsfeed.frii.net!news.compaq.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail