🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #15444

Re: How to use LoadPackage in BCB ?

#15444
From: Clayton Arends
Date: Fri, 27 Jun 2003 19:17
115 lines
4150 bytes
Example 1: This is little more than a glorified DLL

Here is what I did:
- Create a new Package, saved as Package1
- Created a new unit, saved as PackageUnit1
- Added function to unit

- Created a new Application, saved as Project1
- Saved the form unit as MainForm
- Added facility to load/unload the package
- Added facility to get and call the procedure

  //-----------------------------------------------------------------------
  // PackageUnit1.h
  #ifndef PackageUnit1H
  #define PackageUnit1H

  extern "C" String PACKAGE __stdcall GetSomeText();

  #endif

  //-----------------------------------------------------------------------
  // PackageUnit1.cpp
  #include <vcl.h>
  #pragma hdrstop

  #include "PackageUnit1.h"
  #pragma package(smart_init)

  extern "C" String __stdcall GetSomeText()
  {
    return "Hola";
  }

  //-----------------------------------------------------------------------
  // 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"

  typedef String (__stdcall *TGetSomeText)();

  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");

    TGetSomeText  fptr = reinterpret_cast <TGetSomeText>
      (::GetProcAddress(fPackageModule, "GetSomeText"));
    if (!fptr)
      throw Exception("Could not find procedure");

    ShowMessage(fptr());
  }

Note the use of extern "C" and __stdcall in the exported function.  extern
"C" removes name mangling and __stdcall removes the preceeding underscore.
Technical Details
Message-ID: <3efc9824$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!priapus.visi.com!news-out.visi.com!petbe.visi.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
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>

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:17:22 -0700
Lines: 117
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: <3efc9824$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1056741412 24.30.135.218 (27 Jun 2003 12:16:52 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!priapus.visi.com!news-out.visi.com!petbe.visi.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail