逐步解說:建立和使用您自己的動態連結程式庫 (c + +) Walkthrough: Create and use your own Dynamic Link Library (C++)

本逐步解說將示範如何使用 Visual Studio IDE 來建立您自己的動態連結程式庫 (DLL) 以 Microsoft c + + (MSVC) 撰寫。This step-by-step walkthrough shows how to use the Visual Studio IDE to create your own dynamic link library (DLL) written in Microsoft C++ (MSVC). 然後,它會顯示如何從另一個 c + + 應用程式使用 DLL。Then it shows how to use the DLL from another C++ app. Dll (也稱為 UNIX 作業系統中的 共用程式庫) 是其中一種最有用的 Windows 元件。DLLs (also known as shared libraries in UNIX-based operating systems) are one of the most useful kinds of Windows components. 您可以使用它們作為共用程式碼和資源的方式,並縮減應用程式的大小。You can use them as a way to share code and resources, and to shrink the size of your apps. Dll 甚至可以讓您更輕鬆地服務及擴充您的應用程式。DLLs can even make it easier to service and extend your apps.

在這個逐步解說中,您將建立可執行一些數學函數的 DLL。In this walkthrough, you'll create a DLL that implements some math functions. 然後,您將建立主控台應用程式,該應用程式會使用來自 DLL 的函式。Then you'll create a console app that uses the functions from the DLL. 您也將瞭解 Windows Dll 中使用的一些程式設計技巧和慣例。You'll also get an introduction to some of the programming techniques and conventions used in Windows DLLs.

這份逐步解說涵蓋下列工作:This walkthrough covers these tasks:

  • 在 Visual Studio 中建立 DLL 專案。Create a DLL project in Visual Studio.

  • 將匯出的函式和變數新增至 DLL。Add exported functions and variables to the DLL.

  • 在 Visual Studio 中建立主控台應用程式專案。Create a console app project in Visual Studio.

  • 在主控台應用程式中使用從 DLL 匯入的函式與變數。Use the functions and variables imported from the DLL in the console app.

  • 執行完成的應用程式。Run the completed app.

如同靜態連結的程式庫,DLL 會依名稱 匯出 變數、函式和資源。Like a statically linked library, a DLL exports variables, functions, and resources by name. 用戶端應用程式會匯 名稱以使用這些變數、函式和資源。A client app imports the names to use those variables, functions, and resources. 與靜態連結程式庫不同,Windows 會在載入時或在執行階段將應用程式中的匯入連線至 DLL 中的匯出,而不是在連結時將其連線。Unlike a statically linked library, Windows connects the imports in your app to the exports in a DLL at load time or at run time, instead of connecting them at link time. Windows 需要不屬於標準 C++ 編譯模型一部分的額外資訊來建立這些連線。Windows requires extra information that isn't part of the standard C++ compilation model to make these connections. MSVC 編譯器會將一些 Microsoft 特定的延伸模組實作於 C++,以提供這些額外資訊。The MSVC compiler implements some Microsoft-specific extensions to C++ to provide this extra information. 我們會逐步說明這些延伸模組。We explain these extensions as we go.

此逐步解說會建立兩個 Visual Studio 方案;一個是建置 DLL,另一個是建置用戶端應用程式。This walkthrough creates two Visual Studio solutions; one that builds the DLL, and one that builds the client app. DLL 會使用 C 呼叫慣例。The DLL uses the C calling convention. 您可以從以其他程式設計語言撰寫的應用程式呼叫它,只要平臺、呼叫慣例和連結慣例相符即可。It can be called from apps written in other programming languages, as long as the platform, calling conventions, and linking conventions match. 用戶端應用程式會使用「隱含連結」,其中 Windows 會在載入時將應用程式連結到 DLL。The client app uses implicit linking, where Windows links the app to the DLL at load-time. 如同靜態連結程式庫中的函式,此連結可讓應用程式呼叫 DLL 提供的函式。This linking lets the app call the DLL-supplied functions just like the functions in a statically linked library.

本逐步解說不涵蓋某些常見的情況。This walkthrough doesn't cover some common situations. 程式碼不會顯示其他程式設計語言使用 c + + Dll。The code doesn't show the use of C++ DLLs by other programming languages. 它不會示範如何 建立僅含資源的 DLL,或如何在執行時間使用 明確連結 來載入 dll,而不是在載入時使用。It doesn't show how to create a resource-only DLL, or how to use explicit linking to load DLLs at run-time rather than at load-time. 請放心,您可以使用 MSVC 和 Visual Studio 來執行上述所有作業。Rest assured, you can use MSVC and Visual Studio to do all these things.

如需 DLL 的詳細資訊連結,請參閱在 Visual Studio 中建立 C/C++ DLLFor links to more information about DLLs, see Create C/C++ DLLs in Visual Studio. 如需隱含連結和明確連結的詳細資訊,請參閱 判斷要使用哪一個連結方法For more information about implicit linking and explicit linking, see Determine which linking method to use. 如需有關建立 c + + Dll 以搭配使用 C 語言連結慣例之程式設計語言的詳細資訊,請參閱 匯出 c + + 函式以用於 c 語言可執行檔For information about creating C++ DLLs for use with programming languages that use C-language linkage conventions, see Exporting C++ functions for use in C-language executables. 如需如何建立 DLL 以與 .NET 語言搭配使用的資訊,請參閱從 Visual Basic 應用程式呼叫 DLL 函式For information about how to create DLLs for use with .NET languages, see Calling DLL Functions from Visual Basic Applications.

PrerequisitesPrerequisites

  • 執行 Microsoft Windows 7 或更新版本的電腦。A computer that runs Microsoft Windows 7 or later versions. 建議使用 Windows 10 以獲得最佳開發體驗。We recommend Windows 10 for the best development experience.
  • 一份 Visual Studio 複本。A copy of Visual Studio. 如需如何下載並安裝 Visual Studio 的詳細資訊,請參閱安裝 Visual StudioFor information on how to download and install Visual Studio, see Install Visual Studio. 當您執行安裝程式時,請確認已選取 使用 C++ 的桌面開發 工作負載。When you run the installer, make sure that the Desktop development with C++ workload is checked. 如果您在安裝 Visual Studio 時未安裝此工作負載,也不用擔心。Don't worry if you didn't install this workload when you installed Visual Studio. 您可以再次執行安裝程式並立即安裝。You can run the installer again and install it now.

    使用 C++ 開發桌面Desktop development with C++

  • 一份 Visual Studio 複本。A copy of Visual Studio. 如需有關如何下載及安裝 Visual Studio 2015 的詳細資訊,請參閱 安裝 Visual Studio 2015For information on how to download and install Visual Studio 2015, see Install Visual Studio 2015. 您可以使用 自訂 安裝來安裝 c + + 編譯器和工具,因為預設不會安裝它們。Use a Custom installation to install the C++ compiler and tools, since they're not installed by default.
  • 了解使用 Visual Studio IDE 的基本概念。An understanding of the basics of using the Visual Studio IDE. 如果您先前使用過 Windows 傳統型應用程式,您應能輕鬆跟上。If you've used Windows desktop apps before, you can probably keep up. 如需簡介,請參閱 Visual Studio IDE 功能導覽For an introduction, see Visual Studio IDE feature tour.

  • 了解足夠的 C++ 語言基本概念。An understanding of enough of the fundamentals of the C++ language to follow along. 別擔心,我們不會進行太複雜的操作。Don't worry, we don't do anything too complicated.

注意

本逐步解說假設您使用的是 Visual Studio 2017 15.9 版或更新版本。This walkthrough assumes you're using Visual Studio 2017 version 15.9 or later. 某些舊版的 Visual Studio 2017 在程式碼範本中有瑕疵,或使用不同的使用者介面對話方塊。Some earlier versions of Visual Studio 2017 had defects in the code templates, or used different user interface dialogs. 若要避免問題,請使用 Visual Studio 安裝程式將 Visual Studio 2017 更新至15.9 版或更新版本。To avoid problems, use the Visual Studio Installer to update Visual Studio 2017 to version 15.9 or later.

建立 DLL 專案Create the DLL project

在這組工作中,您可以為您的 DLL 建立專案、新增程式碼及加以建置。In this set of tasks, you create a project for your DLL, add code, and build it. 首先請啟動 Visual Studio IDE,並根據您的需要登入。To begin, start the Visual Studio IDE, and sign in if you need to. 這些指示會根據您所使用的 Visual Studio 版本而稍有不同。The instructions vary slightly depending on which version of Visual Studio you're using. 請確認您在本頁面左上角的控制項中選取正確版本。Make sure you have the correct version selected in the control in the upper left of this page.

在 Visual Studio 2019 中建立 DLL 專案To create a DLL project in Visual Studio 2019

  1. 在功能表列上,選擇 [檔案][新增] > [專案] > ,以開啟 [建立新專案] 對話方塊。On the menu bar, choose File > New > Project to open the Create a New Project dialog box.

    建立新的 DLL 專案Create a new DLL project

  2. 在對話方塊頂端,將 [語言] 設定為 C++,將 [平台] 設定為 Windows,並將 [專案類型] 設定為 程式庫At the top of the dialog, set Language to C++, set Platform to Windows, and set Project type to Library.

  3. 從專案類型的篩選清單中,選取 [ 動態連結程式庫] (DLL),然後選擇 [下一步]From the filtered list of project types, select Dynamic-link Library (DLL), and then choose Next.

  4. 在 [設定您的新專案] 頁面的 [專案名稱] 方塊中輸入 MathLibrary ,以指定專案的名稱。In the Configure your new project page, enter MathLibrary in the Project name box to specify a name for the project. 保留預設 位置方案名稱 值。Leave the default Location and Solution name values. 設定 解決方案建立新的解決方案Set Solution to Create new solution. 取消核取 [將方案和專案放置在相同的目錄中 (如果已核取)]。Uncheck Place solution and project in the same directory if it's checked.

  5. 選擇 [建立] 按鈕以建立專案。Choose the Create button to create the project.

建立方案時,您可以在 Visual Studio 的 [ 方案總管 ] 視窗中看到產生的專案和原始程式檔。When the solution is created, you can see the generated project and source files in the Solution Explorer window in Visual Studio.

已反白顯示數學程式庫 Visual Studio 2019 方案總管視窗的螢幕擷取畫面。Screenshot of the Visual Studio 2019 Solution Explorer window with the Math Library highlighted.

在 Visual Studio 2017 中建立 DLL 專案To create a DLL project in Visual Studio 2017

  1. 在功能表列上,選擇 [檔案][新增] > [專案] > ,以開啟 [新增專案] 對話方塊。On the menu bar, choose File > New > Project to open the New Project dialog box.

  2. 在 [新增專案] 對話方塊的左窗格中,選取 [已安裝 > Visual C++ > Windows 桌面]。In the left pane of the New Project dialog box, select Installed > Visual C++ > Windows Desktop. 在中央窗格中,選取 [ 動態連結程式庫] (DLL)In the center pane, select Dynamic-Link Library (DLL). 在 [名稱] 方塊中輸入 MathLibrary ,以指定專案的名稱。Enter MathLibrary in the Name box to specify a name for the project. 保留預設 位置方案名稱 值。Leave the default Location and Solution name values. 設定 解決方案建立新的解決方案Set Solution to Create new solution. 如果未核取 [ 為方案建立目錄 ],請檢查它。Check Create directory for solution if it's unchecked.

    2017 Visual Studio [新增專案] 對話方塊的螢幕擷取畫面,其中顯示 [名稱] 文字方塊中的 Math Library。Screenshot of the Visual Studio 2017 New Project dialog box showing Math Library in the Name text box.

  3. 選擇 [確定] 按鈕以建立專案。Choose the OK button to create the project.

建立方案時,您可以在 Visual Studio 的 [ 方案總管 ] 視窗中看到產生的專案和原始程式檔。When the solution is created, you can see the generated project and source files in the Solution Explorer window in Visual Studio.

已反白顯示數學程式庫 Visual Studio 2017 方案總管視窗的螢幕擷取畫面。Screenshot of the Visual Studio 2017 Solution Explorer window with the Math Library highlighted.

若要在 Visual Studio 2015 及更舊版本中建立 DLL 專案To create a DLL project in Visual Studio 2015 and older versions

  1. 在功能表列上 ,選擇 [ 檔案 > 新增 > 專案]。On the menu bar, choose File > New > Project.

  2. 在 [新增專案] 對話方塊的左窗格中,展開 [已安裝] > [範本] 並選取 [Visual C++],然後在中央窗格中選取 [Win32 主控台應用程式]。In the left pane of the New Project dialog box, expand Installed > Templates, and select Visual C++, and then in the center pane, select Win32 Console Application. 在 [名稱] 編輯方塊中輸入 MathLibrary ,以指定專案的名稱。Enter MathLibrary in the Name edit box to specify a name for the project. 保留預設 位置方案名稱 值。Leave the default Location and Solution name values. 設定 解決方案建立新的解決方案Set Solution to Create new solution. 如果未核取 [ 為方案建立目錄 ],請檢查它。Check Create directory for solution if it's unchecked.

    2015 Visual Studio [新增專案] 對話方塊的螢幕擷取畫面,其中顯示 [名稱] 文字方塊中的 Math Library。Screenshot of the Visual Studio 2015 New Project dialog box showing Math Library in the Name text box.

  3. 選擇 [確定] 按鈕以關閉 [新增專案] 對話方塊,並啟動 [Win32 應用程式精靈]。Choose the OK button to dismiss the New Project dialog and start the Win32 Application Wizard.

    Win32 應用程式精靈總覽Win32 Application Wizard Overview

  4. 選擇 [下一步] 按鈕。Choose the Next button. 在 [應用程式設定] 頁面的 [應用程式類型] 下,選取 [DLL]。On the Application Settings page, under Application type, select DLL.

    在 Win32 應用程式中建立 DLLCreate DLL in Win32 Application Wizard

  5. 選擇 [完成] 按鈕以建立專案。Choose the Finish button to create the project.

精靈完成方案後,您可以在 Visual Studio 的 [方案總管] 視窗中,看到產生的專案和來源檔案。When the wizard completes the solution, you can see the generated project and source files in the Solution Explorer window in Visual Studio.

已反白顯示數學程式庫 Visual Studio 2015 方案總管視窗的螢幕擷取畫面。Screenshot of the Visual Studio 2015 Solution Explorer window with the Math Library highlighted.

目前,此 DLL 還不能做太多事。Right now, this DLL doesn't do very much. 接下來,您將建立標頭檔來宣告 DLL 匯出的函式,然後將函式定義加入 DLL 以使其更有用。Next, you'll create a header file to declare the functions your DLL exports, and then add the function definitions to the DLL to make it more useful.

將標頭檔新增至 DLLTo add a header file to the DLL

  1. 若要建立函式的標頭檔,請在功能表列上選擇 [專案 > 加入新專案]。To create a header file for your functions, on the menu bar, choose Project > Add New Item.

  2. 在 [新增項目] 對話方塊的左窗格中,選取 [Visual C++]。In the Add New Item dialog box, in the left pane, select Visual C++. 在中間窗格中,選取 [標頭檔 (.h)] 。In the center pane, select Header File (.h). MathLibrary 指定為標頭檔的名稱。Specify MathLibrary.h as the name for the header file.

    在 [加入新專案] 對話方塊中新增標頭Add header in Add New Item dialog

  3. 選擇 [新增] 按鈕以產生空白標頭檔,這會顯示在新的編輯器視窗中。Choose the Add button to generate a blank header file, which is displayed in a new editor window.

    編輯器中的空白 MathLibrary .h 檔案Empty MathLibrary.h file in editor

  4. 使用此程式碼來取代標頭檔的內容:Replace the contents of the header file with this code:

    // MathLibrary.h - Contains declarations of math functions
    #pragma once
    
    #ifdef MATHLIBRARY_EXPORTS
    #define MATHLIBRARY_API __declspec(dllexport)
    #else
    #define MATHLIBRARY_API __declspec(dllimport)
    #endif
    
    // The Fibonacci recurrence relation describes a sequence F
    // where F(n) is { n = 0, a
    //               { n = 1, b
    //               { n > 1, F(n-2) + F(n-1)
    // for some initial integral values a and b.
    // If the sequence is initialized F(0) = 1, F(1) = 1,
    // then this relation produces the well-known Fibonacci
    // sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
    
    // Initialize a Fibonacci relation sequence
    // such that F(0) = a, F(1) = b.
    // This function must be called before any other function.
    extern "C" MATHLIBRARY_API void fibonacci_init(
        const unsigned long long a, const unsigned long long b);
    
    // Produce the next value in the sequence.
    // Returns true on success and updates current value and index;
    // false on overflow, leaves current value and index unchanged.
    extern "C" MATHLIBRARY_API bool fibonacci_next();
    
    // Get the current value in the sequence.
    extern "C" MATHLIBRARY_API unsigned long long fibonacci_current();
    
    // Get the position of the current value in the sequence.
    extern "C" MATHLIBRARY_API unsigned fibonacci_index();
    

在具有兩個初始值的情況下,此標頭檔會宣告一些函式來產生一般化的 Fibonacci 序列。This header file declares some functions to produce a generalized Fibonacci sequence, given two initial values. fibonacci_init(1, 1) 的呼叫會產生熟悉的 Fibonacci 數字序列。A call to fibonacci_init(1, 1) generates the familiar Fibonacci number sequence.

請注意檔案頂端的前置處理器陳述式。Notice the preprocessor statements at the top of the file. DLL 專案的新專案範本會將 專案名稱_匯出 新增至已定義的預處理器宏。The new project template for a DLL project adds PROJECTNAME_EXPORTS to the defined preprocessor macros. 在此範例中,當您的 MathLibrary DLL 專案完成建置時,Visual Studio 會定義 MATHLIBRARY_EXPORTSIn this example, Visual Studio defines MATHLIBRARY_EXPORTS when your MathLibrary DLL project is built.

定義 MATHLIBRARY_EXPORTS 巨集後,MATHLIBRARY_API 巨集會在函式宣告中設定 __declspec(dllexport) 修飾詞。When the MATHLIBRARY_EXPORTS macro is defined, the MATHLIBRARY_API macro sets the __declspec(dllexport) modifier on the function declarations. 此修飾詞會告知編譯器和連結器從 DLL 匯出函式或變數,以供其他應用程式使用。This modifier tells the compiler and linker to export a function or variable from the DLL for use by other applications. 如果 MATHLIBRARY_EXPORTS 並未定義 (例如當用戶端應用程式包含標頭檔時),則 MATHLIBRARY_API 會將 __declspec(dllimport) 修飾詞套用至宣告。When MATHLIBRARY_EXPORTS is undefined, for example, when the header file is included by a client application, MATHLIBRARY_API applies the __declspec(dllimport) modifier to the declarations. 此修飾詞會將應用程式中函式或變數的匯入最佳化。This modifier optimizes the import of the function or variable in an application. 如需詳細資訊,請參閱 dllexport、dllimportFor more information, see dllexport, dllimport.

將實作新增至 DLLTo add an implementation to the DLL

  1. 方案總管 中,以滑鼠右鍵按一下 [來源 檔案] 節點,然後選擇 [加入 > 新專案]。In Solution Explorer, right-click on the Source Files node and choose Add > New Item. 以您在上一個步驟中加入新標頭檔的相同方式,建立名為 MathLibrary 的新 .cpp 檔案。Create a new .cpp file called MathLibrary.cpp, in the same way that you added a new header file in the previous step.

  2. 在編輯器視窗中,選取 [MathLibrary.cpp] 索引標籤 (如果已經開啟)。In the editor window, select the tab for MathLibrary.cpp if it's already open. 如果沒有,請 在方案總管 中,按兩下 MathLibrary 專案的 [原始 程式檔] 資料夾中的 [ MathLibrary ],將它開啟。If not, in Solution Explorer, double-click MathLibrary.cpp in the Source Files folder of the MathLibrary project to open it.

  3. 在編輯器中,以下列程式碼取代 MathLibrary.cpp 檔案的內容:In the editor, replace the contents of the MathLibrary.cpp file with the following code:

    // MathLibrary.cpp : Defines the exported functions for the DLL.
    #include "pch.h" // use stdafx.h in Visual Studio 2017 and earlier
    #include <utility>
    #include <limits.h>
    #include "MathLibrary.h"
    
    // DLL internal state variables:
    static unsigned long long previous_;  // Previous value, if any
    static unsigned long long current_;   // Current sequence value
    static unsigned index_;               // Current seq. position
    
    // Initialize a Fibonacci relation sequence
    // such that F(0) = a, F(1) = b.
    // This function must be called before any other function.
    void fibonacci_init(
        const unsigned long long a,
        const unsigned long long b)
    {
        index_ = 0;
        current_ = a;
        previous_ = b; // see special case when initialized
    }
    
    // Produce the next value in the sequence.
    // Returns true on success, false on overflow.
    bool fibonacci_next()
    {
        // check to see if we'd overflow result or position
        if ((ULLONG_MAX - previous_ < current_) ||
            (UINT_MAX == index_))
        {
            return false;
        }
    
        // Special case when index == 0, just return b value
        if (index_ > 0)
        {
            // otherwise, calculate next sequence value
            previous_ += current_;
        }
        std::swap(current_, previous_);
        ++index_;
        return true;
    }
    
    // Get the current value in the sequence.
    unsigned long long fibonacci_current()
    {
        return current_;
    }
    
    // Get the current index position in the sequence.
    unsigned fibonacci_index()
    {
        return index_;
    }
    
  1. 在編輯器視窗中,選取 [MathLibrary.cpp] 索引標籤 (如果已經開啟)。In the editor window, select the tab for MathLibrary.cpp if it's already open. 如果沒有,請 在方案總管 中,按兩下 MathLibrary 專案的 [原始 程式檔] 資料夾中的 [ MathLibrary ],將它開啟。If not, in Solution Explorer, double-click MathLibrary.cpp in the Source Files folder of the MathLibrary project to open it.

  2. 在編輯器中,以下列程式碼取代 MathLibrary.cpp 檔案的內容:In the editor, replace the contents of the MathLibrary.cpp file with the following code:

    // MathLibrary.cpp : Defines the exported functions for the DLL.
    #include "stdafx.h" // use pch.h in Visual Studio 2019 and later
    #include <utility>
    #include <limits.h>
    #include "MathLibrary.h"
    
    // DLL internal state variables:
    static unsigned long long previous_;  // Previous value, if any
    static unsigned long long current_;   // Current sequence value
    static unsigned index_;               // Current seq. position
    
    // Initialize a Fibonacci relation sequence
    // such that F(0) = a, F(1) = b.
    // This function must be called before any other function.
    void fibonacci_init(
        const unsigned long long a,
        const unsigned long long b)
    {
        index_ = 0;
        current_ = a;
        previous_ = b; // see special case when initialized
    }
    
    // Produce the next value in the sequence.
    // Returns true on success, false on overflow.
    bool fibonacci_next()
    {
        // check to see if we'd overflow result or position
        if ((ULLONG_MAX - previous_ < current_) ||
            (UINT_MAX == index_))
        {
            return false;
        }
    
        // Special case when index == 0, just return b value
        if (index_ > 0)
        {
            // otherwise, calculate next sequence value
            previous_ += current_;
        }
        std::swap(current_, previous_);
        ++index_;
        return true;
    }
    
    // Get the current value in the sequence.
    unsigned long long fibonacci_current()
    {
        return current_;
    }
    
    // Get the current index position in the sequence.
    unsigned fibonacci_index()
    {
        return index_;
    }
    

為了確認目前為止一切順利,請編譯動態連結程式庫。To verify that everything works so far, compile the dynamic link library. 若要進行編譯,請選擇 > 功能表列上的 [組建 組建方案]。To compile, choose Build > Build Solution on the menu bar. DLL 和相關的編譯器輸出會放在方案資料夾正下方的 [ Debug ] 資料夾中。The DLL and related compiler output are placed in a folder called Debug directly below the solution folder. 如果您建立發行組建,則會將輸出放在名為 release 的資料夾中。If you create a Release build, the output is placed in a folder called Release. 輸出應該看起來像這樣:The output should look something like this:

1>------ Build started: Project: MathLibrary, Configuration: Debug Win32 ------
1>pch.cpp
1>dllmain.cpp
1>MathLibrary.cpp
1>Generating Code...
1>   Creating library C:\Users\username\Source\Repos\MathLibrary\Debug\MathLibrary.lib and object C:\Users\username\Source\Repos\MathLibrary\Debug\MathLibrary.exp
1>MathLibrary.vcxproj -> C:\Users\username\Source\Repos\MathLibrary\Debug\MathLibrary.dll
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
1>------ Build started: Project: MathLibrary, Configuration: Debug Win32 ------
1>stdafx.cpp
1>dllmain.cpp
1>MathLibrary.cpp
1>Generating Code...
1>   Creating library C:\Users\username\Source\Repos\MathLibrary\Debug\MathLibrary.lib and object C:\Users\username\Source\Repos\MathLibrary\Debug\MathLibrary.exp
1>MathLibrary.vcxproj -> C:\Users\username\Source\Repos\MathLibrary\Debug\MathLibrary.dll
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
1>------ Build started: Project: MathLibrary, Configuration: Debug Win32 ------
1>MathLibrary.cpp
1>dllmain.cpp
1>Generating Code...
1>   Creating library C:\Users\username\Source\Repos\MathLibrary\Debug\MathLibrary.lib and object C:\Users\username\Source\Repos\MathLibrary\Debug\MathLibrary.exp
1>MathLibrary.vcxproj -> C:\Users\username\Source\Repos\MathLibrary\Debug\MathLibrary.dll
1>MathLibrary.vcxproj -> C:\Users\username\Source\Repos\MathLibrary\Debug\MathLibrary.pdb (Partial PDB)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

恭喜,您已使用 Visual Studio 建立 DLL!Congratulations, you've created a DLL using Visual Studio! 接下來,您將建立一個用戶端應用程式,該應用程式使用由 DLL 匯出的函式。Next, you'll create a client app that uses the functions exported by the DLL.

建立使用 DLL 的用戶端應用程式Create a client app that uses the DLL

當您建立 DLL 時,請考慮用戶端應用程式可能使用它的方式。When you create a DLL, think about how client apps may use it. 若要呼叫函式或存取由 DLL 匯出的資料,用戶端原始程式碼在編譯階段必須有宣告可用。To call the functions or access the data exported by a DLL, client source code must have the declarations available at compile time. 在連結時,連結器需要資訊來解析函式呼叫或資料存取。At link time, the linker requires information to resolve the function calls or data accesses. DLL 會在匯 入程式庫 中提供這項資訊,這是一個檔案,其中包含如何尋找函式和資料,而不是實際程式碼的相關資訊。A DLL supplies this information in an import library, a file that contains information about how to find the functions and data, instead of the actual code. 並且,在執行階段,DLL 必須可供用戶端使用,位於作業系統可以找到的位置。And at run time, the DLL must be available to the client, in a location that the operating system can find.

無論是您自己或協力廠商,用戶端應用程式專案都需要使用多個資訊片段才能使用 DLL。Whether it's your own or from a third-party, your client app project needs several pieces of information to use a DLL. 它需要找出宣告 DLL 匯出的標頭、連結器的匯入程式庫,以及 DLL 本身。It needs to find the headers that declare the DLL exports, the import libraries for the linker, and the DLL itself. 其中一個解決方法是將所有這些檔案複製到您的用戶端專案。One solution is to copy all of these files into your client project. 針對不太可能在您開發用戶端時變更的第三方 DLL,此方法可能是使用它們的最佳方法。For third-party DLLs that are unlikely to change while your client is in development, this method may be the best way to use them. 不過,如果您也要建置 DLL,則最好避免重複。However, when you also build the DLL, it's better to avoid duplication. 如果您在開發期間建立 DLL 檔案的本機複本,您可能會不小心變更一個複本中的標頭檔,而不是使用過期的程式庫。If you make a local copy of DLL files that are under development, you may accidentally change a header file in one copy but not the other, or use an out-of-date library.

若要避免同步處理常式代碼,建議您在用戶端專案中設定 include 路徑,以便直接從 DLL 專案包含 DLL 標頭檔。To avoid out-of-sync code, we recommend you set the include path in your client project to include the DLL header files directly from your DLL project. 此外,也在您的用戶端專案中設定程式庫路徑,以包含 DLL 專案中的 DLL 匯入程式庫。Also, set the library path in your client project to include the DLL import libraries from the DLL project. 最後,從 DLL 專案將建立的 DLL 複製到您的用戶端組建輸出目錄中。And finally, copy the built DLL from the DLL project into your client build output directory. 此步驟可讓用戶端應用程式使用您所建置的相同 DLL 程式碼。This step allows your client app to use the same DLL code you build.

若要在 Visual Studio 中建立用戶端應用程式To create a client app in Visual Studio

  1. 在功能表列上 ,選擇 [ 檔案 > 新增 > 專案 ] 以開啟 [ 建立新專案 ] 對話方塊。On the menu bar, choose File > New > Project to open the Create a new project dialog box.

  2. 在對話方塊頂端,將 [語言] 設定為 C++,將 [平台] 設定為 Windows,並將 [專案類型] 設定為 主控台At the top of the dialog, set Language to C++, set Platform to Windows, and set Project type to Console.

  3. 從專案類型的篩選清單中,選擇 [主控台應用程式],然後選擇 [下一步]。From the filtered list of project types, choose Console App then choose Next.

  4. 在 [設定您的新專案] 頁面的 [專案名稱] 方塊中輸入 >mathclient.cpp ,以指定專案的名稱。In the Configure your new project page, enter MathClient in the Project name box to specify a name for the project. 保留預設 位置方案名稱 值。Leave the default Location and Solution name values. 設定 解決方案建立新的解決方案Set Solution to Create new solution. 取消核取 [將方案和專案放置在相同的目錄中 (如果已核取)]。Uncheck Place solution and project in the same directory if it's checked.

    [建立新專案] 對話方塊的螢幕擷取畫面,其中已醒目提示主控台應用程式選項。Screenshot of the Create a new project dialog box with the Console App option highlighted.

  5. 選擇 [建立] 按鈕以建立用戶端專案。Choose the Create button to create the client project.

系統會為您建立基本的主控台應用程式專案。A minimal console application project is created for you. 主要來源檔案的名稱,與您先前輸入的名稱相同。The name for the main source file is the same as the project name that you entered earlier. 在此範例中,其名稱是 MathClient.cppIn this example, it's named MathClient.cpp. 您可以建置該檔案,但該檔案尚不會使用您的 DLL。You can build it, but it doesn't use your DLL yet.

在 Visual Studio 2017 中建立用戶端應用程式To create a client app in Visual Studio 2017

  1. 若要建立將使用您建立之 DLL 的 C++ 應用程式,請在功能表列上選擇 [檔案][新增] > [專案] > 。To create a C++ app that uses the DLL that you created, on the menu bar, choose File > New > Project.

  2. 在 [新增專案] 對話方塊左窗格中的 [已安裝] > [Visual C++] 下方,選取 [Windows 傳統型]。In the left pane of the New Project dialog, select Windows Desktop under Installed > Visual C++. 在中央窗格中,選取 [ Windows 主控台應用程式]。In the center pane, select Windows Console Application. 在 [名稱] 編輯方塊中指定專案的名稱 >mathclient.cppSpecify the name for the project, MathClient, in the Name edit box. 保留預設 位置方案名稱 值。Leave the default Location and Solution name values. 設定 解決方案建立新的解決方案Set Solution to Create new solution. 如果未核取 [ 為方案建立目錄 ],請檢查它。Check Create directory for solution if it's unchecked.

    [新增專案] 對話方塊的螢幕擷取畫面,其中已安裝 > Visual C + + > 選取 [windows 桌面],並醒目提示 [Windows 主控台應用程式],並在 [名稱] 文字方塊中輸入數學用戶端。Screenshot of the New Project dialog box with Installed > Visual C plus plus > Windows Desktop selected, Windows Console Application highlighted, and Math Client typed in the Name text box.

  3. 選擇 [確定] 以建立用戶端應用程式專案。Choose OK to create the client app project.

系統會為您建立基本的主控台應用程式專案。A minimal console application project is created for you. 主要來源檔案的名稱,與您先前輸入的名稱相同。The name for the main source file is the same as the project name that you entered earlier. 在此範例中,其名稱是 MathClient.cppIn this example, it's named MathClient.cpp. 您可以建置該檔案,但該檔案尚不會使用您的 DLL。You can build it, but it doesn't use your DLL yet.

在 Visual Studio 2015 中建立用戶端應用程式To create a client app in Visual Studio 2015

  1. 若要建立將使用您建立之 DLL 的 C++ 應用程式,請在功能表列上選擇 [檔案][新增] > [專案] > 。To create a C++ app that uses the DLL that you created, on the menu bar, choose File > New > Project.

  2. 在 [新增專案] 對話方塊左窗格中的 [已安裝] > [範本] > [Visual C++] 下方,選取 Win32In the left pane of the New Project dialog, select Win32 under Installed > Templates > Visual C++. 在中央窗格中,選取 [Win32 主控台應用程式] 。In the center pane, select Win32 Console Application. 在 [名稱] 編輯方塊中指定專案的名稱 >mathclient.cppSpecify the name for the project, MathClient, in the Name edit box. 保留預設 位置方案名稱 值。Leave the default Location and Solution name values. 設定 解決方案建立新的解決方案Set Solution to Create new solution. 如果未核取 [ 為方案建立目錄 ],請檢查它。Check Create directory for solution if it's unchecked.

    [新增專案] 對話方塊的螢幕擷取畫面,其中包含已安裝的 > 範本 > Visual C + + 選取的 > Win32、Win32 主控台應用程式 Visual C 加上反白顯示,以及 [名稱] 文字方塊中輸入的數學用戶端。Screenshot of the New Project dialog box with Installed > Templates > Visual C plus plus > Win32 selected, Win32 Console Application Visual C plus plus highlighted, and Math Client typed in the Name text box.

  3. 選擇 [確定] 按鈕以關閉 [新增專案] 對話方塊,並啟動 [Win32 應用程式精靈]。Choose the OK button to dismiss the New Project dialog and start the Win32 Application Wizard. 在 [Win32 應用程式精靈] 對話方塊的 [概觀] 頁面上,選擇 [下一步] 按鈕。On the Overview page of the Win32 Application Wizard dialog box, choose the Next button.

  4. 在 [應用程式設定] 頁面的 [應用程式類型] 下方,選取 [主控台應用程式] (若尚未選取)。On the Application Settings page, under Application type, select Console application if it isn't already selected.

  5. 選擇 [完成] 按鈕以建立專案。Choose the Finish button to create the project.

精靈完成時,會為您建立最基本的主控台應用程式專案。When the wizard finishes, a minimal console application project is created for you. 主要來源檔案的名稱,與您先前輸入的名稱相同。The name for the main source file is the same as the project name that you entered earlier. 在此範例中,其名稱是 MathClient.cppIn this example, it's named MathClient.cpp. 您可以建置該檔案,但該檔案尚不會使用您的 DLL。You can build it, but it doesn't use your DLL yet.

接下來,若要在原始程式碼中呼叫 MathLibrary 函式,您的專案必須包含 MathLibrary .h 檔案。Next, to call the MathLibrary functions in your source code, your project must include the MathLibrary.h file. 您可以將此標頭檔複製到用戶端應用程式專案,然後將其新增至專案作為現有的項目。You could copy this header file into your client app project, then add it to the project as an existing item. 這個方法對於第三方程式庫是理想選擇。This method can be a good choice for third-party libraries. 但是,如果您同時處理 DLL 和用戶端的程式碼,標頭檔可能會不同步。若要避免這個問題,請在專案中設定 其他 Include 目錄 路徑,以包含原始標頭的路徑。However, if you're working on the code for your DLL and your client at the same time, the header files could get out of sync. To avoid this issue, set the Additional Include Directories path in your project to include the path to the original header.

將 DLL 標頭新增至您的 Include 路徑To add the DLL header to your include path

  1. 以滑鼠右鍵按一下 [方案總管] 中的 MathClient 節點以開啟 [屬性頁] 對話方塊。Right-click on the MathClient node in Solution Explorer to open the Property Pages dialog.

  2. 在 [設定] 下拉式清單方塊中,選取 [所有 設定] (如果尚未選取)。In the Configuration drop-down box, select All Configurations if it's not already selected.

  3. 在左窗格中,選取 [設定 屬性 > C/c + + > 一般]。In the left pane, select Configuration Properties > C/C++ > General.

  4. 在屬性窗格中,選取 [其他 Include 目錄] 編輯方塊旁的下拉式清單控制項,然後選擇 [編輯]。In the property pane, select the drop-down control next to the Additional Include Directories edit box, and then choose Edit.

    編輯其他 Include 目錄屬性Edit the Additional Include Directories property

  5. 在上方窗格中按兩下 [其他 Include 目錄] 對話方塊以啟用編輯控制項。Double-click in the top pane of the Additional Include Directories dialog box to enable an edit control. 或者,選擇資料夾圖示來建立新專案。Or, choose the folder icon to create a new entry.

  6. 在編輯控制項中,指定 MathLibrary.h 標頭檔的位置路徑。In the edit control, specify the path to the location of the MathLibrary.h header file. 您可以選擇省略號 (...) 控制項流覽至正確的資料夾。You can choose the ellipsis (...) control to browse to the correct folder.

    您也可以輸入從用戶端來源檔案到包含 DLL 標頭檔的資料夾的相對路徑。You can also enter a relative path from your client source files to the folder that contains the DLL header files. 如果您遵循指示將用戶端專案放在 DLL 的不同方案中,相對路徑看起來應該像這樣:If you followed the directions to put your client project in a separate solution from the DLL, the relative path should look like this:

    ..\..\MathLibrary\MathLibrary

    如果您的 DLL 和用戶端專案位於相同的方案中,相對路徑可能如下所示:If your DLL and client projects are in the same solution, the relative path might look like this:

    ..\MathLibrary

    當 DLL 和用戶端專案位於其他資料夾時,請調整相對路徑以符合。When the DLL and client projects are in other folders, adjust the relative path to match. 或者,使用省略號控制項來流覽資料夾。Or, use the ellipsis control to browse for the folder.

    將標頭位置新增至其他 Include 目錄屬性Add the header location to the Additional Include Directories property

  7. 在 [ 其他 Include 目錄 ] 對話方塊中輸入標頭檔的路徑之後,請選擇 [ 確定] 按鈕。After you've entered the path to the header file in the Additional Include Directories dialog box, choose the OK button. 在 [ 屬性頁 ] 對話方塊中,選擇 [ 確定] 按鈕以儲存您的變更。In the Property Pages dialog box, choose the OK button to save your changes.

