C++ DLL 模板

1、使用VS2005创建Win32 DLL项目,选择空项目,然后加入CppDll.h和CppDll.cpp文件。

2、修改CppDll.h和CppDll.cpp文件使之成为需要的内容。

3、编译生成CppDll.dll。

下面是模板文件:

  1. //
  2. // CppDll.h
  3. // by cheungmine
  4. // C++ DLL 模板
  5. //
  6. /*** 使用CPPDLL:
  7. #include "../CppDll.h"
  8. #ifdef _DEBUG
  9. #  pragma comment(lib, "F:/del/CppDll/Debug/CppDlld.lib")
  10. #else
  11. #  pragma comment(lib, "F:/del/CppDll/Release/CppDll.lib")
  12. #endif
  13. ***/
  14. #ifndef _CPPDLL_H__
  15. #define _CPPDLL_H__
  16. //#include <windows.h>
  17. //#include <math.h>
  18. //#include <assert.h>
  19. //#include <memory.h>
  20. //#include <malloc.h>
  21. // 下列 ifdef 块是创建使从 DLL 导出更简单的宏的标准方法。
  22. // 此 DLL 中的所有文件都是用命令行上定义的 CPPDLL_EXPORTS 符号编译的。
  23. // 在使用此 DLL 的任何其他项目上不应定义此符号。
  24. // 这样,源文件中包含此文件的任何其他项目都会将 CPPDLL_API 函数视为是从此 DLL 导入的,
  25. // 而此 DLL 则将用此宏定义的符号视为是被导出的。
  26. #ifdef CPPDLL_EXPORTS
  27. #define CPPDLL_API __declspec(dllexport)
  28. #else
  29. #define CPPDLL_API __declspec(dllimport)
  30. #endif
  31. #define     CPPDLL_VERSION   1.0            // 常量定义
  32. // 名称空间
  33. namespace CppDll
  34. {
  35. //
  36. // 从 CppDll.dll 导出类
  37. //
  38. // 导出类: MyStruct
  39. struct CPPDLL_API MyStruct
  40. {
  41. long    x;
  42. long    y;
  43. };
  44. // 导出类: MyClass2
  45. class CPPDLL_API MyClass2
  46. {
  47. void Clear()
  48. {
  49. // 实现
  50. };
  51. public:
  52. MyClass2();
  53. ~MyClass2();
  54. };
  55. // 导出共享变量
  56. extern CPPDLL_API int g_nVar;
  57. //
  58. // 导出方法
  59. //
  60. CPPDLL_API double Method(const MyStruct *s1, const MyStruct *s2);
  61. CPPDLL_API double Method(const MyStruct &s1, const MyStruct &s2);
  62. };   // End of namespace CppDll
  63. #endif  // _CPPDLL_H__

//
// CppDll.h
// by cheungmine
// C++ DLL 模板
//
/*** 使用CPPDLL:
#include "../CppDll.h"
#ifdef _DEBUG
# pragma comment(lib, "F:/del/CppDll/Debug/CppDlld.lib")
#else
# pragma comment(lib, "F:/del/CppDll/Release/CppDll.lib")
#endif
***/
#ifndef _CPPDLL_H__
#define _CPPDLL_H__
//#include <windows.h>
//#include <math.h>
//#include <assert.h>
//#include <memory.h>
//#include <malloc.h>
// 下列 ifdef 块是创建使从 DLL 导出更简单的宏的标准方法。
// 此 DLL 中的所有文件都是用命令行上定义的 CPPDLL_EXPORTS 符号编译的。
// 在使用此 DLL 的任何其他项目上不应定义此符号。
// 这样,源文件中包含此文件的任何其他项目都会将 CPPDLL_API 函数视为是从此 DLL 导入的,
// 而此 DLL 则将用此宏定义的符号视为是被导出的。
#ifdef CPPDLL_EXPORTS
#define CPPDLL_API __declspec(dllexport)
#else
#define CPPDLL_API __declspec(dllimport)
#endif
#define CPPDLL_VERSION 1.0 // 常量定义
// 名称空间
namespace CppDll
{
//
// 从 CppDll.dll 导出类
//
// 导出类: MyStruct
struct CPPDLL_API MyStruct
{
long x;
long y;
};
// 导出类: MyClass2
class CPPDLL_API MyClass2
{
void Clear()
{
// 实现
};
public:
MyClass2();
~MyClass2();
};
// 导出共享变量
extern CPPDLL_API int g_nVar;
//
// 导出方法
//
CPPDLL_API double Method(const MyStruct *s1, const MyStruct *s2);
CPPDLL_API double Method(const MyStruct &s1, const MyStruct &s2);

}; // End of namespace CppDll
#endif // _CPPDLL_H__

  1. //
  2. // CppDll.cpp
  3. // by cheungmine
  4. //
  5. #include "CppDll.h"
  6. // 包含其他必要文件
  7. // #include <vector>
  8. using namespace CppDll;
  9. ///////////////////////////////////////////////////////////////////////////////
  10. // struct MyStruct
  11. ///////////////////////////////////////////////////////////////////////////////
  12. // class MyClass2
  13. MyClass2::MyClass2()
  14. {
  15. }
  16. MyClass2::~MyClass2()
  17. {
  18. }
  19. ///////////////////////////////////////////////////////////////////////////////
  20. // 导出变量
  21. CPPDLL_API int g_nVar = 0;
  22. ///////////////////////////////////////////////////////////////////////////////
  23. // 导出方法
  24. CPPDLL_API double CppDll::Method(const MyStruct *s1, const MyStruct *s2)
  25. {
  26. return 0;
  27. }
  28. CPPDLL_API double CppDll::Method(const MyStruct &s1, const MyStruct &s2)
  29. {
  30. return 0;
  31. }

