MFC基礎知識(九)——幾種創建DLL動態庫的方法

本文主要介紹:用VS創建DLL動態庫的幾種方法:

                              1. 創建DLL工程+MFC頭文件

                              2. 創建DLL工程+空項目

                              3. 創建控制檯應用程序+MFC頭文件

                              4.創建含有對話框的動態庫

1.創建DLL工程+MFC頭文件

(1)應用程序設置:


(2)在生成的.h .cpp文件中修改程序:

.h文件

// 下列 ifdef 塊是創建使從 DLL 導出更簡單的
// 宏的標準方法。此 DLL 中的所有文件都是用命令行上定義的 DLLTEST_EXPORTS
// 符號編譯的。在使用此 DLL 的
// 任何其他項目上不應定義此符號。這樣,源文件中包含此文件的任何其他項目都會將
// DLLTEST_API 函數視爲是從 DLL 導入的,而此 DLL 則將用此宏定義的
// 符號視爲是被導出的。
#ifdef DLLTEST_EXPORTS
#define DLLTEST_API __declspec(dllexport)
#else
#define DLLTEST_API __declspec(dllimport)
#endif

#include "Sub.h"

// 此類是從 DllTest.dll 導出的
class DLLTEST_API CDllTest {
public:
	CDllTest(void);
	// TODO: 在此添加您的方法。
};

extern DLLTEST_API int nDllTest;

DLLTEST_API int fnDllTest(void);

extern "C" _declspec (dllexport) int Add(int a, int b);
.cpp文件:

#include "stdafx.h"
#include "DllTest.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// 唯一的應用程序對象
CWinApp theApp;

using namespace std;
/*
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	HMODULE hModule = ::GetModuleHandle(NULL);

	if (hModule != NULL)
	{
		// 初始化 MFC 並在失敗時顯示錯誤
		if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
		{
			// TODO: 更改錯誤代碼以符合您的需要
			_tprintf(_T("錯誤: MFC 初始化失敗\n"));
			nRetCode = 1;
		}
		else
		{
			// TODO: 在此處爲應用程序的行爲編寫代碼。
		}
	}
	else
	{
		// TODO: 更改錯誤代碼以符合您的需要
		_tprintf(_T("錯誤: GetModuleHandle 失敗\n"));
		nRetCode = 1;
	}

	return nRetCode;
} */


extern "C" _declspec (dllexport) int Add(int a, int b)
{
	//MFC初始化 也可以不初始化
	HMODULE hModule = ::GetModuleHandle(NULL);
	if (hModule != NULL)
	{
		// 初始化 MFC 並在失敗時顯示錯誤
		if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
		{
			AfxMessageBox(_T("MFC初始化失敗"));
		}
	}

	return a+b;
}
注:如果動態庫dll中,用到MFC的資源視圖,如:對話框(Dialog)、string table等資源,需要進行MFC初始化,不然調用時會出現錯誤;同時,調用改dll的工程中,也需要進行MFC的初始化,否則,資源文件會沒有作用,即不能顯示對話框,或者出現異常中斷。

2.創建DLL工程+空項目

(1)應用程序設置

(2)生成的工程沒有.h .cpp文件,需要自己創建與添加程序:

.h文件:

#pragma once

extern "C" _declspec (dllexport) int Add(int a, int b);
.cpp文件:

#include "sub.h"

extern "C" _declspec (dllexport) int Add(int a, int b)
{
	return a+b;
}

3. 創建控制檯應用程序+MFC

(1)應用程序設置


(2)工程屬性修改:

修改配置類型:

修改dll生成路徑:

(3)在生成的.h .cpp文件中修改程序:

.h文件:

#pragma once

#include "resource.h"

extern "C" _declspec (dllexport) int Add(int a, int b);
.cpp文件

#include "stdafx.h"
#include "AddDll.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 唯一的應用程序對象

CWinApp theApp;

using namespace std;

/*
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	HMODULE hModule = ::GetModuleHandle(NULL);

	if (hModule != NULL)
	{
		// 初始化 MFC 並在失敗時顯示錯誤
		if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
		{
			// TODO: 更改錯誤代碼以符合您的需要
			_tprintf(_T("錯誤: MFC 初始化失敗\n"));
			nRetCode = 1;
		}
		else
		{
			// TODO: 在此處爲應用程序的行爲編寫代碼。
		}
	}
	else
	{
		// TODO: 更改錯誤代碼以符合您的需要
		_tprintf(_T("錯誤: GetModuleHandle 失敗\n"));
		nRetCode = 1;
	}

	return nRetCode;
}
*/

extern "C" _declspec (dllexport) int Add(int a, int b)
{
	HMODULE hModule = ::GetModuleHandle(NULL);

	if (hModule != NULL)
	{
		// 初始化 MFC 並在失敗時顯示錯誤
		if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
		{
			AfxMessageBox(_T("MFC初始化失敗"));
		}
	}

	return a+b;
}

4. 創建含有對話框的動態庫

生成dll的工程創建方法可以使用以上三種方法的任意一個,需要注意的是,動態庫工程需要進行MFC初始化,同時調用DLL的工程,也需要MFC初始化,否則,對話框不能顯示也不會運行,即:程序運行時,會跳過對話框顯示程序。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章