🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.migration
Article #136

Re: Dll problem in CBC2007.

#136
From: Ed Mulroy [TeamB]
Date: Tue, 08 Jul 2008 23:00
135 lines
3860 bytes
Below is the source for a program and a DLL such as you are asking about, a 
program which provides no body for the templated function and a DLL that 
provides a body for the templated function specialized for the type used to 
call the function in the main program.

.  Ed

----------------------------------------------
#ifndef THEDLLH
#define THEDLLH

// header file used by both the DLL and the program using the DLL
class IntType
  {
public:
  IntType(int i = 0) : value(i)
    {
    }
  ~IntType()
    {
    }
  void swap(IntType & a)
    {
    value ^= a.value;
    a.value ^= value;
    value ^= a.value;
    }
  int Value() const
    {
    return value;
    }

private:
  int value;
  };


#ifdef __BUILDING_DLL
 #define __EXPORT_TYPE _declspec(dllexport)
#else
 #define __EXPORT_TYPE _declspec(dllimport)
#endif


__EXPORT_TYPE
template <class T>
void ClearContainer(T& x);

#endif

----------------------------------------------

// main program source

#include <windows.h>
#include "thedll.h"

void ShowContainer(const char * title, const IntType & c)
  {
  char buffer[128];

  wsprintf(buffer, "Container value is %d", c.Value());
  MessageBox(HWND_DESKTOP, buffer, title, MB_OK);
  }


int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  {
  IntType container(12);

  ShowContainer("Starting", container);
  ClearContainer(container);
  ShowContainer("After Calling ClearContainer", container);
  return 0;
  }

----------------------------------------------
// DLL source

#include <windows.h>

#define __BUILDING_DLL
#include "thedll.h"


int WINAPI DllEntryPoint(HINSTANCE, unsigned long, void*)
  {
  return 1;
  }


// specialization of ClearContainer for T == IntType
__EXPORT_TYPE template <>
void ClearContainer<IntType>(IntType& x)
  {
  IntType y;
  y.swap(x);
  };


// dummy function used to force compiler to create an
// instance of ClearContainer specialized for T == IntType
__EXPORT_TYPE void NeverCalled(IntType& x)
  {
  ClearContainer(x);
  }

----------------------------------------------

> Gene wrote in message
> news:486fa525$1@newsgroups.borland.com...
>
> Hello,
>
> I got a new problem for Dll calling from BCB2007. The Dll calling is ok in 
> BCB6. That code is as in below:
>
> In the Dll header:
>
> #ifdef __BUILDING_DLL
>  #define __EXPORT_TYPE __export
> #else
>  #define __EXPORT_TYPE __import
> #endif
>
> template <class T>
> void __EXPORT_TYPE ClearContainer(T& x) { T y; y.swap(x); };
>
>
> Then I include the header in my main cpp file, and call the function. The 
> Dll project build is ok, but when I try to build the main project. It says 
> "Link32 error: unresolved external function..." Does any one know what 
> causes that and what should I do to get around?
>
> Any suggestion will be appreciated.
Technical Details
Message-ID: <4873f1a6$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder.news-service.com!ecngs!feeder2.ecngs.de!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <486fa525$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 110f51,ff6960108b83e27b
X-Google-Attributes: gid110f51,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Ed Mulroy [TeamB]" <dont_email_me@bitbuc.ket>
Newsgroups: borland.public.cppbuilder.migration
References: <486fa525$1@newsgroups.borland.com>
Subject: Re: Dll problem in CBC2007.
Date: Tue, 8 Jul 2008 19:00:54 -0400
Lines: 137
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.5512
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5512
NNTP-Posting-Host: 69.142.0.27
Message-ID: <4873f1a6$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1215558054 69.142.0.27 (8 Jul 2008 16:00:54 -0700)
X-Authenticated-User: edmulroy
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder.news-service.com!ecngs!feeder2.ecngs.de!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail