创建.dll文件

时间:2014-01-27 08:52:49

标签: c++ visual-studio-2012

我有4个文件:

  

mySender.h mySender.cpp myReceiver.h myReceiver.cpp

我想在VisualStudio 2012中的.dll项目“MyIfa”中导入这4个文件。 我创建了一个新项目---> win32 --->我在向导中选择导出外部符号---->至少创建了项目(使用所有组件创建新文件夹)。

现在我需要做什么?

  • 在我的项目中添加四个文件(添加现有项目)并构建?
  • 在我的项目中添加四个文件(添加现有项目),包括.h 文件(mySender.h,myReceiver.h)和建筑?
  • 在我的项目中添加四个文件(添加现有项目),包括.h 文件(mySender.h,myReceiver.h),添加其他目录和 库(我在mySender.h中使用,myReceiver.h,mySender.cpp, myReceiver.cpp)在项目属性和构建中?

这是生成的新代码: IfacomAmqDll.h

// The following ifdef block is the standard way of creating macros which make exporting 
// from a DLL simpler. All files within this DLL are compiled with the IFACOMAMQDLL_EXPORTS
// symbol defined on the command line. This symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see 
// IFACOMAMQDLL_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#ifdef IFACOMAMQDLL_EXPORTS
#define IFACOMAMQDLL_API __declspec(dllexport)
#else
#define IFACOMAMQDLL_API __declspec(dllimport)
#endif

// This class is exported from the IfacomAmqDll.dll
class IFACOMAMQDLL_API CIfacomAmqDll {
public:
    CIfacomAmqDll(void);
    // TODO: add your methods here.
};

extern IFACOMAMQDLL_API int nIfacomAmqDll;

IFACOMAMQDLL_API int fnIfacomAmqDll(void);

这是IfacomAmqDll.cpp:

// IfacomAmqDll.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "IfacomAmqDll.h"


// This is an example of an exported variable
IFACOMAMQDLL_API int nIfacomAmqDll=0;

// This is an example of an exported function.
IFACOMAMQDLL_API int fnIfacomAmqDll(void)
{
    return 42;
}

// This is the constructor of a class that has been exported.
// see IfacomAmqDll.h for the class definition
CIfacomAmqDll::CIfacomAmqDll()
{
    return;
}

那么我需要用现有的.h和.cpp文件来导出所有内部定义的类?

1 个答案:

答案 0 :(得分:1)

如果您运行向导来创建包含一些示例导出的DLL项目,则可以看到从dll导出类和/或函数时需要遵循的格式。应该这么简单:

  1. 将现有源文件添加到DLL项目中。
  2. 对于您要导出的任何类,#include生成的DLL头文件,并将解析为#define的{​​{1}}'ed符号添加到类名称前。
  3. 重建。
  4. 例如,向导将生成一个头文件,例如__declspec(dllexport),其中包含以下内容:

    header.h

    现在,假设您现有的源代码中有一个名为// The following ifdef block is the standard way of creating macros which make exporting // from a DLL simpler. All files within this DLL are compiled with the WIN32PROJECT2_EXPORTS // symbol defined on the command line. This symbol should not be defined on any project // that uses this DLL. This way any other project whose source files include this file see // WIN32PROJECT2_API functions as being imported from a DLL, whereas this DLL sees symbols // defined with this macro as being exported. #ifdef WIN32PROJECT2_EXPORTS #define WIN32PROJECT2_API __declspec(dllexport) #else #define WIN32PROJECT2_API __declspec(dllimport) #endif 的现有类:

    CMyClass

    如果要从dll导出他的类,请执行以下操作:

    class CMyClass
    {
        // ... blah ...
    };