Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between Load DLL and Direct Call

Tags:

c++

i think is very stupid, but I can't understand,

for example, I want use Windows API like GetWindowsDirectory, GetSystemInfo and etc... I can use Api directly or calling through GetProcAddress :

Method 1 here I can calling APIs with LoadLibrary and GetProcAddress :

#include <windows.h>

typedef UINT (WINAPI *GET_WIN_DIR)(LPWSTR lpBuffer, UINT size);

TCHAR infoBuffer[MAX_PATH + 1];
HINSTANSE dllLoad = LoadLibrary("Kernel32.dll");
GET_WIN_DIR function = (GET_WIN_DIR )GetProcAddress(dllLoad, "GetWindowsDirectoryW");

int result = function2(infoBuffer, MAX_PATH + 1);

Method 2 here I can calling directly APIs like GetWindowsDirectory :

#include <windows.h>

TCHAR infoBuffer[MAX_PATH + 1];
GetWindowsDirectory(infoBuffer, MAX_PATH);

I have 2 question :

  1. What is the difference between the two methods above?

  2. is it load Library impact on executable file?(.exe)(I did test, but it'snot changed)

like image 976
Mr.DeveloperCplus Avatar asked Jun 17 '18 06:06

Mr.DeveloperCplus


1 Answers

Microsoft calls

  • Method 1 ... Explicit linking
  • Method 2 ... Implicit linking

From MSDN Linking an Executable to a DLL:

Implicit linking is sometimes referred to as static load or load-time dynamic linking. Explicit linking is sometimes referred to as dynamic load or run-time dynamic linking.

With implicit linking, the executable using the DLL links to an import library (.lib file) provided by the maker of the DLL. The operating system loads the DLL when the executable using it is loaded. The client executable calls the DLL's exported functions just as if the functions were contained within the executable.

With explicit linking, the executable using the DLL must make function calls to explicitly load and unload the DLL and to access the DLL's exported functions. The client executable must call the exported functions through a function pointer.

An executable can use the same DLL with either linking method. Furthermore, these mechanisms are not mutually exclusive, as one executable can implicitly link to a DLL and another can attach to it explicitly.

In our projects we use implicit linking in any common case.

We use the explicit linking exceptionally in two situations:

  1. for plug-in DLLs which are loaded explicitly at run-time
  2. in special cases where the implicit linked function is not the right one.

The 2nd case may happen if we use DLLs which themselves link to distinct versions of other DLLs (e.g. from Microsoft). This is, of course, a bit critical. Actually, we try to prevent the 2nd case.

like image 187
Scheff's Cat Avatar answered Oct 19 '22 03:10

Scheff's Cat