简体   繁体   English

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

[英]Importing C# or C++ DLL as COM object in Delphi

I am pretty new to Delphi. 我对Delphi很新。 We tried to import a C++ or C# dll to Delphi. 我们尝试将C ++或C#dll导入Delphi。 Our requirement is to import the library as COM object. 我们的要求是将库导入为COM对象。 Since there are multiple classes and structs we have in the DLL , we don't want it to P/Invoke the methods. 由于DLL中有多个类和结构,我们不希望它P / Invoke方法。 We tried to build a DLL in C++/C# and as MFC ActiveX(ocx component). 我们尝试用C ++ / C#和MFC ActiveX(ocx组件)构建DLL。 I have been following these links to create a DLL : 我一直在关注这些链接来创建一个DLL:

and this link( http://wiki.freepascal.org/LazActiveX ) to import the TLIB I created. 和这个链接( http://wiki.freepascal.org/LazActiveX )导入我创建的TLIB。 All the DLLs I create comes to stopping point where I create an object in Delphi. 我创建的所有DLL都停止在Delphi中创建对象的停止点。

The one I created in C# is : 我在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;
        }
    }
}

We tried two options to invoke the Type library we imported. 我们尝试了两个选项来调用我们导入的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.

Method 1 gives: 方法1给出: 在此输入图像描述 Method 2 gives: 方法2给出: 在此输入图像描述

We made sure that the library is registered and found the GUID in Wow6432Node>CLSID> 我们确保已注册库,并在Wow6432Node> CLSID>中找到GUID 在此输入图像描述

I hope this is the correct way to create an object. 我希望这是创建对象的正确方法。 May I know what I am missing in here? 我可以知道我在这里缺少什么吗?

-(VS) Project Options>Application>Assembly Information>Make assembly COM-visible checked? - (VS)项目选项>应用程序>装配信息>检查装配COM是否可见?

-(VS) Project Options>Build>Register for COM Interop checked? - (VS)项目选项>构建>注册COM Interop已检查?

-Register with RegAsm.exe? - 注册RegAsm.exe?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM