🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: __declspec(dllimport) in bpl

#3618
From: Remy Lebeau (TeamB)
Date: Mon, 22 May 2006 17:24
108 lines
3425 bytes
"Dmitry Konnov" <dmitry@weblinedesign.biz> wrote in message
news:447171ab@newsgroups.borland.com...

> In bpl code I declare methods implemented in main executable like this:
>
> extern "C"  TComponent * __declspec(dllimport) MyExeMethod();
>
> When I compiling bpl,  I get linker error
> Unresolved external MyExeMethod() refernced from ....

You can't import functions from the application into the package like that.
You have two choices:

1) declare the functions as __declspec(export) in the application, and then
have the package obtain them via LoadLibrary() and GetProcAddress() at
runtime.  Even though it is not a DLL, an application executable can
actually be loaded via LoadLibrary(), and will have a suitable exports table
for GetProcAddress() to find.

2) have the package export a function that the application can call to pass
its function pointers to the package programmably.  This is the preferred
way to share functions from an application to a DLL.  For example:

    --- MyDll.h ---

    #ifndef MyDllH
    #define MyDllH

    #ifdef _BUILDING_DLL_
    #define MY_EXPORT __declspec(dllexport)
    #else
    #define MY_EXPORT __declspec(dllimport)
    #endif

    typedef TComponent* __fastcall (*MyExeMethod)();

    #pragma pack(push, 1)
    struct sMyFunctions
    {
        MyExeMethod Method1;
        //...
    };
    #pragma pack(pop)

    #ifdef __cplusplus
    extern "C" {
    #endif

    MY_EXPORT void __stdcall SetDllFunctions(sFunctions *pFuncs);

    #ifdef __cplusplus
    }
    #endif

    #endif


    --- MyDll.cpp ---

    #define _BUILDING_DLL_
    #include "MyDll.h"

    sMyFunctions Funcs = {0};

    void __stdcall SetDllFunctions(sFunctions *pFuncs)
    {
        if( pFuncs )
            memcpy(&Funcs, pFuncs, sizeof(Funcs));
        else
            memset(&Funcs, 0, sizeof(Funcs));
    }

    void DoSomething()
    {
        TComponent *pComp = NULL;

        if( Funcs.Method1 )
            pComp = Funcs.Method1();

        // use pComp as needed ...
    }

    --- MyApp.cpp ---

    #include "MyDll.h"

    TComponent* __fastcall MyMethod()
    {
        // do something ...
        return SomeObject;
    }

    __fastcall TForm1::TForm1(TComponent *Owner)
    {
        sMyFunctions funcs = {0};
        funcs.Method1 = &MyMethod;

        SetDllFunctions(&funcs);
    }

    __fastcall TForm1::~TForm1()
    {
        SetDllFunctions(NULL);
    }


Gambit
Technical Details
Message-ID: <4471f45e$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <447171ab@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,a1cb33bc02ad1dec
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <447171ab@newsgroups.borland.com>
Subject: Re: __declspec(dllimport)  in bpl 
Date: Mon, 22 May 2006 10:24:13 -0700
Lines: 110
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <4471f45e$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1148318814 68.69.17.41 (22 May 2006 10:26:54 -0700)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail