Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: Importing a .Net DLL into a Delphi 10 DLL project


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 5 - Last Post: Aug 22, 2017 6:10 AM Last Post By: Bismark Appah
Fred Smith

Posts: 81
Registered: 12/4/15
Importing a .Net DLL into a Delphi 10 DLL project  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 17, 2015 5:51 AM
Hi all,
I have a DLL written in C# (.NET 4) and I'm trying to integrate it in my Delphi project.
I read on forums that COM Interop is the best way to do it.
How do I import the .NET DLL into my Delphi project ?
I'm using Delphi 10.
I go to "Component", then "Import Component", then "Import .Net Assembly", as my assembly is not showing I click on "Add" and add my .NET DLL but I still cannot see my DLL ?
What am I missing ?
Thanks.

Info:
DLL is written in C# (.NET 4)
I'm using Delphi - RAD Studio 10 Seattle

Remy Lebeau (Te...


Posts: 9,442
Registered: 12/23/01
Re: Importing a .Net DLL into a Delphi 10 DLL project  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 17, 2015 9:28 AM   in response to: Fred Smith in response to: Fred Smith
Fred wrote:

I read on forums that COM Interop is the best way to do it.

There are other solutions available that do not involve COM Interop. Wrapper
frameworks such as AToZed CrossTalk (http://www.atozed.com/CrossTalk/) can
load and call into .NET DLLs directly. And there are VisualStudio plugins
such as UnmanagedExports (https://www.nuget.org/packages/UnmanagedExports)
that allow unmanaged functions to be exported from a .NET DLL.

I go to "Component", then "Import Component", then "Import .Net
Assembly", as my assembly is not showing I click on "Add" and add
my .NET DLL but I still cannot see my DLL ?

Does your DLL export its .NET classes as COM objects (has them marked as
COMVisible=true)?

--
Remy Lebeau (TeamB)
Fred Smith

Posts: 81
Registered: 12/4/15
Re: Importing a .Net DLL into a Delphi 10 DLL project  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 18, 2015 7:55 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thanks very much for your reply.
I'll check the links you've included.

ComVisible is set to false in the DLL I'm attempting to integrate in my project:
[assembly: ComVisible(false)]
Thanks


Remy Lebeau (TeamB) wrote:
Fred wrote:

I read on forums that COM Interop is the best way to do it.

There are other solutions available that do not involve COM Interop. Wrapper
frameworks such as AToZed CrossTalk (http://www.atozed.com/CrossTalk/) can
load and call into .NET DLLs directly. And there are VisualStudio plugins
such as UnmanagedExports (https://www.nuget.org/packages/UnmanagedExports)
that allow unmanaged functions to be exported from a .NET DLL.

I go to "Component", then "Import Component", then "Import .Net
Assembly", as my assembly is not showing I click on "Add" and add
my .NET DLL but I still cannot see my DLL ?

Does your DLL export its .NET classes as COM objects (has them marked as
COMVisible=true)?

--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,442
Registered: 12/23/01
Re: Importing a .Net DLL into a Delphi 10 DLL project  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 18, 2015 12:45 PM   in response to: Fred Smith in response to: Fred Smith
Fred wrote:

ComVisible is set to false in the DLL I'm attempting to integrate
in my project

Then you can't use COM Interop, unless the individual classes are marked
as [ComVisible(true)]. Otherwise, you will have to use a different solution.

--
Remy Lebeau (TeamB)
ted nguyen

Posts: 1
Registered: 10/16/99
Re: Importing a .Net DLL into a Delphi 10 DLL project  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 13, 2016 11:53 AM   in response to: Fred Smith in response to: Fred Smith
Fred Smith wrote:
Hi all,
I have a DLL written in C# (.NET 4) and I'm trying to integrate it in my Delphi project.
I read on forums that COM Interop is the best way to do it.
How do I import the .NET DLL into my Delphi project ?
I'm using Delphi 10.
I go to "Component", then "Import Component", then "Import .Net Assembly", as my assembly is not showing I click on "Add" and add my .NET DLL but I still cannot see my DLL ?
What am I missing ?
Thanks.

Info:
DLL is written in C# (.NET 4)
I'm using Delphi - RAD Studio 10 Seattle

Hi LeBeau,

Even I have check on the box Make assembly COM-visible when I build the .NET Assembly Delphi still not load. I remember the last time this worked is Delphi 7. Since, I upgrade to Delphi 2007, 2009... XE... XE8... all of the version later never work.

I posted the issue while back... years ago... it seems no one of Delphi team in Borland then Embarcadero really cares... I just give it up!!! I am glad that Fred bring this up.

Sorry, it's not a really good comment... but I have to say it!!!

Ted.

Bismark Appah

Posts: 26
Registered: 3/13/15
Re: Importing a .Net DLL into a Delphi 10 DLL project  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 12, 2017 2:27 AM   in response to: Fred Smith in response to: Fred Smith
You can use the .Net Runtime Library for Delphi which is the best library to work with the .Net Framework. It is designed to provide a way to interact with applications written in anyone of the .Net languages from Delphi.

The library contains classes and interfaces which host the CLR and allow a user to:

- Create .Net objects from assemblies not registered for COM and loading .Net assemblies from partial or full assembly name, location and streams.
- Create and use .Net objects that are not COM visible.
- Create .Net objects using parameterized or non-parameterized constructors.
- Call Static and non-Static properties/methods/fields of .Net object.
- Handle .Net exceptions and get all .Net exception properties including exception type name, StackTrace etc.
- Get information about loaded instance of .Net framework such as memory usage and size of heap by generation.
- Work with .Net events.
- and many more.

You can load the .net DLL and access or invoke the members of the types in the loaded assembly. If you want to access the members of the class directly, you have to add DispId attributes to the members of the class. See example below.
You can also use the [.Net Assembly/WSDL Importer|http://www.crystalnet-tech.com/RuntimeLibrary/RuntimeLibUtilities] which is part of the .Net Runtime Library for Delphi to import the .Net Assembly DLL and automatically create delphi classes from the types in the DLL assembly.

C# Code
//Assuming I have a C# Class which looks like this:;
// =================================================
 
//Compiled To: ClassLibraryTest.dll
 
namespace ClassLibraryTest
{
    public class Class1
    {
        [DispId(0)]
        public int Add(int a, int b)
        {
            return a + b;
        }
        [DispId(1)]
        public string GetID(string ID)
        {
            return ID;
        }
    }
}


Delphi Code:
The Delphi equivalent of the above C# code with dispatch IDs is shown below:
program ClassLibraryTest;
 
{$APPTYPE CONSOLE}
 
{$R *.res}
 
uses
  System.SysUtils,
  CNClrLib.Host;
 
type
  _Class1 = dispinterface
  ['{10A4465E-8D4A-4AA4-B113-D1B4B37F3658}']
    function Add(a, b : Integer) : Integer; dispid 0;
    function GetID(InString: WideString): WideString; dispid 1;
  end;
 
var
  oClass : _Class1;
begin
  try
    begin
      // Assuming ClassLibraryTest.dll is in the same location as the EXE.
      TClrAssembly.LoadFrom('ClassLibraryTest.dll');
      oClass := _Class1(TClrDispatchActivator.CreateInstance('ClassLibraryTest.Class1'));
 
      WriteLn('Add Method: ', oClass.Add(10, 20));
      WriteLn('GetID Method: ', oClass.GetID('12345'));
    end;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.
 
//  Output
//    Add Method: 30
//    GetID Method: 12345
            


Note: You don't need to register the .Net dll, the runtime library will host the CLR and allow you to load the .net dll as assembly, get the types of the loaded assembly, create an instance of the type and invoke the members of the type.

For more information, go to http://www.crystalnet-tech.com/RuntimeLibrary/RuntimeLibrary4Delphi.aspx

Edited by: Bismark Appah on Aug 22, 2017 2:07 PM
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02