建立和使用動態連結程式庫 (C++)C#調用

來源:互聯網
上載者:User
  1. 我們將建立的第一種類型的庫是動態連結程式庫 (DLL)。使用 DLL 是一種重用代碼的絕佳方式。您不必在自己建立的每個程式中重新實現同一常式,而只需對這些常式編寫一次,然後從需要該功能的應用程式引用它們即可。

    本演練涵蓋以下內容:

    • 建立新的動態連結程式庫 (DLL) 項目。

    • 向動態連結程式庫添加類。

    • 建立引用動態連結程式庫的應用程式。

    • 在控制台應用程式中使用類庫的功能。

    • 運行應用程式。

    系統必備

    本主題假定您具備 C++ 語言的基礎知識。 如果您是剛開始學習 C++,建議您參閱 Herb Schildt 編寫的 C++ Beginner's Guide(《C++ 初學者指南》),該書可從 http://go.microsoft.com/fwlink/?LinkId=115303 線上獲得。

    建立新的動態連結程式庫 (DLL) 項目
    1. “檔案”菜單中,選擇“建立”,然後選擇“項目…”

    2. “項目類型”窗格中,選擇“Visual C++”下的“Win32”

    3. “模板”窗格中,選擇“Win32 控制台應用程式”

    4. 為項目選擇一個名稱,如 MathFuncsDll,並將其鍵入“名稱”欄位。為解決方案選擇一個名稱,如 DynamicLibrary,並將其鍵入“解決方案名稱”欄位。

    5. 單擊“確定”啟動 Win32 應用程式嚮導。在“Win32 應用程式嚮導”對話方塊的“概述”頁中,單擊“下一步”

    6. “Win32 應用程式嚮導”中的“應用程式設定”頁中,選擇“應用程式類型”下的“DLL”(如果可用),或者選擇“控制台應用程式”(如果“DLL”不可用)。某���版本的 Visual Studio 不支援通過使用嚮導建立 DLL 項目。您可以稍後對此變更,以將項目編譯為 DLL。

    7. “Win32 應用程式嚮導”“應用程式設定”頁中,選擇“附加選項”下的“空項目”

    8. 單擊“完成”建立項目。

    向動態連結程式庫添加類
    1. 若要為新類建立標頭檔,請從“項目”菜單中選擇“添加新項…”。將顯示“添加新項”對話方塊。在“類別”窗格中,選擇“Visual C++”下的“代碼”。在“模板”窗格中選擇“標頭檔(.h)”。為標頭檔選擇一個名稱,如 MathFuncsDll.h,並單擊“添加”。將顯示一個空白檔案。

    2. 添加一個名為“MyMathFuncs”的簡單類,以執行常見的算術運算,如加、減、乘和除。代碼應與以下內容類別似:

      #ifdef MathFuncsDll_EXPORTS
      #define MathFuncsDll_API __declspec(dllexport)
      #else
      #define MathFuncsDll_API __declspec(dllimport)
      #endif // Windows 標頭檔:
      #include <windows.h>       
    3.   // Returns a + b
              extern "C" MathFuncsDll_API double Add(double a, double b);      
    4.    // Returns a - b
              extern "C" MathFuncsDll_API double Subtract(double a, double b);    
    5.     // Returns a * b
              extern "C" MathFuncsDll_API double Multiply(double a, double b);    
    6.      // Returns a / b
           // Throws DivideByZeroException if b is 0
              extern "C" MathFuncsDll_API double Divide(double a, double b);
    7. 請注意此代碼方法聲明中的 __declspec(dllexport) 修飾符。這些修飾符使 DLL 能夠匯出該方法以供其他應用程式使用。有關更多資訊,請參見 dllexport, dllimport。

    8. 若要為新類建立源檔案,請從“項目”菜單中選擇“添加新項…”。將顯示“添加新項”對話方塊。在“類別”窗格中,選擇“Visual C++”下的“代碼”。在“模板”窗格中,選擇“C++ 檔案(.cpp)”。為源檔案選擇一個名稱,如 MathFuncsDll.cpp,並單擊“添加”。將顯示一個空白檔案。

    9. 在源檔案中實現“MyMathFuncs”的功能。代碼應與以下內容類別似:

      #include "MathFuncsDll.h"
    10. #include <stdexcept>
    11. using namespace std;

    12. BOOL APIENTRY DllMain( HMODULE hModule,
                            DWORD  ul_reason_for_call,
                            LPVOID lpReserved
      )
      {
          return TRUE;
      }
    13.  extern "C"   double Add(double a, double b)
          {
              return a + b;
          }
    14.     extern "C"   double Subtract(double a, double b)
          {
              return a - b;
          }
    15.     extern "C"   double Multiply(double a, double b)
          {
              return a * b;
          }
    16.    extern "C"    double Divide(double a, double b)
          {
              if (b == 0)
              {
                  throw new invalid_argument("b cannot be zero!");
              }
              return a / b;
          }
    17. 建立C#控制台應用程式
    18. 建立一個APIHelper類,專門管理DllImport,添加using System.Runtime.InteropServices;
    19.     public class APIHelper
          {
    20.         [DllImport("MathFuncsDll.dll", EntryPoint = "Divide", CallingConvention = CallingConvention.StdCall)]
              public static extern double Divide(double a, double b);
    21.         [DllImport("MathFuncsDll.dll")]
              public static extern double Add(double x, double y);
          }

    1. 在Program中調用,將Debug檔案夾下的所有檔案copy到Bin/Debug下
    2. Console.WriteLine(APIHelper.Divide(2, 3));
                  Console.WriteLine(APIHelper.Add(2, 3));
                  Console.ReadLine();
      :http://download.csdn.net/source/2980458

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.