1
votes

I am pretty new to Delphi. We tried to import a C++ or C# dll to Delphi. Our requirement is to import the library as COM object. Since there are multiple classes and structs we have in the DLL , we don’t want it to P/Invoke the methods. We tried to build a DLL in C++/C# and as MFC ActiveX(ocx component). I have been following these links to create a DLL :

and this link(http://wiki.freepascal.org/LazActiveX) to import the TLIB I created. All the DLLs I create comes to stopping point where I create an object in Delphi.

The one I created in C# is :

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.

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: enter image description here Method 2 gives: enter image description here

We made sure that the library is registered and found the GUID in Wow6432Node>CLSID> enter image description here

I hope this is the correct way to create an object. May I know what I am missing in here?

1
The error message is clear enough isn't it?David Heffernan
@DavidHeffernan Thanks for the comment. But I think am not clear about the error, I see the class as registered.ACB
The system doesn't though does it. Why not.David Heffernan
BTW, you don't seem to be initialising com anywhereDavid Heffernan
@geisterfurz007. Thanks! I will make sure next timeACB

1 Answers

0
votes

-(VS) Project Options>Application>Assembly Information>Make assembly COM-visible checked?

-(VS) Project Options>Build>Register for COM Interop checked?

-Register with RegAsm.exe?