Solved

Export instance of class into dll

Posted on 1998-07-09
8
292 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now