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

//dll1.cpp

#include "dll1.h"

int add(int x,int y)
{
    return x+y; 
}

//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

//dll2.cpp

#include "dll2.h"
int add_10(int x,int y)
{
    return add(x+y)+10; 
}

//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();
}

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);
       }
    }
}

你可能感兴趣的:(dll 二次封装)