我有4个文件:
mySender.h mySender.cpp myReceiver.h myReceiver.cpp
我想在VisualStudio 2012中的.dll项目“MyIfa”中导入这4个文件。 我创建了一个新项目---> win32 --->我在向导中选择导出外部符号---->至少创建了项目(使用所有组件创建新文件夹)。
现在我需要做什么?
这是生成的新代码: 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文件来导出所有内部定义的类?
答案 0 :(得分:1)
如果您运行向导来创建包含一些示例导出的DLL项目,则可以看到从dll导出类和/或函数时需要遵循的格式。应该这么简单:
#include
生成的DLL头文件,并将解析为#define
的{{1}}'ed符号添加到类名称前。例如,向导将生成一个头文件,例如__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 ...
};