动态链接库

一、生成.dll和.lib文件

 

(1)在VC2003Studio 创建Win32 控制台 应用程序设置 选DLL选项

(2)在工程的属性的配置属性-〉常规-〉配置类型 选 动态库(。dll);

 

头文件:


#define WIN32_LEAN_AND_MEAN  // 从 Windows 头中排除极少使用的资料
// Windows 头文件:
#include <windows.h>

#ifdef MYLIBAPI
#else
#define MYLIBAPI extern "C" __declspec(dllimport)
#endif

MYLIBAPI int g_nResult;

MYLIBAPI int Add(int nLeft, int nRight);

 

CPP文件

 

#include "stdafx.h"
//#define MYLIBAPI extern"C" __declspec(dllexport)
//#include "MyDll.h"

int g_nResult;
int Add(int nLeft, int nRight)
{
 g_nResult = nLeft + nRight;
 return g_nResult;
}

 

运行生成相应的.dll和.lib文件

 

二、调用自己的.dll中的函数

(1)在VC2003Studio 创建Win32 控制台;

(2)将.lib和.dll文件copy到本工程目录中

(3)在工程的属性的链接器-〉输入-〉附加依赖项 添加.lib

 

调用函数的CPP

 

#include "stdafx.h"
#include <windows.h>

extern "C" int Add(int nLeft, int nRight);
int _tmain(int argc, _TCHAR* argv[])
{
 int nLeft;
 int nRight;
 int nResult;
 nLeft = 10;
 nRight = 25;
 nResult = Add(nLeft, nRight);
 return 0;
}

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值