A question regarding dllexport and dllimport
Usually we use the following statements to export a class or member functions,
Code:
#ifdef DLLMICRO
#define DLLIO __declspec(dllexport)
#else
#define DLLIO __declspec(dllimport)
#endif
I understand that the files using the exported class or function need to call this class or function with dllimport and the file containing the exported class or function needs to call this class or function with dllexport. But I tried to use __declspec(dllexport) only instead of the statements above. It still works. Is there anything I am missing?Why'd we have to switch between dllexport and dllimport? Thanks.
Re: A question regarding dllexport and dllimport
Search Bing or Google with "__declspec(dllexport) vs. __declspec(dllimport)" for the many available answers.
You could even search this forum for the same.
Re: A question regarding dllexport and dllimport
What is working? Building the dll? Nobody here can read your mind so how could we know what you're missing?
Re: A question regarding dllexport and dllimport
In the project that exports something, you must marked the exported symbols (such as functions or types) with __declspec(dllexport). However, in the project where you import them, you must mark them with __declspec(dllimport). The code above is a little trick to have a single declaration of these symbols. By defining DLLMICRO in your exporting project, you make sure these symbols are compiled with the __declspec(dllexport) markup, while in the importing project where DLLMICRO should not be defined, they would end up marked with __declspec(dllimport).
Re: A question regarding dllexport and dllimport
In my last post, I tried to explain that if I use dllexport only without using a macro to switch between dllexport and dllimport, my app still works. In other words, I don't see why I need dllimport. What problems will I have without dllimport? Thanks.
Re: A question regarding dllexport and dllimport
Quote:
Originally Posted by
LarryChen
I tried to explain that if I use dllexport only without using a macro to switch between dllexport and dllimport, my app still works.
You tried. But nobody still understands what "I use", "switch between dllexport and dllimport", "app works" really mean. Without seeing how you "use", "switch" and test "app working" nobody would help you with your question.
I believe, cilu has fully answered your question, and now the question is whether you comprehend the answer.
Re: A question regarding dllexport and dllimport
Generally, the sentence "the program works" doesn't prove anything. In most cases, this means: "program crashes once per month and this crash cannot be reproduced" or "program works on my computer, but crashes on another computer/Windows version".
Re: A question regarding dllexport and dllimport
It is very simple. Here is the code,
Code:
//AClass.h
class AClass
{
public:
__declspec(dllexport) void foo();
__declspec(dllexport) void bar();
};
My question is that is there any potential problem with this class? Thanks.
Re: A question regarding dllexport and dllimport
Quote:
Originally Posted by
LarryChen
It is very simple. Here is the code,
Code:
//AClass.h
class AClass
{
public:
__declspec(dllexport) void foo();
__declspec(dllexport) void bar();
};
My question is that is there any potential problem with this class? Thanks.
Only if you use it in a dll and consume it in an exe.
Re: A question regarding dllexport and dllimport
Quote:
Originally Posted by
Arjay
Only if you use it in a dll and consume it in an exe.
Thanks for your reply. So in what situation the code is going to fail?
Re: A question regarding dllexport and dllimport
Yes, it's going to fail when you want to import it in another module (DLL or EXE).
Re: A question regarding dllexport and dllimport
Quote:
Originally Posted by
LarryChen
Thanks for your reply. So in what situation the code is going to fail?
Larry, when you ask a question and 5 people respond with the same answer, it might be time to write some code to try to understand the answer.
1 Attachment(s)
Re: A question regarding dllexport and dllimport
Quote:
Originally Posted by
LarryChen
Thanks for your reply. So in what situation the code is going to fail?
Code:
E:\Temp\68>make > nul
E:\Temp\68>main.exe
foo
E:\Temp\68>make trouble > nul
E:\Temp\68>main.exe
foo (special)