2014-01-27 85 views
0

我有4個文件:創建.dll文件

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

我想要導入這些4文件的一個.dll項目「 MyIfa「在VisualStudio 2012中。 我創建了一個新項目---> win32 --->我在嚮導中選擇了導出外部符號---->至少創建了項目(新文件夾是用所有組件創建的)。

現在我需要做什麼?

  • 添加四個文件(添加現有的項目)在我的項目和建設?
  • 在我的項目中添加四個文件(添加現有項目),包括.h 文件(mySender.h,myReceiver.h)和building?
  • 添加四個文件(添加現有項)在我的項目,包括.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

如果您運行嚮導以創建一個帶有一些示例導出的DLL項目,您可以看到從dll導出類和/或函數時需要遵循的格式。它應該如此簡單:

  1. 將您現有的源文件添加到DLL項目。
  2. 對於要導出的任何類,生成的DLL頭文件爲#include,並且將前綴#define'ed符號解析爲類名爲__declspec(dllexport)
  3. 重建。

例如,嚮導將已生成的頭文件,說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

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

如果你想自己的類從DLL導出,這樣做:

#include "header.h" 

class WIN32PROJECT2_API CMyClass 
{ 
    // ... blah ... 
}; 
+0

對於我在源代碼中使用的lybraries,您認爲這樣可以嗎?我的意思是... .dll知道如何鏈接所有? – CecchinoSMI

+0

@CecchinoSMI如果您現有的代碼對第三方庫具有任何依賴性,那麼您將不得不將它們添加到項目屬性中,或者(更好地)在源代碼中通過#pragma註釋(lib,「somelib.lib」)來執行它etc. –

+0

我很習慣操縱項目屬性中的所有依賴......所以我認爲我以這種方式進行...... – CecchinoSMI