Dynamic link library dll static loading and dynamic loading

 
Dynamic link means when the executable is not a function of all procedures used to link to a file, because there are many functions in the dll file with the operating system, looking directly from the operating system when the program runs .  
The static link is to use all the functions all linked to the exe file.
Dynamic links are established only a reference to the interface, with the true code and additional data stored in the executable module, and then charged at runtime;  
The static link is to put all the code and data are copied to this module, you no longer need a run-time library.
 
 
1. generate   static link library newdll) win32 project -> dll

Add .h file 
betabinlib.h

BETABINLIB_H #ifndef  
 #define BETABINLIB_H   
   
#ifdef NEWDLL_EXPORTS    // automatically add macros right-engineering - Properties - Configuration Properties - preprocessor - .. define   
#define MYDLL_API extern "C" __declspec (dllexport)  
 #else   
#define MYDLL_API extern "C" __declspec (dllimport)  
 #endif   
   
MYDLL_API int the add ( int X, int Y);   // be prefixed   
#endif  

 

Add .cpp file betabinlib.cpp

 

#include "stdafx.h"
#include "betabinlib.h"
 
int add(int x, int y)
{
    return x + y;
}

 

And compiled .dll. (1) Static load the dll - will load the entire file into the dll .exe file
Features: The program is larger, a larger memory footprint, but faster (without having to call the function LOAD
 
#include <stdio.h>
#include "betabinlib.h"
#include <Windows.h>
#pragma comment(lib, "newdll.lib")
 
int main()
{
    printf("2 + 3 = %d \n", add(2, 3));
    return 0;
}

 

#include <stdio.h>  
#include <Windows.h>  
   
int main()  
{  
    HINSTANCE h=LoadLibraryA("newdll.dll");  
    typedef int (* FunPtr)(int a,int b);//定义函数指针  
   
    if(h == NULL)  
    {  
    FreeLibrary(h);  
    printf("load lib error\n");  
    }  
    else  
    {  
        FunPtr funPtr = (FunPtr)GetProcAddress(h,"add");  
        if(funPtr != NULL)  
        {  
            int result = funPtr(3, 3);  
            printf("3 + 3 = %d \n", result);  
        }  
        else  
        {  
            printf("get process error\n");  
            printf("%d",GetLastError());  
        }  
        FreeLibrary(h);  
    }  
   
    return 0;  
}  

 

 

Original Address: https: //www.cnblogs.com/loanhicks/p/7413996.html

 

Guess you like

Origin www.cnblogs.com/xiangtingshen/p/10979419.html