參考資料:   Walkthrough: Creating and Using a Dynamic Link Library (C++)

dll的使用時機及意義:

強調重複使用程式碼, 而不必每次重新造輪子,只需要撰寫一次, 之後把這些函式庫引入並參考它們。

 

專案1: 建立 DLL 專案, 將類別加入至 DLL。

專案2: 建立主控台應用程式, 並呼叫剛剛所產生的DLL

-----------------------------------------------------------------------------------------------------------

專案1: 建立動態連結程式庫 (DLL)

1.在功能表列上,選擇 [檔案]、[新增]、[專案]。

2.在 [新的專案] 選擇[Visual C++],然後選取 [Win32]。

3.在中間窗格中,選取 [Win32 主控台應用程式]。

4.專案名稱: DllLib

5.在 [應用程式設定] 頁面上,在 [應用程式類型] 下,選取 [DLL]。

在功能表列上,選擇 [專案]],則 [加入新項目。]。 在 [加入新項目。] 對話方塊中,選取左窗格中,在 [Visual C++] 下,選取 [程式碼]。 在中間窗格中,選取 [標頭檔 (.h)]。 為標頭中指定的檔案名稱 (例如, MathFuncsDll.h 然後選擇 [加入] 按鈕。
// MathFuncsDll.h
#ifdef MATHFUNCSDLL_EXPORTS [註1] 
#define MATHFUNCSDLL_API __declspec(dllexport)
#else
#define MATHFUNCSDLL_API __declspec(dllimport) [註2]
#endif

namespace MathFuncs

{
    // This class is exported from the MathFuncsDll.dll
    class MyMathFuncs
    {
    public:
        // Returns a + b
        static MATHFUNCSDLL_API double Add(double a, double b);

        // Returns a - b
        static MATHFUNCSDLL_API double Subtract(double a, double b);

        // Returns a * b
        static MATHFUNCSDLL_API double Multiply(double a, double b);

        // Returns a / b
        // Throws const std::invalid_argument& if b is 0
        static MATHFUNCSDLL_API double Divide(double a, double b);
    };
}
-------------------------------------------------------------------------------------------------------------------------------------------------

[註1]
當 MATHFUNCSDLL_EXPORTS 符號定義, MATHFUNCSDLL_API 符號被替換成式


 __declspec(dllexport) 修飾詞可以讓函式由 DLL 匯出,以供其他應用程式使用。[專案1編譯時]


[註2]當 MATHFUNCSDLL_EXPORTS 定義時, MATHFUNCSDLL_API 定義在成員函式宣告的

__declspec(dllimport) 修飾詞可讓編譯器最佳化從DLL匯入的函式用於其他應用程式。[專案2編譯時]


同一個方案時, 專案2(CallDllLib)先執行編譯動作, 此時MATHFUNCSDLL_EXPORT尚未定義,

因此會解讀成 __declspec(dllimport) , 接著輪到專案1(DllLib),

由於MATHFUNCSDLL_EXPORT變成已定義, 因此會解讀成  __declspec(dllexport)

-------------------------------------------------------------------------------------------------------------------------------------------------
 
// MathFuncsDll.cpp : Defines the exported functions for the DLL application. // 

#include "stdafx.h"
#include "MathFuncsDll.h"
#include <stdexcept>
using
namespace std;
namespace
MathFuncs
{
double MyMathFuncs::Add(double a, double b)
{
return a + b;
}
double
MyMathFuncs::Subtract(double a, double b)
{
return
a - b;
}
double MyMathFuncs::Multiply(double a, double b)
{
return a * b;
}
double MyMathFuncs::Divide(double a, double b)
{
if (b == 0)
{
throw invalid_argument("b cannot be zero!");
}
return
a / b;
}
}

專案2: 呼叫動態連結程式庫 (DLL)

1. 建立C++ 應用程式,請選取 [檔案]],則 [新增], [專案]。

2. 在左窗格中,在 [Visual C++] 底下,選取 [Win32]。

3. 在中間窗格中,選取 [Win32 主控台應用程式]。

4. 專案名稱: CallDllLib

5. 在 [方案] 旁邊,選取下拉式清單中的 [加入至方案], 選擇剛剛建立的專案1 DllLib 專案。

6. 加入新參考

7. 加入Include路徑:DllLib

 

// CallDllLib.cpp // compile with: /EHsc /link DllLib.lib 
#include <iostream>

#include "MathFuncsDll.h"

using namespace std;

int main()
{
    double a = 7.4;
    int b = 99;

    cout << "a + b = " <<
        MathFuncs::MyMathFuncs::Add(a, b) << endl;
    cout << "a - b = " <<
        MathFuncs::MyMathFuncs::Subtract(a, b) << endl;
    cout << "a * b = " <<
        MathFuncs::MyMathFuncs::Multiply(a, b) << endl;
    cout << "a / b = " <<
        MathFuncs::MyMathFuncs::Divide(a, b) << endl;

    try
    {
        cout << "a / 0 = " <<
            MathFuncs::MyMathFuncs::Divide(a, 0) << endl;
    }
    catch (const invalid_argument &e)
    {
        cout << "Caught exception: " << e.what() << endl;
    }

    return 0;
}


[[註1]
 MathFuncs為namespace, MyMathFuncs 為class name

 

 

 

原始程式: 專案1:產生Dll Library   DllLib

              專案2:呼叫Dll Library CallDllLib


   Youtube 操作展示:

2013-04-16_專案1產生Dll Library

2013-04-16_專案2呼叫Dll Library

arrow
arrow
    全站熱搜

    me1237guy 發表在 痞客邦 留言(0) 人氣()