Delphi7程式調用C#寫的DLL解決辦法(轉)

來源:互聯網
上載者:User

標籤:

近來,因工作需要,必須解決Delphi7寫的主程式調用C#寫的dll的問題。在網上一番搜尋,又經過種種實驗,最終證明有以下兩種方法可行:
    編寫C#dll的方法都一樣,首先在vs2005中建立一個“類庫”項目TestDll,
using System.Runtime.InteropServices;
namespace TestDll
{
public interface  I TestClass
  {
     void YourProcedure(stirng param1);
}
[ClassInterface(ClassInterfaceType.None)]
public class TestClass:I TestClass
{
public void YourProcedure (stirng param1);
{ //自己的代碼    }
    }
}
完成之後,設定項目的屬性“Make assembly COM-Visible”為選中狀態。編譯之後得到 TestClass.dll,把此dll放到Delphi主程式目錄下。開啟vs2005內建的工具“Visual Studio 2005命令提示”,輸入
Regasm  路徑/TestClass.dll 向系統註冊此dll。
Delphi程式調用此Dll方式有兩種:
一、開啟vs2005內建的工具“Visual Studio 2005命令提示”,輸入 TlbExp  路徑/TestClass.dll 得到一個TestClass.tlb 檔案。開啟Delphi,選擇“Project”--“import type library”找到剛才的TestClass.tlb,點擊 CreateUnit,向delphi中引入一個com介面。
delphi 調用代碼如下:
var aClass: TestClass;
begin
  aClass : =  CoTestClass.Create;
  aClass. YourProcedure (‘參數‘);
end;
二、不需產生tlb檔案,仿照調用Excel的方式。代碼如下:
var aClass: Variant;
begin
  aClass:= CreateOleObject(‘TestDll.TestClass‘);
  aClass.YourProcedure (‘參數‘);
end;
以上兩種方法都可以調用成功,其中調用regasm.exe向系統註冊dll是必需的。第一種方法需要產生tlb檔案,並引入delphi中,操作繁瑣,但可以看到介面的定義。第二種方法操作簡單,但看不到介面的定義。

==============================================================

本人用第二種方法已正常實現功能,但DLL中一些自動建立的方法無法在外部直接調用,需要DLL準備特定的介面函數

Delphi7程式調用C#寫的DLL解決辦法(轉)

聯繫我們

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