Solved

Export instance of class into dll

Posted on 1998-07-09
8
286 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

706 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

18 Experts available now in Live!

Get 1:1 Help Now