🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: How to export a C++ object from DLL

#5870
From: Andrue Cope [TeamB]
Date: Tue, 28 Jun 2005 08:17
55 lines
2527 bytes
Exporting an object out of a DLL is possible but not generally
recommended. The problem is that the export is language and often
compiler specific. This means that by exporting objects you are
probably forcing clients to generate their code using the same compiler
that you used. This defeats one of the main purposes of DLLs.

The generally accepted way to 'export an object' is to provide a handle
based interface to it. You can either export an 'int' that the DLL uses
to index into an array:

// DLL
TObject * objectArray[ MaxObjects ];

int method_TObjectWibble( int Handle,
                          char const * info )
{
  return objectArray[ Handle ]->Wibble( info );
}

int method_TObjectWibbleCTOR()
{
  int freeHandle=getFreeSlot();

  if( freeHandle!=-1 )
    objectArray[ freeHandle ]=new TObject();

  return freeHandle;
}

Or export a pointer to the object but declare it as 'void *':

// DLL
int method_TObjectWibble( void * Handle,
                          char const * info )
{
  return reinterpret_cast<TObject *>( Handle )->Wibble( info );
}

void * method_TObjectWibbleCTOR()
{
  return new TObject();
}

The first solution is a bit slower and requires more 'infrastructure'
in the DLL but is easier to police.

The advantage of both methods is that you are exporting things that
every language understands and thus anyone can use your DLL. The
downside is that you lose the encapsulation to an extent but you can
always provide a wrapping object that the EXE writer can add to their
project to hide the API.
-- 
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Technical Details
Message-ID: <42c10791$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news-out.nntp.be!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <42c0c61f$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,6f086e2adea8beea
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Andrue Cope [TeamB]" <no.spam@not.a.valid.address>
Subject: Re: How to export a C++ object from DLL
Newsgroups: borland.public.cppbuilder.nativeapi
References: <42c0c61f$1@newsgroups.borland.com>
Date: Tue, 28 Jun 2005 09:17:20 +0100
User-Agent: XanaNews/1.17.5.7
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
NNTP-Posting-Host: 217.207.223.34
Message-ID: <42c10791$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1119946641 217.207.223.34 (28 Jun 2005 01:17:21 -0700)
Lines: 55
X-Authenticated-User: acope
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news-out.nntp.be!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail