Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Exporting/Importing variable from a Package


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Feb 22, 2018 1:02 PM Last Post By: James Williams
James Williams

Posts: 14
Registered: 6/9/99
Exporting/Importing variable from a Package  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 22, 2018 10:44 AM
Hello,

I am trying to export a shared instance of a class in a package BPL file as follows

.cpp of BPL

__declspec(dllexport) TMyClass * GSharedClass;

Then, in the exe application which will get access to this is like follows:

if(GSharedClass==NULL)
.... //Instantiate the instance

In the header file for the imported BPL I have tried
extern __delcspec(dllimport) TMyClass * GSharedClass;

However every time I tried to compile the .exe I get a linker error with an unresolved external.

I have included the link file .bpi of the library via a #pragma link "MySharedLibrary"

How can I import the shared "Variable" into my .exe "without" going through a exported function to get the variable?

Edited by: James Williams on Feb 22, 2018 10:44 AM
James Williams

Posts: 14
Registered: 6/9/99
Re: Exporting/Importing variable from a Package  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 22, 2018 12:16 PM   in response to: James Williams in response to: James Williams
I have resolved the issue with a simple variable being exported, like an int or even a class. However, it seems that namespaces creates the problem.

For example, in a BPL I can do the following

MyBPL.h

__declspec(dllexport) class TMyClass
{

};

extern __declspec(dllexport) TMyClass * GMyClass;

In the BPL.cpp

__declspec(dllexport) TMyClass * GMyClass;

In the header of the .exe main form .h I have
__declspec(dllimport) TMyClass * GMyGlass;

In my main form .cpp I have

if(GMyClass==NULL)
GMyClass = new TMyClass();

The above both imports and exports without an issue. Now, if I place the class into a namespace as the following examples, it does not link.

MyBPL.h

namespace TestNameSpace
{
__declspec(dllexport) class TMyClass
{

};

extern __declspec(dllexport) TMyClass * GMyClass;

};

In the BPL.cpp

using namespace TestNameSpace;
__declspec(dllexport) TMyClass * GMyClass;

In the header of the .exe main form .h I have
__declspec(dllimport) TMyClass * GMyGlass;

In my main form .cpp I have

if(TestNameSpace::GMyClass==NULL)
TestNameSpace::GMyClass = new TestNameSpace::TMyClass();

For some reason, I can't get a valid import when I place everything in a namespace. Is there something specific I need to do for imports/exports when dealing with namespaces?


James Williams wrote:
Hello,

I am trying to export a shared instance of a class in a package BPL file as follows

.cpp of BPL

__declspec(dllexport) TMyClass * GSharedClass;

Then, in the exe application which will get access to this is like follows:

if(GSharedClass==NULL)
.... //Instantiate the instance

In the header file for the imported BPL I have tried
extern __delcspec(dllimport) TMyClass * GSharedClass;

However every time I tried to compile the .exe I get a linker error with an unresolved external.

I have included the link file .bpi of the library via a #pragma link "MySharedLibrary"

How can I import the shared "Variable" into my .exe "without" going through a exported function to get the variable?

Edited by: James Williams on Feb 22, 2018 10:44 AM
James Williams

Posts: 14
Registered: 6/9/99
Re: Exporting/Importing variable from a Package  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 22, 2018 1:02 PM   in response to: James Williams in response to: James Williams
Ok,

So I figured out the issue with namespace. Basically, in my .cpp of the BPL I needed to do add the namespace reference to the variable name as follows:

In the BPL.cpp

using namespace TestNameSpace;
__declspec(dllexport) TMyClass * TestNameSpace::GMyClass;

This is required because the extern reference in the .h file was placed in the namespace section.

James Williams wrote:
I have resolved the issue with a simple variable being exported, like an int or even a class. However, it seems that namespaces creates the problem.

For example, in a BPL I can do the following

MyBPL.h

__declspec(dllexport) class TMyClass
{

};

extern __declspec(dllexport) TMyClass * GMyClass;

In the BPL.cpp

__declspec(dllexport) TMyClass * GMyClass;

In the header of the .exe main form .h I have
__declspec(dllimport) TMyClass * GMyGlass;

In my main form .cpp I have

if(GMyClass==NULL)
GMyClass = new TMyClass();

The above both imports and exports without an issue. Now, if I place the class into a namespace as the following examples, it does not link.

MyBPL.h

namespace TestNameSpace
{
__declspec(dllexport) class TMyClass
{

};

extern __declspec(dllexport) TMyClass * GMyClass;

};

In the BPL.cpp

using namespace TestNameSpace;
__declspec(dllexport) TMyClass * GMyClass;

In the header of the .exe main form .h I have
__declspec(dllimport) TMyClass * GMyGlass;

In my main form .cpp I have

if(TestNameSpace::GMyClass==NULL)
TestNameSpace::GMyClass = new TestNameSpace::TMyClass();

For some reason, I can't get a valid import when I place everything in a namespace. Is there something specific I need to do for imports/exports when dealing with namespaces?


James Williams wrote:
Hello,

I am trying to export a shared instance of a class in a package BPL file as follows

.cpp of BPL

__declspec(dllexport) TMyClass * GSharedClass;

Then, in the exe application which will get access to this is like follows:

if(GSharedClass==NULL)
.... //Instantiate the instance

In the header file for the imported BPL I have tried
extern __delcspec(dllimport) TMyClass * GSharedClass;

However every time I tried to compile the .exe I get a linker error with an unresolved external.

I have included the link file .bpi of the library via a #pragma link "MySharedLibrary"

How can I import the shared "Variable" into my .exe "without" going through a exported function to get the variable?

Edited by: James Williams on Feb 22, 2018 10:44 AM
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02