dll秘籍

来源:互联网 发布:安卓手机数据恢复工具 编辑:程序博客网 时间:2024/05/16 14:35
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <stdio.h>
#include <windows.h>
//typedef int(*lpAddFun)(int,int);//宏定义函数


class DllLoad
{
public:
    DllLoad();
    ~DllLoad();

private:
    HINSTANCE m_hDll;//DLL句柄加载
    typedef int(*lpAddFun)(int,int);//宏定义函数
public:
    lpAddFun addFun;

};

DllLoad::DllLoad()
{
    m_hDll = LoadLibraryA("F:\\Program\\VC\\dll\\dllTest\\Debug\\dllTest.dll");
    if (m_hDll == NULL)
    {
        //提示
        return ;
    }
    addFun = (lpAddFun)GetProcAddress(m_hDll,"add");
}
DllLoad::~DllLoad()
{    
    FreeLibrary(m_hDll);
}

int _tmain(int argc, _TCHAR* argv[])
{
    //HINSTANCE hDll;//DLL句柄加载
    //lpAddFun addFun;
    //hDll = LoadLibraryA("F:\\Program\\VC\\dll\\dllTest\\Debug\\dllTest.dll");
    DllLoad myLoad;

    printf("%d",myLoad.addFun(1,2));
    return 0;

}


//lib.cpp
#include "lib.h"

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

//lib.h
#ifndef LIB_H
#define LIB_H
extern "C" int _declspec(dllexport)add(int x,int y);
#endif



原创粉丝点击