信息交流、传播、提炼

nice to meet you

博客园 首页 新随笔 联系 订阅 管理

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


选择DLL (D) ->完成.

1、新建头文件testdll.h
testdll.h代码如下:
#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代码如下:
#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代码如下:
LIBRARY "MyDLL"
EXPORTS
Add @1


 

 

 

 

 

 

 

 

 

 

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

dllmain.cpp代码如下:
// dllmain.cpp : 定义 DLL 应用程序的入口点。
#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

 



转自:hi.baidu.com/lipeiyi2006/blog/item/bc59756d197274e64216943b.html

posted on 2011-12-18 17:19  seeyou  阅读(243)  评论(0)    收藏  举报