__declspec(dllimport)如何加载库 [英] __declspec(dllimport) how to load library

查看:526
本文介绍了__declspec(dllimport)如何加载库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



MSDN说我可以从 __ declspec(dllexport)导出函数库,但是如何加载

 <$> 

c $ c> __declspec(dllexport)void myfunc(){}

现在我想使用它在我的可执行文件:

  __declspec(dllimport)void myfunc 

但是我的程序如何知道在哪里找到这个函数?

解决方案

这是编译器/链接器工作,只要你自动完成


  1. 在链接器选项中包含.lib

  2. 在运行时提供DLL,以便exe可以找到它

.lib文件是在编译DLL时生成的,如果它不是您的代码,则会随之提供。在这种情况下,代码编译时使用 __ declspec(dllexport)



编译你的exe时,包含的函数是在DLL中找到的。在这种情况下,代码是用 __ declspec(dllimpport)



编译的。 ,并在exe中生成相应的指令。



这些指令将使Exe找到DLL并在运行时加载导出的函数。



__ declspec(dllimpport) __ declspec(dllexport)是由宏执行的,提供创建DLL项目时的Visual C ++


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?

解决方案

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(dllimpport).

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(dllimpport) and __declspec(dllexport) is done by a macro, provided by Visual C++ when creating a DLL project.

这篇关于__declspec(dllimport)如何加载库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