dll二次封装

需要用到二次封装,其实很简单,不过在第二个dll调用第一个dll的方法而已。 
笔记下以免忘了。 
//dll1.h:

#ifndef _dll1_h
#define _dll1_h
#define MYDLL extern "C" _declspec (dllexport)
MYDLL int add(int x,int y);
#endif //_dll1_h
  • 1
  • 2
  • 3
  • 4
  • 5

//dll1.cpp

#include "dll1.h"

int add(int x,int y)
{
    return x+y; 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

//dll2.h

#include "dll1.h"
#pragma comment(lib,"dll1.lib")
#ifndef _dll2_h
#define _dll2_h
#define MYDLL extern "C" _declspec (dllexport)
MYDLL int add_10(int x,int y);
#endif //_dll2_h
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

//dll2.cpp

#include "dll2.h"
int add_10(int x,int y)
{
    return add(x+y)+10; 
}
  • 1
  • 2
  • 3
  • 4
  • 5

//usedll

#include "stdafx.h"

#include "dll1.h"
#pragma comment(lib,"dll1.lib")
#include "dll2.h"
#pragma comment(lib,"dll2.lib")

int main()
{
    cout<<"dll1_add:"<<add(3+6)<<endl;
    cout<<"dll2_add:"<<add_10(3+6)<<endl;
    getchar();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

Dll的显式链接调用。

typedef int (CALLBACK* LPFun)(int,int);

void callDll()
{ 
    HINSTANCE hDLL; // Handle to DLL
    LPFun pFun;     // Function pointer
    int nParam1,nParam2, nReturnVal;

    hDLL = LoadLibrary("MyDLL");
    if (hDLL != NULL)
    {
       pFun = (LPFun)GetProcAddress(hDLL,"DLLFunc1");
       if (!pFun){
          // handle the error
          FreeLibrary(hDLL);
          return;
       }else{
          // call the function
          nReturnVal = pFun(nParam1, nParam2);
       }
    }
}

猜你喜欢

转载自blog.csdn.net/hk121/article/details/80802855