In a Windows environment,
在Windows环境中,
When I tried to link a DLL to my program Explicitly (using LoadLibrary),
当我试图显式地将DLL链接到我的程序时(使用LoadLibrary),
- First I need to define the function pointers according to each function signature inside the DLL.
- 首先,我需要根据DLL中的每个函数签名定义函数指针。
- Then get the function addresses using 'GetProcAddress' and assign them to those pointers.
- 然后使用“GetProcAddress”获取函数地址,并将它们分配给这些指针。
When I tried to link the DLL to my program Implicitly (using header file)
当我试图隐式地将DLL链接到程序时(使用头文件)
- First it need the relevant header file to get function signatures.
- 首先,它需要相关的头文件来获取函数签名。
-
Then it needs the relevant
Lib
file that was generated with the DLL.然后它需要用DLL生成的相关库文件。
My questions are
我的问题是
- Why does implicitly linking need a
Lib
file as well? - 为什么隐式链接也需要一个Lib文件?
- What information does it need to retrieve from 'Lib' file that it cannot get from the
DLL
orHeader file
? - 它需要从'Lib'文件中检索它不能从DLL或头文件中获取的什么信息?
- If there is something for question 2, how is information retrieved when explicitly loading?
- 如果有问题2,在显式加载时如何检索信息?
- Why does implicitly linking need a
I've already gone trough this question. But I cannnot understand any worthy reason. Please, could someone help to explain this in simple terms. Thank you.
我已经回答了这个问题。但我无法理解任何有价值的理由。谁能帮我简单地解释一下吗?谢谢你!
2 个解决方案
#1
3
Why Implicitly linking need Lib file too.
为什么隐式链接也需要Lib文件。
The .libs have the import information of the dll, you can check the information using the dumpbin
command that is included in Windows/Visual Studio SDK.
libs具有dll的导入信息,您可以使用Windows/Visual Studio SDK中包含的dumpbin命令检查这些信息。
This is the link information of recv inside ws2_32.lib for example:
这是ws2_32中recv的链接信息。例如:*
Version : 0
Machine : 14C (x86)
TimeDateStamp: 4907F6ED Wed Oct 29 01:38:53 2008
SizeOfData : 00000014
DLL name : WS2_32.dll
Symbol name : _recv@16
Type : code
Name type : ordinal
Ordinal : 16
You can check there is the ordinal and the name inside ws2_32.dll (check that now it says to import a DLL).
您可以检查在ws2_32中是否有序数和名称。dll(现在检查它说要导入一个dll)。
What information it need to retrieve from 'Lib' file that cannot get from DLL or Header file
它需要从'Lib'文件中检索什么信息,而不能从DLL或头文件中获取
In the header file, there is no information from where to extract the imports, so them are marked as imports (__imp__name) when compiled, and when it's linked against the .lib, it resolves the name:
在头文件中,没有从何处提取导入的信息,因此在编译时将它们标记为导入(__imp__name),当它与.lib链接时,它将解析名称:
- If it's inside the .lib it just links against it.
- 如果它在。lib中,它就会链接到它。
- But if there is information on external reference (DLL), it will construct the import inside the import table so it's loaded dinamically.
- 但是如果有关于外部引用(DLL)的信息,它将在导入表中构造导入,以便以dinamically方式加载它。
If there is something for question 2, How those information retrieve when explicit loading.
如果有问题2,当显式加载时,这些信息是如何检索的。
If for explicit loading you mean the LoadLibrary, you are telling it at runtime and not at link time. So the PE loader will search the DLL inside the PATH and will load it dynamically. Then you have other functions to get the exported functions addresses.
如果显式加载是指LoadLibrary,则在运行时告诉它,而不是在链接时。因此,PE加载程序将在路径中搜索DLL并动态加载它。然后还有其他函数来获取导出的函数地址。
If you don't understand something just ask me, try playing with dumpbin and read about PE if you want to understand this better.
如果你不理解某事,就问我,试着玩垃圾箱,如果你想更好地理解这个问题,就阅读有关PE的书籍。
#2
1
When linking implicitly, the function declaration specifies the name to be used in the program, and the prototype and calling convention. But more information is needed. Specifically:
当隐式链接时,函数声明指定程序中要使用的名称、原型和调用约定。但是需要更多的信息。具体地说:
- The fact that the function is implemented externally in a DLL.
- 这个函数是在DLL中外部实现的。
- The name of that DLL.
- 那个DLL的名称。
- The exported name of the function. That is the name used to export the function from the DLL which may not be the same as that used when you import it.
- 函数的导出名称。这是用于从DLL中导出函数的名称,它可能与导入时使用的函数不一样。
Some language designers chose to provide this information using language extensions. For example Delphi took this route. Implicit linking is specified entirely in code with no .lib files. On the other hand the convention for C and C++ is to use .lib files to specify the missing information.
一些语言设计人员选择使用语言扩展来提供这些信息。例如,德尔福走了这条路。隐式链接完全在没有.lib文件的代码中指定。另一方面,C和c++的约定是使用.lib文件指定丢失的信息。
#1
3
Why Implicitly linking need Lib file too.
为什么隐式链接也需要Lib文件。
The .libs have the import information of the dll, you can check the information using the dumpbin
command that is included in Windows/Visual Studio SDK.
libs具有dll的导入信息,您可以使用Windows/Visual Studio SDK中包含的dumpbin命令检查这些信息。
This is the link information of recv inside ws2_32.lib for example:
这是ws2_32中recv的链接信息。例如:*
Version : 0
Machine : 14C (x86)
TimeDateStamp: 4907F6ED Wed Oct 29 01:38:53 2008
SizeOfData : 00000014
DLL name : WS2_32.dll
Symbol name : _recv@16
Type : code
Name type : ordinal
Ordinal : 16
You can check there is the ordinal and the name inside ws2_32.dll (check that now it says to import a DLL).
您可以检查在ws2_32中是否有序数和名称。dll(现在检查它说要导入一个dll)。
What information it need to retrieve from 'Lib' file that cannot get from DLL or Header file
它需要从'Lib'文件中检索什么信息,而不能从DLL或头文件中获取
In the header file, there is no information from where to extract the imports, so them are marked as imports (__imp__name) when compiled, and when it's linked against the .lib, it resolves the name:
在头文件中,没有从何处提取导入的信息,因此在编译时将它们标记为导入(__imp__name),当它与.lib链接时,它将解析名称:
- If it's inside the .lib it just links against it.
- 如果它在。lib中,它就会链接到它。
- But if there is information on external reference (DLL), it will construct the import inside the import table so it's loaded dinamically.
- 但是如果有关于外部引用(DLL)的信息,它将在导入表中构造导入,以便以dinamically方式加载它。
If there is something for question 2, How those information retrieve when explicit loading.
如果有问题2,当显式加载时,这些信息是如何检索的。
If for explicit loading you mean the LoadLibrary, you are telling it at runtime and not at link time. So the PE loader will search the DLL inside the PATH and will load it dynamically. Then you have other functions to get the exported functions addresses.
如果显式加载是指LoadLibrary,则在运行时告诉它,而不是在链接时。因此,PE加载程序将在路径中搜索DLL并动态加载它。然后还有其他函数来获取导出的函数地址。
If you don't understand something just ask me, try playing with dumpbin and read about PE if you want to understand this better.
如果你不理解某事,就问我,试着玩垃圾箱,如果你想更好地理解这个问题,就阅读有关PE的书籍。
#2
1
When linking implicitly, the function declaration specifies the name to be used in the program, and the prototype and calling convention. But more information is needed. Specifically:
当隐式链接时,函数声明指定程序中要使用的名称、原型和调用约定。但是需要更多的信息。具体地说:
- The fact that the function is implemented externally in a DLL.
- 这个函数是在DLL中外部实现的。
- The name of that DLL.
- 那个DLL的名称。
- The exported name of the function. That is the name used to export the function from the DLL which may not be the same as that used when you import it.
- 函数的导出名称。这是用于从DLL中导出函数的名称,它可能与导入时使用的函数不一样。
Some language designers chose to provide this information using language extensions. For example Delphi took this route. Implicit linking is specified entirely in code with no .lib files. On the other hand the convention for C and C++ is to use .lib files to specify the missing information.
一些语言设计人员选择使用语言扩展来提供这些信息。例如,德尔福走了这条路。隐式链接完全在没有.lib文件的代码中指定。另一方面,C和c++的约定是使用.lib文件指定丢失的信息。