加文

希望是美好的……
随笔 - 0, 文章 - 209, 评论 - 0, 引用 - 0
数据加载中……

vs2010环境下生成dll文件

1,新建win32工程,选中DLL项目,如下图:

2,分别添加头文件和cpp文件
#ifndef LIB_H
#define LIB_H
extern "C" int _declspec(dllexport)add(int x,int y);    // 声明为C编译、链接方式的外部函数
#endif
#include "stdafx.h"
int add(int x,int y)
{
    return x+y;
}
3,新建win32控制台工程,添加头文件后,然后在main函数中写下如下的代码:
#include "stdafx.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
    typedef int(*lpAddFun)(int,int);//宏定义函数指针类型
    HINSTANCE  hInst=NULL;
    lpAddFun addFun; //函数指针
    hInst=::LoadLibraryA("C:\\Users\\Administrator\\Documents\\Visual Studio 2010\\Projects\\Win32Study\\Debug\\test_1.dll");
    if(hInst==NULL)  
    {
        printf("Load mydll.DLL fail!\n");
        return 0;  
    }
    else
    {
        addFun=(lpAddFun)GetProcAddress(hInst,"add");
        if (addFun!=NULL)
        {
            int result=addFun(2,3);
            cout<<result<<endl;
            getchar();
        }
    }
    FreeLibrary(hInst);
    return 0;
}
4,运行结果如下图:

posted on 2012-02-03 21:21 加文 阅读(7006) 评论(0)  编辑 收藏 引用 所属分类: Compile


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理