Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

http://msdn.microsoft.com/en-us/library/9h658af8.aspx

MSDN says I can export function from the library with __declspec(dllexport) but how can I load this library in my executable?

I've got an exported function in DLL:

 __declspec(dllexport) void myfunc(){}

And now I would like to use it in my executable:

 __declspec(dllimport) void myfunc(void);

But how my program will know where to find this function?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
634 views
Welcome To Ask or Share your Answers For Others

1 Answer

This is the compiler/linker job, it's done automatically as long as you

  1. include the .lib in the Linker options
  2. provide the DLL at runtime so that it's found by the exe

The .lib file is generated when you compile the DLL, or is shipped with it if it's not your code. In this case the code is compiled with __declspec(dllexport).

When compiling your exe, the compiler sees that the included function is to be found in DLL. In this case the code is compiled with __declspec(dllimport).

The linker is provided with the .lib file, and generates appropriate instructions in the exe.

These instructions will make the Exe find the DLL and load the exported function at runtime. The DLL just has to be next to the Exe (there are other possible places, however).

Switching between __declspec(dllimport) and __declspec(dllexport) is done by a macro, provided by Visual C++ when creating a DLL project.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...