在 C# 中載入自己編寫的動態連結程式庫

來源:互聯網
上載者:User
一、發生的背景
    在開發新項目中使用了新的語言開發 C# 和新的技術方案 WEB Service,但是在新項目中,一些舊的模組需要繼續使用,一般是採用 C 或 C++ 或 Delphi 編寫的,如何利用舊模組對於開發人員來說,有三種可用方法供選擇:第一、將 C 或 C++ 函數用 C# 徹底改寫一遍,這樣整個項目代碼比較統一,維護也方便一些。但是儘管微軟以及某些書籍說,C# 和 C++ 如何接近,但是改寫起來還是很痛苦的事情,特別是 C++ 裡的指標和記憶體操作;第二、將 C 或 C++ 函數封裝成 COM,在 C# 中調用COM 比較方便,只是在封裝時需要處理 C 或 C++ 類型和 COM 類別型之間的轉換,也有一些麻煩,另外COM 還需要註冊,註冊次數多了又可能導致混亂;第三、將 C 或 C++ 函數封裝成動態連結程式庫,封裝的過程簡單,工作量不大。因此我決定採用載入動態連結程式庫的方法實現,於是產生了在 C# 中如何調用自訂的動態連結程式庫問題,我在網上搜尋相關主題,發現一篇調用系統 API 的文章,但是沒有說明如何解決此問題,在 MSDN 上也沒有相關詳細說明。基於此,我決定自己從簡單出發,逐步實驗,看看能否達到自己的目標。
    (說明一點:我這裡改寫為什麼很怕麻煩,我改寫的代碼是變長密碼編譯演算法函數,代碼有600多行,對演算法本身不熟悉,演算法中指標和記憶體操作太多,要想保證演算法正確,最可行的方法就是少動代碼,否則只要有一點點差錯,就不能肯定演算法與以前相容)
  
  二、技術實現
    下面看看如何逐步實現動態庫的載入,類型的匹配,動態連結程式庫函數匯出的定義,這個不需要多說,大家參考下面宏定義即可:
  
  #define LIBEXPORT_API extern "C" __declspec(dllexport)
  第一步,我先從簡單的調用出發,定義了一個簡單的函數,該函數僅僅實現一個整數加法求和:
  
  LIBEXPORT_API int mySum(int a,int b){ return a+b;}
  C# 匯入定義:
  
  public class RefComm
  {
  [DllImport("LibEncrypt.dll",
      EntryPoint=" mySum ",
      CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
      public static extern int mySum (int a,int b);
  }
  在C#中調用測試:
  
  int iSum = RefComm.mySum(2,3);
  運行查看結果iSum為5,調用正確。第一步實驗完成,說明在C#中能夠調用自訂的動態連結程式庫函數。
  
  第二步,我定義了字串操作的函數(簡單起見,還是採用前面的函數名),返回結果為字串:
  
  LIBEXPORT_API char *mySum(char *a,char *b){sprintf(b,"%s",a); return a;}
  C# 匯入定義:
  
  public class RefComm
  {
  [DllImport("LibEncrypt.dll",
       EntryPoint=" mySum ",
       CharSet=CharSet.Auto,
       CallingConvention=CallingConvention.StdCall)]
       public static extern string mySum (string a, string b);
  }
  在C#中調用測試:
  
  string strDest="";
  string strTmp= RefComm.mySum("12345", strDest);
  運行查看結果 strTmp 為"12345",但是strDest為空白。我修改動態連結程式庫實現,返回結果為串b:
  
  LIBEXPORT_API char *mySum(char *a,char *b){sprintf(b,"%s",a) return b;}
  修改 C# 匯入定義,將串b修改為ref方式:
  
  public class RefComm
  {
  [DllImport("LibEncrypt.dll",
       EntryPoint=" mySum ",
       CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
       public static extern string mySum (string a, ref string b);
  }
  在C#中再調用測試:
  
  string strDest="";
  string strTmp= RefComm.mySum("12345", ref strDest);
    運行查看結果 strTmp 和 strDest 均不對,含不可見字元。再修改 C# 匯入定義,將CharSet從Auto修改為Ansi:
  
  public class RefComm
  {
  [DllImport("LibEncrypt.dll",
       EntryPoint=" mySum ",
       CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]
       public static extern string mySum (string a, string b);
  }
  在C#中再調用測試:
  
  string strDest="";
  string strTmp= RefComm. mySum("12345", ref strDest);
    運行查看結果 strTmp 為"12345",但是串 strDest 沒有賦值。第二步實現函數返回串,但是在函數出口參數中沒能進行輸出。再次修改 C# 匯入定義,將串b修改為引用(ref):
  
  public class RefComm
  {
  [DllImport("LibEncrypt.dll",
       EntryPoint=" mySum ",
       CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]
       public static extern string mySum (string a, ref string b);
  }
  運行時調用失敗,不能繼續執行。
  
  第三步,修改動態連結程式庫實現,將b修改為雙重指標:
  
  LIBEXPORT_API char *mySum(char *a,char **b){sprintf((*b),"%s",a); return *b;}
  C#匯入定義:
  
  public class RefComm
  {
  [DllImport("LibEncrypt.dll",
       EntryPoint=" mySum ",
       CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]
       public static extern string mySum (string a, ref string b);
  }
  在C#中調用測試:
  
  string strDest="";
  string strTmp= RefComm. mySum("12345", ref strDest);
    運行查看結果 strTmp 和 strDest 均為"12345",調用正確。第三步實現了函數出口參數正確輸出結果。
  
  第四步,修改動態連結程式庫實現,實現整數參數的輸出:
  
  LIBEXPORT_API int mySum(int a,int b,int *c){ *c=a+b; return *c;}
  C#匯入的定義:
  
  public class RefComm
  {
  [DllImport("LibEncrypt.dll",
       EntryPoint=" mySum ",
       CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]
       public static extern int mySum (int a, int b,ref int c);
  }
  在C#中調用測試:
  
  int c=0;
  int iSum= RefComm. mySum(2,3, ref c);
  運行查看結果iSum 和c均為5,調用正確。
    經過以上幾個步驟的實驗,基本掌握了如何定義動態庫函數以及如何在 C# 定義匯入,有此基礎,很快我實現了變長加密函數在 C# 中的調用,至此目標實現。
  
  三、結論
    在 C# 中調用 C++ 編寫的動態連結程式庫函數,如果需要出口參數輸出,則需要使用指標,對於字串,則需要使用雙重指標,對於 C# 的匯入定義,則需要使用引用(ref)定義。
    對於函數傳回值,C# 匯入定義和 C++ 動態庫函式宣告定義需要保持一致,否則會出現函數調用失敗。定義匯入時,一定注意 CharSet 和 CallingConvention 參數,否則導致調用失敗或結果異常。運行時,動態連結程式庫放在 C# 程式的目錄下即可,我這裡是一個 C# 的動態連結程式庫,兩個動態連結程式庫就在同一個目錄下運行。
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.