CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    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.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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).
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Jul 2005
    Posts
    1,030

    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.

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: A question regarding dllexport and dllimport

    Quote Originally Posted by LarryChen View 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.
    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.
    Best regards,
    Igor

  7. #7
    Join Date
    Jul 2002
    Posts
    2,543

    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".

  8. #8
    Join Date
    Jul 2005
    Posts
    1,030

    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.

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: A question regarding dllexport and dllimport

    Quote Originally Posted by LarryChen View Post
    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.

  10. #10
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A question regarding dllexport and dllimport

    Quote Originally Posted by Arjay View Post
    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?

  11. #11
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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).
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: A question regarding dllexport and dllimport

    Quote Originally Posted by LarryChen View Post
    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.

  13. #13
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: A question regarding dllexport and dllimport

    Quote Originally Posted by LarryChen View Post
    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)
    Attached Files Attached Files
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured