Article View: borland.public.cppbuilder.nativeapi
Article #15442Re: How to use LoadPackage in BCB ?
From: Clayton Arends
Date: Fri, 27 Jun 2003 19:19
Date: Fri, 27 Jun 2003 19:19
187 lines
5962 bytes
5962 bytes
Example 2: This enables you to design usable classes in your module that can
act as plug-ins for an existing interface.
Here is what I did:
- Create a new Package, saved as BasePackage
- Created a new unit, saved as BaseClass
- Added TBaseClass and supporting interfaces to unit
- Compiled (important to be able to add package below)
- Create a new Package, saved as Package1
- Added BasePackage.bpi to "requires" list
- Added vcl.bpi to "requires" list (necessary for call to ShowMessage())
- Created a new unit, saved as PackageUnit1
- Added TDerivedClass and supporting interface to unit
- Created a new Application, saved as Project1
- Saved the form unit as MainForm
- Added facility to load/unload the package
- Added call to create the object and call to virtual method
//-----------------------------------------------------------------------
// BaseClass.h
#ifndef BaseClassH
#define BaseClassH
class PACKAGE TBaseClass
{
private:
public:
TBaseClass();
virtual ~TBaseClass();
virtual void Foo()=0;
};
// Each package that derives from TBaseClass should export a function
// named 'CreateBaseClass' with this signature:
//
// extern "C" TBaseClass* __stdcall CreateBaseClass();
extern const char PACKAGE CreateBaseClass_FunctionName[];
typedef TBaseClass* __stdcall (*CreateBaseClass_fptr)();
#endif
//-----------------------------------------------------------------------
// BaseClass.cpp
#include <vcl.h>
#pragma hdrstop
#include "BaseClass.h"
#pragma package(smart_init)
const char CreateBaseClass_FunctionName[] = "CreateBaseClass";
TBaseClass::TBaseClass() {}
TBaseClass::~TBaseClass() {}
//-----------------------------------------------------------------------
// PackageUnit1.h
#ifndef PackageUnit1H
#define PackageUnit1H
#include "BaseClass.h"
class PACKAGE TDerivedClass : public TBaseClass
{
typedef TBaseClass inherited;
private:
public:
TDerivedClass();
virtual void Foo();
};
#endif
//-----------------------------------------------------------------------
// PackageUnit1.cpp
#include <vcl.h>
#pragma hdrstop
#include "PackageUnit1.h"
#pragma package(smart_init)
extern "C" TBaseClass* PACKAGE __stdcall CreateBaseClass()
{
return new TDerivedClass;
}
TDerivedClass::TDerivedClass() : inherited()
{
}
void TDerivedClass::Foo()
{
ShowMessage("Bar");
}
//-----------------------------------------------------------------------
// MainForm.h
#ifndef MainFormH
#define MainFormH
#include <Classes.hpp>
#include <Controls.hpp>
#include <Forms.hpp>
#include <StdCtrls.hpp>
class TForm1 : public TForm
{
__published:
TButton *Button1;
TButton *Button2;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private:
HMODULE fPackageModule;
public:
__fastcall TForm1(TComponent* Owner);
__fastcall ~TForm1();
};
extern PACKAGE TForm1 *Form1;
#endif
//-----------------------------------------------------------------------
// MainForm.cpp
#include <vcl.h>
#pragma hdrstop
#include "MainForm.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
#include "BaseClass.h"
TForm1 *Form1;
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
}
__fastcall TForm1::~TForm1()
{
if (fPackageModule)
{
UnloadPackage(reinterpret_cast <unsigned> (fPackageModule));
fPackageModule = 0;
}
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (!fPackageModule)
{
fPackageModule = reinterpret_cast <HMODULE>
(LoadPackage("Package1.bpl"));
if (!fPackageModule)
throw Exception("Could not load package");
}
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if (!fPackageModule)
throw Exception("Module hasn't been loaded yet");
CreateBaseClass_fptr CreateBaseClass =
reinterpret_cast <CreateBaseClass_fptr>
(::GetProcAddress(fPackageModule, CreateBaseClass_FunctionName));
if (!CreateBaseClass)
throw Exception("Could not find procedure");
TBaseClass* obj = CreateBaseClass();
obj->Foo();
delete obj;
}
Note that with this example you are forced to distribute supporting packages
such as vcl.bpl and all the rest that are used in your app and base package.
But, you receive the added benefit of being able to use object oriented
design.
Technical Details
Message-ID:
<3efc98b2$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-09!supernews.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<3ef2a233@newsgroups.borland.com> <3ef2aa44$1@newsgroups.borland.com> <3ef5c77e@newsgroups.borland.com> <3ef7a17a$1@newsgroups.borland.com> <3efb1fd9@newsgroups.borland.com> <3efc97c1$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,2a0bcef6d2779e50 X-Google-Attributes: gid10cc67,public X-Google-Thread: 102541,2a0bcef6d2779e50 X-Google-Attributes: gid102541,public X-Google-ArrivalTime: 2003-06-27 12:39:32 PST From: "Clayton Arends" <nospam_claytonarends@hotmail.com> Newsgroups: borland.public.cppbuilder.nativeapi,borland.public.cppbuilder.students References: <3ef2a233@newsgroups.borland.com> <3ef2aa44$1@newsgroups.borland.com> <3ef5c77e@newsgroups.borland.com> <3ef7a17a$1@newsgroups.borland.com> <3efb1fd9@newsgroups.borland.com> <3efc97c1$1@newsgroups.borland.com> Subject: Re: How to use LoadPackage in BCB ? Date: Fri, 27 Jun 2003 12:19:43 -0700 Lines: 189 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: 24.30.135.218 Message-ID: <3efc98b2$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1056741554 24.30.135.218 (27 Jun 2003 12:19:14 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-09!supernews.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail