在Delphi中将C#或C ++ DLL导入为COM对象

时间:2016-11-25 06:32:34

标签: c# c++ delphi dll com

我对Delphi很新。我们尝试将C ++或C#dll导入Delphi。我们的要求是将库导入为COM对象。由于DLL中有多个类和结构,我们不希望它P / Invoke方法。 我们尝试用C ++ / C#和MFC ActiveX(ocx组件)构建DLL。  我一直在关注这些链接来创建DLL:

和此链接(http://wiki.freepascal.org/LazActiveX)导入我创建的TLIB。 我创建的所有DLL都停止在Delphi中创建对象的停止点。

我在C#中创建的那个是:

using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace CSharpSampleLib
{
    [ComVisible(true),
    Guid("CDBFD892-7473-4AC4-8B44-D75A828599AD")]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface ICSharpCom
    {
        [DispId(1)]
        void Init();
        [DispId(2)]
        int AddNumbers(int num1, int num2);
        [DispId(3)]
        String StringConcat(String num1, String num2);

    }



   [Guid("D51F086B-B593-436D-8900-92CDC1E427CE"),
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface CSharpCom_Events
    {
    }
    [Guid("4AB74C5C-6F3C-4B15-9E00-5174551B50A2"),
    ClassInterface(ClassInterfaceType.None)]
    [ProgId("Sample.CSharpCom")]
    [ComVisible(true)]
    public class CSharpCom : ICSharpCom
    {
        public CSharpCom() { }
        public void Init() { }

        public int AddNumbers(int num1, int num2) 
        {
            return num1 + num2;
        }
        public String StringConcat(String str1, String str2) 
        {
            return str1 + str2;
        }
    }
}

我们尝试了两个选项来调用我们导入的Type库。

program CSharpDemo;

uses comobj, CSharpSampleLib_1_0_TLB;
var
  objCsLib1:ICSharpCom;
  objCsLib2:OLEVariant;

begin
  objCsLib1:=CoCSharpCom.Create; // Method 1
objCsLib2:= CreateOleObject('CSharpSampleLib.CSharpCom'); //Method 2
end.

方法1给出: enter image description here 方法2给出: enter image description here

我们确保该库已注册,并在Wow6432Node> CLSID>中找到了GUID。 enter image description here

我希望这是创建对象的正确方法。我可以知道我在这里缺少什么吗?

1 个答案:

答案 0 :(得分:0)

- (VS)项目选项>应用程序>装配信息>使装配COM可见检查?

- (VS)项目选项>构建>注册COM Interop?

- 使用RegAsm.exe注册?