//
// CppDll.cpp
// by cheungmine
//
#include "CppDll.h"
// 包含其他必要文件
// #include <vector>
using namespace CppDll;
///////////////////////////////////////////////////////////////////////////////
// struct MyStruct

///////////////////////////////////////////////////////////////////////////////
// class MyClass2
MyClass2::MyClass2()
{
}

MyClass2::~MyClass2()
{
}
///////////////////////////////////////////////////////////////////////////////
// 导出变量
CPPDLL_API int g_nVar = 0;
///////////////////////////////////////////////////////////////////////////////
// 导出方法
CPPDLL_API double CppDll::Method(const MyStruct *s1, const MyStruct *s2)
{
return 0;
}
CPPDLL_API double CppDll::Method(const MyStruct &s1, const MyStruct &s2)
{
return 0;
}

C++ DLL 模板 .的更多相关文章

  1. VS2005环境下的DLL应用

    VS2005环境下的DLL应用 作者:一点一滴的Beer http://beer.cnblogs.com/ 以前写过一篇题为<VC++的DLL应用(含Demo演示)>的文章,当时是刚开始接 ...

  2. 如何用AU3调用自己用VC++写的dll函数

    这问题困扰我一个上午了,终于找到原因了,不敢藏私,和大家分享一下. 大家都知道,AU3下调用dll文件里的函数是很方便的,只要一个dllcall语句就可以了. 比如下面这个: $result = Dl ...

  3. Coablt strike官方教程中文译版本

    安装和设置 系统要求 Cobalt Strike的最低系统要求 2 GHz +以上的cpu 2 GB RAM 500MB +可用空间 在Amazon的EC2上,至少使用较高核数的CPU(c1.medi ...

  4. MFC模块状态(一)

    先看一个例子: 1.创建一个动态链接到MFC DLL的规则DLL,其内部包含一个对话框资源.指定该对话框ID如下:              #define IDD_DLL_DIALOG  2000 ...

  5. Coablt strike官方教程中文版

    安装和设置 系统要求 Cobalt Strike的最低系统要求 2 GHz +以上的cpu 2 GB RAM 500MB +可用空间 在Amazon的EC2上,至少使用较高核数的CPU(c1.medi ...

  6. T4 模板自动生成带注释的实体类文件 - 只需要一个 SqlSugar.dll

    生成实体就是这么简单,只要建一个T4文件和 文件夹里面放一个DLL. 使用T4模板教程 步骤1 创建T4模板 ,一定要自已新建,把T4代码复制进去,好多人因为用我现成的T4报错(原因不明) 点击添加文 ...

  7. VS编译器从DLL导出模板类

    DLL与模板 http://msdn.microsoft.com/en-us/library/twa2aw10.aspx http://www.codesynthesis.com/~boris/blo ...

  8. [百度空间] [原]DLL导出实例化的模板类

    因为模板是在编译的时候根据模板参数实例化的,实例化之后就像一个普通的类(函数),这样才有对应的二进制代码;否则,没有模板参数,那么编译器就不知道怎么生成代码,所以生成的DLL就没有办法导出模板了.但是 ...

  9. DLL中导出STL模板类的问题

    接上一篇. 上一篇的dll在编译过程中一直有一个警告warning C4251: ‘CLASS_TEST::m_structs’ : class ‘std::vector<_Ty>’ ne ...

随机推荐

  1. PHP学习笔记 - 进阶篇(7)

    PHP学习笔记 - 进阶篇(7) 文件操作 读取文件内容 PHP具有丰富的文件操作函数,最简单的读取文件的函数为file_get_contents,可以将整个文件全部读取到一个字符串中. $conte ...

  2. Android的selector,背景选择器

    原文地址 http://android.blog.51cto.com/268543/564581 首先android的selector是在drawable/xxx.xml中配置的,相关图片放在同目录下 ...

  3. JavaScript 函数的执行过程

    每一个JavaScript函数都是Function对象的一个实例, 它有一个仅供JavaScript引擎存取的内部属性[[Scope]]. 这个[[Scope]]存储着一个作用域的集合, 这个集合就叫 ...

  4. 【leetcode】365. Water and Jug Problem

    题目描述: You are given two jugs with capacities x and y litres. There is an infinite amount of water su ...

  5. Tomcat虚拟目录的设置

    在学习JSP/Servlet的过程中,配置Tomcat的虚拟目录可能是我们遇到的第一个比较麻烦的问题,说是麻烦是针对我们初学者而言,对于高手那都不是问题.反正我是弄了一天才配置好,发现网上给出的很多配 ...

  6. libevent 初试

    一直就想用一下libevent库,直到今天才去码代码.用法在他的头文件里面写的很清楚,原理的话也不是很难,感谢作者做的工作! 今天做了几个探索: 实现自定义事件类型的设置与触发 尝试 setInter ...

  7. 基于IOS和Android设备MDM技术方案服务价格

    导读:前段时间 www.mbaike.net 博客被恶意攻击,导致程序崩溃,目前已经替换了以前的Wordpress程序,现提供IOS和Android版本MDM的代码和相关文档咨询服务. 一.IOS版M ...

  8. Hash function

    Hash function From Wikipedia, the free encyclopedia   A hash function that maps names to integers fr ...

  9. IIS上的错误与解决方案

    1.未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序. 解决方案:在IIS上打开该网站应用程序池-->高级设置-->启动32位程序-->选True

  10. 开源 侧滑 和 Tab滑动翻页 控件

    侧滑 https://github.com/jfeinstein10/SlidingMenu Tab滑动翻页 https://github.com/astuetz/PagerSlidingTabStr ...