Solved

Export instance of class into dll

Posted on 1998-07-09
8
296 Views
Last Modified: 2010-04-10
I want to export global variable that is instance of some class into dll. I have tried usual technique:
 __declspec(dllexport) CMyClass MyInst; // declaration in .exe
 __declspec(dllimport) CMyClass MyInst; // declaration in .dl
but I get link error (error LNK2001: unresolved external symbol "class CMyClass  MyInst").
If I try do the same with simple type ( like int or void* ) it succedes. Is it possible to export
instance of class into dll and if it is - how?
Thanx for any help.
Platform: VC5/NT4 ( not using MFC )
0
Comment
Question by:tulin
  • 5
  • 2
8 Comments
 
LVL 8

Expert Comment

by:trestan
ID: 1167506
From the error message, it seems that you did not include the file contain the class, did you?
0
 
LVL 22

Accepted Solution

by:
nietod earned 70 total points
ID: 1167507
Yes it is possible, but the class must be exported as well as the instance.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1167508
for exmple you could have

class __declspec(dllexport) SomeClass
{
};

__declspec(dllexport) SomeClass  SomeObject;

Are you familiar with exporting classes?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 22

Expert Comment

by:nietod
ID: 1167509
trestan may be closer to the problem than I.  (what I said is right, but it might not be your problem.)  As he said, the partcular error you get seems to suggest that you are not linking with the file that has the exported object.  (If that is the problem, you should reject my answer so he can get the points.)
0
 
LVL 1

Author Comment

by:tulin
ID: 1167510
Nietod is right, and I will give him the points, in spite of that I had some more
problems.
  Now, using the opportunity, does anybody know how can I do opposite thing -
in dll I want to see variable is defined outside in exe, this means to export variable
FROM exe TO dll.
  If you know, but want points, just write.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1167511
Let me just confirm the question first
The EXE uses the DLL, but the EXE has an export (function or data, it doesn't matter) that the DLL needs to import.  Is that right?

If so it can be done, but is a bit of a pain.  If there are just one or two exports in the EXE that the DLL needs to know about, I would recomend that you have the EXE "register" the function or data with the DLL by passing a pointer to the functions or data to some procedure in the DLL.  This is likely to be a lot easier for you.  

If you want to do it the other way, let me know and I'll look it up.  (Its somewhere in the VC help, I read it long ago, because I thought I was going to have to use it but got around it.)
0
 
LVL 1

Author Comment

by:tulin
ID: 1167512
You understood question right.
I will try to avoid "a bit of pain"; if I still will need this, I will ask new question.
Thank you.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1167513
feel free to ask here if you want.  it should only take me 10 minutes or so to find it.  I just didn't think it was worth 10 minutes if there was an easier solution that is in many ways better.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question