C language static and dynamic link library (notes)

Watched the video has no time to write ...........

C static link library Needless to say, just like you write a function in the cpp file does not have a separate module will not go live with less 

example

 

 

 

 

 

 

 

 

 .h file

you Plus ( you get, you y);        
you Sub ( you get, you y);        
you Mul ( you get, you y);        
you Div ( you get, you y);        

cpp

int Plus(int x, int y)        
{        
    return x+y;    
}        
int Sub(int x, int y)        
{        
    return x-y;    
}        
int Mul(int x, int y)        
{        
    return x*y;    
}        
int Div(int x, int y)        
{        
    return x/y;    
}        

Use compiled the lib files and .h files and you need to use the lib project in the same folder 

 

 

 

 

 

                  Click Project Properties library modules to add to the back of your lib

 

 

 


 

C dynamic link library

2、头文件中                            
                            
extern "C" _declspec(dllexport) __stdcall int Plus (int x,int y);                              
extern "C" _declspec(dllexport) __stdcall int Sub (int x,int y);                            
extern "C" _declspec(dllexport) __stdcall int Mul (int x,int y);                            
extern "C" _declspec(dllexport) __stdcall int Div (int x,int y);                            

cpp

int __stdcall Plus(int x,int y)            
{            
    return x+y;        
}            
int __stdcall Sub(int x,int y)            
{            
    return x-y;        
}            
int __stdcall Mul(int x,int y)            
{            
    return x*y;        
}            
int __stdcall Div(int x,int y)            
{            
    return x/y;        
}            

vs int might have to be placed in front of _declspec   

__stdcall means within the stack and then the function returns to a flat stack balance

 

 

 

DLL use

One way: implicit connection                             
                            
Step 1: * .dll * .lib into the project directory                             
                            
Step 2: #pragma the Comment (lib, "DLL name .lib") added to the calling file                             
                            
Step 3: Declare join function                             
                            
extern  " C " __declspec (dllimport) the __stdcall int Plus ( int X, int Y);                              
 extern  " C " __declspec (dllimport) the __stdcall int Sub ( int X, int Y);                            
 extern  " C "__declspec (dllimport) the __stdcall int adder Mul ( int X, int Y);                            
 extern  " C " __declspec (dllimport) the __stdcall int Div's ( int X, int Y);                             
                            
Note:                             
      Add extern c compiled form of c                         
__declspec (dllimport) tells the compiler this function is a function to import;                            
                            

Second way

    Second way: show links                             
                                
    Step 1:     // definition of the function pointer                         
        typedef int (lpPlus the __stdcall *) ( int , int );                         
        typedef int (lpSub the __stdcall *) ( int , int );                         
        typedef int (lpMul the __stdcall *) ( int , int );                         
        typedef int (lpDiv the __stdcall *) ( int , int );                         
                                
    step 2:   //Declare function pointer variable                             
        lpPlus myPlus;                         
        lpSub mySub;                         
        lpMul myMul;                         
        lpDiv myDiv;                         
                                
    Step 3:   //     // dynamically loaded into memory dll                         
        the HINSTANCE the hModule = the LoadLibrary ( " DllDemo.dll " );                          
                                
    Step 4:   // Get function address                             
        myPlus = (lpPlus) the GetProcAddress (the hModule,    " _Plus @. 8 " );                         
        mySub = (lpSub) the GetProcAddress (the hModule,    "@. 8 _sub " );                         
        myMul = (lpMul) the GetProcAddress (the hModule,    " _Mul @. 8 " );                         
        myDiv = (lpDiv) the GetProcAddress (the hModule,    " _Div @. 8 " );                         
                                
                                
    Step 5:     // call the function                         
        int A = myPlus ( 10 , 2 );                        
         int B = mySub ( 10 , 2 );                        
         int C = myMul ( 10 , 2);                        
        int d = myDiv(10,2);                        
                                

Tip specific function name can generate what dll function viewer with a look created in this way changes the function name

 

 


 

 


 

def export function and the function name, like the name can also be defined as dll export anonymous call to prevent others

 

CPP file and header files and you usually defined as written on the line

1、*.h文件    
    
int Plus (int x,int y);      
int Sub (int x,int y);    
int Mul (int x,int y);    
int Div (int x,int y);    
    
    
2、*.cpp文件    
    
int Plus(int x,int y)    
{    
    return x+y;
}    
int Sub(int x,int y)    
{    
    return x-y;
}    
int Mul(int x,int y)    
{    
    return x*y;
}    
int Div(int x,int y)    
{    
    return x/y;
}    

 

3、*.def文件    
    
EXPORTS    
    
Plus       @12
Sub            @15 NONAME
Mul        @13
Div        @16

其中NONAME 不显示调用函数名 使用序号调用  关于文件怎么定义和配置可以参考一下  https://blog.csdn.net/hanxiucaolss/article/details/95079199

 

def匿名dll函数调用方法(和上面的类似只不过这个是序号调用)

 

#include "stdafx.h"
typedef int( *lpPlus)(int, int);
typedef int( *lpSub)(int, int);
typedef int( *lpMul)(int, int);
typedef int( *lpDiv)(int, int);

int _tmain(int argc, _TCHAR* argv[])
{
    

    lpPlus myPlus;
    lpSub mySub;
    lpMul myMul;
    lpDiv myDiv;
    HINSTANCE   hModule = LoadLibrary(L"windll.dll");
    /*
    
    Handle 是代表系统的内核对象,如文件句柄,线程句柄,进程句柄。

    HMODULE 是代表应用程序载入的模块

    HINSTANCE 在win32下与HMODULE是相同的东西 Win16 遗留

    HWND 是窗口句柄

    其实就是一个无符号整型,Windows之所以这样设计有2个目的:

    1、可读性更好

    2、避免在无意中进行运算

    
    */
    myPlus = (lpPlus)GetProcAddress(hModule, (char*)12);//这里的12是你原来定义的序号//mySub = (lpSub)GetProcAddress(hModule, "_Sub@8");
    //myMul = (lpMul)GetProcAddress(hModule, "_Mul@8");
    //myDiv = (lpDiv)GetProcAddress(hModule, "_Div@8");

    //printf_s("%x", GetProcAddress(hModule, "Plus"));
    printf_s("%d", myPlus(1, 2));

    return 0;
}

 

Guess you like

Origin www.cnblogs.com/xuexidememeda/p/12327607.html