您現在可以包含 MathLibrary.h 檔案,並使用其在用戶端應用程式中宣告的函式。You can now include the MathLibrary.h file and use the functions it declares in your client application. 使用此程式碼取代 MathClient.cpp 的內容:Replace the contents of MathClient.cpp by using this code:

// MathClient.cpp : Client app for MathLibrary DLL.
// #include "pch.h" Uncomment for Visual Studio 2017 and earlier
#include <iostream>
#include "MathLibrary.h"

int main()
{
    // Initialize a Fibonacci relation sequence.
    fibonacci_init(1, 1);
    // Write out the sequence values until overflow.
    do {
        std::cout << fibonacci_index() << ": "
            << fibonacci_current() << std::endl;
    } while (fibonacci_next());
    // Report count of values written before overflow.
    std::cout << fibonacci_index() + 1 <<
        " Fibonacci sequence values fit in an " <<
        "unsigned 64-bit integer." << std::endl;
}

您可以編譯此程式碼,但不會連結。This code can be compiled, but not linked. 如果您現在建立用戶端應用程式,則錯誤清單會顯示數個 LNK2019 錯誤。If you build the client app now, the error list shows several LNK2019 errors. 這是因為您的專案遺漏了部分資訊:您尚未指定專案相依于 MathLibrary .lib 程式庫。That's because your project is missing some information: You haven't specified that your project has a dependency on the MathLibrary.lib library yet. 而且,您尚未告訴連結器如何尋找 MathLibrary .lib 檔案。And, you haven't told the linker how to find the MathLibrary.lib file.

若要修正此問題,您可以將程式庫檔案直接複製到您的用戶端應用程式專案。To fix this issue, you could copy the library file directly into your client app project. 連結器會自動尋找並使用它。The linker would find and use it automatically. 但是,如果程式庫和用戶端應用程式都在開發中,可能會導致某個複本中的變更不會顯示在另一個複本中。However, if both the library and the client app are under development, that might lead to changes in one copy that aren't shown in the other. 若要避免這個問題,您可以設定 [其他相依性 ] 屬性,告知組建系統您的專案相依于 MathLibrary .libTo avoid this issue, you can set the Additional Dependencies property to tell the build system that your project depends on MathLibrary.lib. 此外,您可以在專案中設定額外的連結 庫目錄 路徑,以在連結時包含原始程式庫的路徑。And, you can set an Additional Library Directories path in your project to include the path to the original library when you link.

將 DLL 匯入程式庫新增至您的專案To add the DLL import library to your project

  1. 方案總管 中的 [ >mathclient.cpp ] 節點上按一下滑鼠右鍵,然後選擇 [屬性] 以開啟 [屬性頁] 對話方塊。Right-click on the MathClient node in Solution Explorer and choose Properties to open the Property Pages dialog.

  2. 在 [設定] 下拉式清單方塊中,選取 [所有 設定] (如果尚未選取)。In the Configuration drop-down box, select All Configurations if it's not already selected. 它可確保任何屬性變更都適用于 Debug 和 Release 組建。It ensures that any property changes apply to both Debug and Release builds.

  3. 在左窗格中,選取 [設定 屬性 > 連結器 > 輸入]。In the left pane, select Configuration Properties > Linker > Input. 在屬性窗格中,選取 [其他相依性] 編輯方塊旁的下拉式清單控制項,然後選擇 [編輯]。In the property pane, select the drop-down control next to the Additional Dependencies edit box, and then choose Edit.

    編輯 [其他相依性] 屬性Edit the Additional Dependencies property

  4. 在 [ 其他 相依性] 對話方塊中,將 [ MathLibrary ] 新增至頂端編輯控制項中的清單。In the Additional Dependencies dialog, add MathLibrary.lib to the list in the top edit control.

    新增程式庫相依性Add the library dependency

  5. 選擇 [確定] 返回 [屬性頁] 對話方塊。Choose OK to go back to the Property Pages dialog box.

  6. 在左窗格中,選取 [設定 屬性 > 連結器 > 一般]。In the left pane, select Configuration Properties > Linker > General. 在屬性窗格中,選取 [其他程式庫目錄] 編輯方塊旁的下拉式清單控制項,然後選擇 [編輯]。In the property pane, select the drop-down control next to the Additional Library Directories edit box, and then choose Edit.

    編輯 [其他程式庫目錄] 屬性Edit the Additional Library Directories property

  7. 在 [其他程式庫目錄] 對話方塊的上方窗格中按兩下以啟用編輯控制項。Double-click in the top pane of the Additional Library Directories dialog box to enable an edit control. 在編輯控制項中,指定 MathLibrary.lib 檔案的位置路徑。In the edit control, specify the path to the location of the MathLibrary.lib file. 根據預設,它位於 DLL 方案資料夾底下的 [ Debug ] 資料夾中。By default, it's in a folder called Debug directly under the DLL solution folder. 如果您建立發行組建,檔案會放在名為 release 的資料夾中。If you create a release build, the file is placed in a folder called Release. 您可以使用 $(IntDir) 宏,讓連結器可以找到您的 DLL,無論您建立的組建種類為何。You can use the $(IntDir) macro so that the linker can find your DLL, no matter which kind of build you create. 如果您遵循指示將用戶端專案放在 DLL 專案的不同方案中,相對路徑看起來應該像這樣:If you followed the directions to put your client project in a separate solution from the DLL project, the relative path should look like this:

    ..\..\MathLibrary\$(IntDir)

    如果您的 DLL 和用戶端專案位於其他位置,請調整相對路徑以符合。If your DLL and client projects are in other locations, adjust the relative path to match.

    新增程式庫目錄Add the library directory

  8. 一旦您在 [其他程式庫目錄] 對話方塊中輸入程式庫檔案的路徑,請選擇 [確定] 按鈕返回 [屬性頁] 對話方塊。Once you've entered the path to the library file in the Additional Library Directories dialog box, choose the OK button to go back to the Property Pages dialog box. 選擇 [確定] 以儲存屬性變更。Choose OK to save the property changes.

