Recent

Author Topic: .NET Libraries Loading from native application  (Read 772 times)

MaxM74

  • New Member
  • *
  • Posts: 24
.NET Libraries Loading from native application
« on: February 04, 2025, 01:26:19 pm »
is there a way to use a library written in .NET from an application written in lazarus/fpc?

I downloaded and looked at this Bridge library written in c++, so theoretically it is possible to do it from pascal too.

https://www.codeproject.com/Articles/42319/Using-NET-Classes-Modules-from-Native-Cplusplus

I didn't find anything written in fpc or delphi

Thaddy

  • Hero Member
  • *****
  • Posts: 16580
  • Kallstadt seems a good place to evict Trump to.
Re: .NET Libraries Loading from native application
« Reply #1 on: February 04, 2025, 01:32:22 pm »
It is possible, though not recommended in more than one way.
The easiest way is to treat the .net libraries as COM: they usually contain a type library.
This works on Windows, I never even tried to use that with mono on linux.
There are examples here on this forum that do it the hard way.
Also have a look at p/invoke and/or mkbundle that both creates libraries that freepascal can use.

(if I need C#, I tend to write everything in C#)
« Last Edit: February 04, 2025, 01:44:36 pm by Thaddy »
But I am sure they don't want the Trumps back...

d2010

  • Full Member
  • ***
  • Posts: 103
Re: .NET Libraries Loading from native application
« Reply #2 on: February 04, 2025, 01:45:43 pm »
Hello
I found two parallel routines
Code: [Select]
#include "StdAfx.h"
#include "ManagedProxy.h"
//Here is C++ inside ManagedProxy.cpp
using namespace System;
using namespace System::Runtime::InteropServices;
using namespace System::Reflection;


Code: [Select]
using System;
using System.Text;
using System.Diagnostics;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.IO;
Here is C#
        [CommandMethod("samp32")]
        public static void samp32()
        {
            _AcEd.Editor ed = _AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
            try
            {
                Bricscad.Windows.OpenFileDialog dlg =
                  new Bricscad.Windows.OpenFileDialog("Test", "Drawing", "dwg", "Bricscad File Nav",
                    _AcWnd.OpenFileDialog.OpenFileDialogFlags.AllowAnyExtension |
                    _AcWnd.OpenFileDialog.OpenFileDialogFlags.AllowMultiple);

                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    foreach (String file in dlg.GetFilenames())
                        ed.WriteMessage(file);
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage(ex.Message);
            }
        }

Solution01 ,  ;D
step00=you start any app.cpp.exe with NativeToManagedBridge.dll
step01=you extract all DLL.names. files from NativeToManagedBridge.dll
step02=you search all 'dll.names outoff C# from step01
you extract all 'DLL.names inside  C++ from step01
step03=You write sample MINICALL.LPI  OR MINICALL.DPR (e.g consoleapp)
step04=You load all 'DLL from step02 but inside C++
Cele mai frumoase rugăciuni ale Pr. Arsenie Boca. Multumesc.
Anyone can remaster source ..
Thank you.

« Last Edit: February 04, 2025, 01:51:18 pm by d2010 »

Thaddy

  • Hero Member
  • *****
  • Posts: 16580
  • Kallstadt seems a good place to evict Trump to.
Re: .NET Libraries Loading from native application
« Reply #3 on: February 04, 2025, 01:48:02 pm »
I found some good answers here later:
Ask this to DeepSeek.com ....:
"can you point me the directions to use libraries written in C#/Mono in Freepascal?"

Came up with pretty much the same as I wrote already... :P
But with working - well, slight mods needed - example code.
« Last Edit: February 04, 2025, 01:56:43 pm by Thaddy »
But I am sure they don't want the Trumps back...

gidesa

  • Full Member
  • ***
  • Posts: 160
Re: .NET Libraries Loading from native application
« Reply #4 on: February 04, 2025, 04:32:41 pm »
Hello,
a FPC program can use a C# DLL in these cases:
- as said from Thaddy, the C# DLL exports COM (activeX) classes, and you are on Windows  (it's possible
  also to use COM Dll not previously registered with regsvr32)
- the C# library exports C-style functions.

There is also a very old unit from Jedi project, called  JclDotNet.pas, to call directly Net functions
inside an assembly.
See here:
https://adamjohnston.me/delphi-dotnet-interop-with-jvcl/

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12050
  • FPC developer.
Re: .NET Libraries Loading from native application
« Reply #5 on: February 04, 2025, 04:49:30 pm »
Remobjects Hydra is a commercial product to integrate Delphi and .NET, but afaik also works with FPC/Lazarus. However I don't know any details (if the FPC support is reasonably full, or if there are caveats like having to rewrite your Delphi and/or .Net code)

Thaddy

  • Hero Member
  • *****
  • Posts: 16580
  • Kallstadt seems a good place to evict Trump to.
Re: .NET Libraries Loading from native application
« Reply #6 on: February 04, 2025, 05:16:40 pm »
Remobjects Hydra is a commercial product to integrate Delphi and .NET,
That is nothing more - for C# - than a luxury abstracted p/invoke, though. It offers the same for Java with my same opinion.
Not something I would pay for.
(That is: unless it saves time and effort or on programmer capabilities. )
« Last Edit: February 04, 2025, 05:22:30 pm by Thaddy »
But I am sure they don't want the Trumps back...

PascalDragon

  • Hero Member
  • *****
  • Posts: 5870
  • Compiler Developer
Re: .NET Libraries Loading from native application
« Reply #7 on: February 04, 2025, 09:51:59 pm »
is there a way to use a library written in .NET from an application written in lazarus/fpc?

The best - though probably also most involved - solution is probably hosting the .NET runtime in your application. It seems that the JCL/JVCL linked by gidesa is doing just that as can be seen in their JclDotNet unit. And Hydra is probably also doing the same.

A similar approach also exists for Mono though you might have to create an import unit yourself.

wfbhappy

  • New member
  • *
  • Posts: 8
Re: .NET Libraries Loading from native application
« Reply #8 on: February 05, 2025, 05:27:47 am »
DDNRuntime是一个用Delphi调用.NET的库。支持调用C#、VB.NET编写的程序集。支持自动导入.net dll,并创建.net接口。
DDNRuntime测试方法如下:
1、下载例子
gitee:https://gitee.com/kngstr/DDNRuntime-examples
github:https://github.com/KngStr/DDNRuntime-examples
2、下载仓库发行版/Release里的gencode(.net转pas工具)
3、下载仓库发行版/Release里的ddn-xxx-dcus.zip
4、以10.3 win64为例
将对应的dcu拷贝到 \Examples\DDNRuntime\win64\
将对应的dll拷贝到 \Examples\examples\bin-Win64
5、运行例子即可

 

TinyPortal © 2005-2018