T
T
Ternick2020-08-23 00:46:20
C++ / C#
Ternick, 2020-08-23 00:46:20

Which is better to use #include or LoadLibrary?

Good day :)
I searched on the Internet, nothing particularly clear is written.

Many questions

1) Откуда берёт библиотеки LoadLibrary ?
2) Проверяет ли LoadLibrary подпись библиотеки, если берёт её из системных путей ?
3) Уменьшится ли размер исполняемого файла если использовать только LoadLibrary, без #include ? {Один не в счёт, надо же как-то брать HMODULE, WINAPI и прочие штуки ...}
4) Что лучше использовать #include или LoadLibrary ?
5) Если библиотека существует и существует метод к которому я хочу обратится, может ли появиться ошибка ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Shatunov, 2020-08-23
@Ternick

The question, in fact, is not a choice between LoadLibraryand #include, but a choice between explicit and implicit binding .
With implicit linking, the linker adds an additional import block for the specified library to the PE import section. This block is processed by the PE loader in preparation for starting the process. This information should already be familiar to you . By the time control passes to the process entry point, all implicitly linked libraries are already loaded and ready to go. The process code does not need to explicitly manage the loading of such libraries.
Explicit binding is performed entirely by the process code, for which a set of functions is usedto work with libraries. Explicit binding is a big chore. But sometimes without this routine in any way.
Search for dynamic libraries when running LoadLibrary[ ? ] is carried out in accordance with a certain order . At the same time, dynamic library search paths can be managed manually by adding new ones or replacing existing search paths.
The preprocessor directive #include[ ? ] is only indirectly related to the question. Actually the directive #pragma comment( lib )[ ? ].
The pragma only works for the cl compiler from Microsoft. The static library connected in this way must be in the listed Library Path, in the project build settings. This pragma is an alternative to specifying a static library directly in a project's build settings.
This directive is often used in third-party headers, the code for which is supplied only in binary form, in static or dynamic libraries. Together with the dynamic library of such a solution, a static library is also supplied, which contains instructions for implicit linking with the dynamic library. The result of working #pragma comment( lib )with such a static library will be the implicit linking of your code with the specified dynamic library.
The size of PE grows significantly when linked from static libraries.
Explicit and implicit binding have little effect on the size of the PE, but explicit binding makes life difficult for the developer, because the developer will have to support the loading of the library interface himself.

A
anikavoi, 2020-08-23
@anikavoi

#include is a compiler preprocessor directive, it "fires" when you compile your source code.
LoadLibrary is a windows api function that loads a DLL. Accordingly, it "works" when the already compiled executable is launched. DLL is already compiled library - it should be somewhere on PATH.
2) As far as I know - no (what's in 8,10,2016 - xs, I've been on Linux for a long time)
3) I didn't understand at all, sorry
4) Once again - #include is not a "library", although of course it you can bubble the whole code, but imho it's better not to do that. ANNOUNCEMENTS should live in the includes, but the code itself in .cpp
5) again did not understand.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question