VS2010中 C++创建DLL图解.

4 篇文章 0 订阅
转自:http://hi.baidu.com/lipeiyi2006/item/df1d035734472d9509be17c0


VS2010中 C++创建DLL图解.

一、DLL的创建 
创建项目: Win32->Win32项目,名称:MyDLL


选择DLL (D) ->完成.

1、新建头文件testdll.h
testdll.h代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef TestDll_H_
#define TestDll_H_
#ifdef MYLIBDLL
#define MYLIBDLL extern "C" _declspec(dllimport) 
#else
#define MYLIBDLL extern "C" _declspec(dllexport) 
#endif
MYLIBDLL  int  Add( int  plus1,  int  plus2);
//You can also write like this:
//extern "C" {
//_declspec(dllexport) int Add(int plus1, int plus2);
//};
#endif



2、新建源文件testdll.cpp
testdll.cpp代码如下:

1
2
3
4
5
6
7
8
9
#include "stdafx.h"
#include "testdll.h"
#include <iostream>
using  namespace  std;
int  Add( int  plus1,  int  plus2)
{
int  add_result = plus1 + plus2;
return  add_result;
}



3、新建模块定义文件mydll.def
mydll.def代码如下:

1
2
3
LIBRARY  "MyDLL"
EXPORTS
Add @1




4、vs2010自动创建dllmain.cpp文件,它定义了DLL 应用程序的入口点。

dllmain.cpp代码如下:
// dllmain.cpp : 定义 DLL 应用程序的入口点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "stdafx.h"
BOOL  APIENTRY DllMain(  HMODULE  hModule,
                        DWORD   ul_reason_for_call,
                        LPVOID  lpReserved
       )
{
switch  (ul_reason_for_call)
{
case  DLL_PROCESS_ATTACH:
case  DLL_THREAD_ATTACH:
case  DLL_THREAD_DETACH:
case  DLL_PROCESS_DETACH:
   break ;
}
return  TRUE;
}


最后,编译生成MyDLL.dll文件和MyDLL.lib文件。

1>------ 已启动生成: 项目: MyDLL, 配置: Debug Win32 ------

1>  dllmain.cpp

========== 生成: 成功 1 个,失败 0 个,最新 0 个,跳过 0 个 ==========

 

1>------ 已启动生成: 项目: MyDLL, 配置: Debug Win32 ------

1>  stdafx.cpp

1>  testdll.cpp

1>  MyDLL.cpp

1>  正在生成代码...

1>     正在创建库 D:\Visual C++\工程\Libaray\MyDLL\Debug\MyDLL.lib 和对象 D:\Visual C++\工程\Libaray\MyDLL\Debug


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值