windows production and use of a dynamic link library

Production:

// myDll.h 
_declspec (dllexport) int the Add ( int A, int B); 
_declspec (dllexport) int Sub ( int A, int B);
 // myDll.cpp 
#include " myDll.h " // This must , or not turned out to be not the first document, the focus _declspec (dllexport) 
int the Add ( int A, int B) 
{ 
return A + B; 
} 

int Sub ( int A, int B) 
{ 
  return A- B;   
} 
//Entry function, random file name xx.cpp 
#include <the Windows.h> 

BOOL APIENTRY DllMain (HMODULE hModule, 
                       DWORD ul_reason_for_call, 
                       LPVOID lpReserved 
                     ) 
{ 
    Switch (ul_reason_for_call) 
    { // below some free play 
    Case DLL_PROCESS_ATTACH:
     Case DLL_THREAD_ATTACH:
     Case DLL_THREAD_DETACH :
     Case DLL_PROCESS_DETACH:
         BREAK ; 
    } 
    return TRUE; 
}

use:

1, typedef function pointer;

2, the pointer variable declarations;

3, dll dynamically loaded into memory;

4, the address acquisition function;

5, call the function;

6, the release of dynamic link library, FreeLibary (HMODULE hDll);

//test.cpp
#include<stdio.h>

typedef int (*LPdd)(int,int);
typedef int (*LPsub)(int,int);

LPadd add;
LPsub sub;
int main()
{
    HMODULE hDll = LoadLibary("demoDll.dll");
   
   add = (LPadd)GetPrecAddress(hDll,"add");
   sub = (LPsub)GetProcAddress(hDll,"sub");

   int x=add(10,2); // x = 12 
  int y = sub ( 10 , 2 ); // y = 8 

return  0 ;        
}

Guess you like

Origin www.cnblogs.com/a-s-m/p/12215305.html