Solved

Export instance of class into dll

Posted on 1998-07-09
8
301 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

830 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