您的用戶端應用程式現在可以編譯並成功連結,但仍不具備執行所需的所有項目。Your client app can now compile and link successfully, but it still doesn't have everything it needs to run. 當作業系統載入您的應用程式時,會尋找 MathLibrary DLL。When the operating system loads your app, it looks for the MathLibrary DLL. 如果作業系統無法在特定系統目錄、環境路徑或本機應用程式目錄中找到該 DLL,則載入將會失敗。If it can't find the DLL in certain system directories, the environment path, or the local app directory, the load fails. 視作業系統而定,您會看到類似下面的錯誤訊息:Depending on the operating system, you'll see an error message like this:

找不到 MathLibrary DLL 錯誤MathLibrary DLL not found error

避免此問題之其中一個方法是將 DLL 複製到包含用戶端可執行檔的目錄,作為建置程序的一部分。One way to avoid this issue is to copy the DLL to the directory that contains your client executable as part of the build process. 您可以將 建立後事件 新增至您的專案,以加入將 DLL 複製到組建輸出目錄的命令。You can add a Post-Build Event to your project, to add a command that copies the DLL to your build output directory. 此處指定的命令只會在遺失或變更時複製 DLL。The command specified here copies the DLL only if it's missing or has changed. 它會根據您的組建設定,使用宏來複製到偵錯工具或發行位置。It uses macros to copy to and from the Debug or Release locations, based on your build configuration.

在建置後事件中複製 DLLTo copy the DLL in a post-build event

  1. 方案總管 中的 [ >mathclient.cpp ] 節點上按一下滑鼠右鍵,然後選擇 [屬性] 以開啟 [屬性頁] 對話方塊。Right-click on the MathClient node in Solution Explorer and choose Properties to open the Property Pages dialog.

  2. 在 [設定] 下拉式清單方塊中,選取 [所有 設定] (如果尚未選取)。In the Configuration drop-down box, select All Configurations if it isn't already selected.

  3. 在左窗格中,選取 [設定 屬性 > 組建事件建立 > 後事件]。In the left pane, select Configuration Properties > Build Events > Post-Build Event.

  4. 在 [屬性] 窗格中,選取 [ 命令列 ] 欄位中的編輯控制項。In the property pane, select the edit control in the Command Line field. 如果您遵循指示將用戶端專案放在 DLL 專案的不同方案中,請輸入下列命令:If you followed the directions to put your client project in a separate solution from the DLL project, then enter this command:

    xcopy /y /d "..\..\MathLibrary\$(IntDir)MathLibrary.dll" "$(OutDir)"

    如果您的 DLL 和用戶端專案是在其他目錄中,請將對應的相對路徑變更為相符的 DLL。If your DLL and client projects are in other directories, change the relative path to the DLL to match.

    新增建立後命令Add the post-build command

  5. 選擇 [確定] 按鈕,儲存您對專案屬性進行的變更。Choose the OK button to save your changes to the project properties.

現在您的用戶端應用程式已具備建置與執行所需所有項目。Now your client app has everything it needs to build and run. 在功能表列上選擇 [組建 > 組建方案],以建立應用程式。Build the application by choosing Build > Build Solution on the menu bar. 根據您的 Visual Studio 版本,Visual Studio 中的 [ 輸出 ] 視窗應該會有類似下列的範例:The Output window in Visual Studio should have something like the following example depending on your version of Visual Studio:

1>------ Build started: Project: MathClient, Configuration: Debug Win32 ------
1>MathClient.cpp
1>MathClient.vcxproj -> C:\Users\username\Source\Repos\MathClient\Debug\MathClient.exe
1>1 File(s) copied
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

恭喜,您已建立可在 DLL 中呼叫函式的應用程式。Congratulations, you've created an application that calls functions in your DLL. 現在,執行您的應用程式以查看其功能。Now run your application to see what it does. 在功能表列上,選擇 [ Debug > 啟動但不進行調試]。On the menu bar, choose Debug > Start Without Debugging. Visual Studio 會開啟命令視窗,供程式執行。Visual Studio opens a command window for the program to run in. 輸出的最後一部分看起來應該如下所示:The last part of the output should look like:

啟動用戶端應用程式而不進行偵錯工具Start the client app without debugging

按任意鍵以關閉命令視窗。Press any key to dismiss the command window.

您已建立 DLL 和用戶端應用程式,現在您可以進行實驗。Now that you've created a DLL and a client application, you can experiment. 請嘗試在用戶端應用程式的程式碼中設定中斷點,並在偵錯工具中執行應用程式。Try setting breakpoints in the code of the client app, and run the app in the debugger. 查看當您逐步執行程式庫呼叫時會發生什麼事。See what happens when you step into a library call. 將其他函式新增至程式庫,或撰寫可使用您 DLL 的另一個用戶端應用程式。Add other functions to the library, or write another client app that uses your DLL.

部署您的應用程式時,您也必須部署其使用的 DLL。When you deploy your app, you must also deploy the DLLs it uses. 若要讓您建立的 Dll 或從協力廠商包含的 Dll,最簡單的方式就是將它們放在與應用程式相同的目錄中。The simplest way to make the DLLs that you build, or that you include from third parties, available is to put them in the same directory as your app. 這就是所謂的 應用程式本機部署It's known as app-local deployment. 如需部署的詳細資訊,請參閱 Deployment in Visual C++For more information about deployment, see Deployment in Visual C++.

另請參閱See also

從 Visual Basic 應用程式呼叫 DLL 函式Calling DLL Functions from Visual Basic Applications