使用/不使用 JCL 在 Delphi 中托管 CLR - 示例 [英] Hosting CLR in Delphi with/without JCL - example

查看:41
本文介绍了使用/不使用 JCL 在 Delphi 中托管 CLR - 示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以在这里发布一个如何在 Delphi 中托管 CLR 的示例吗?我在这里阅读了类似的问题,但我不能使用 JCL 作为我想在 Delphi 5 中托管它.谢谢.

Can somebody please post here an example how to host CLR in Delphi? I have read similar question here but I cannot use JCL as I want to host it in Delphi 5. Thank you.

这篇文章关于在 Fox Pro 中托管 CLR 看起来很有希望,但我不知道如何从 Delphi 访问 clrhost.dll.

This article about hosting CLR in Fox Pro looks promising but I don't know how to access clrhost.dll from Delphi.

编辑 2: 我放弃了 Delphi 5 的要求.现在我正在使用 Delphi 7 尝试 JCL.但我再次找不到任何示例.这是我到目前为止所拥有的:

Edit 2: I give up on Delphi 5 requirement. Now I'm trying JCL with Delphi 7. But again I am unable to find any example. Here is what I have till now:

我的 C# 程序集:

namespace DelphiNET
{
    public class NETAdder
    {
        public int Add3(int left)
        {
            return left + 3;
        }
    }
}

我已将其编译为 DelphiNET.dll.

现在我想使用 Delphi 中的这个程序集:

Now I want to use this assembly from Delphi:

uses JclDotNet, mscorlib_TLB;

procedure TForm1.Button1Click(Sender: TObject);
var
  clr: TJclClrHost;
  ads: TJclClrAppDomainSetup;
  ad: TJclClrAppDomain;
  ass: TJclClrAssembly;
  obj: _ObjectHandle;
  ov: OleVariant;
begin
  clr := TJclClrHost.Create();
  clr.Start;
  ads := clr.CreateDomainSetup;
  ads.ApplicationBase := 'C:Delhi.NET';
  ads.ConfigurationFile := 'C:Delhi.NETmy.config';
  ad := clr.CreateAppDomain('myNET', ads);
  obj := (ad as _AppDomain).CreateInstanceFrom('DelphiNET.dll', 'DelphiNET.NETAdder');
  ov := obj.Unwrap;
  Button1.Caption := 'done ' + string(ov.Add3(5));
end;

这以错误结束:EOleError:Variant 未引用自动化对象

我很长时间没有使用 Delphi,所以我被困在这里......

I have not worked with Delphi for a long time so I am stuck here...

解决方案: COM 可见性存在问题,默认情况下并非如此.这是正确的 .NET 程序集:

Solution: There was problem in COM visibility which is not by default. This is the correct .NET assembly:

namespace DelphiNET
{
    [ComVisible(true)]
    public class NETAdder
    {
        public int Add3(int left)
        {
            return left + 3;
        }
    }
}

<小时>

重要提示:

在 Delphi 中使用 .NET 时,在程序开始时(即在 Application.Initialize; 之前)调用 Set8087CW($133F); 很重要.Delphi 默认启用了浮点异常(请参阅this)而 CLR 没有不喜欢他们.当我启用它们时,我的程序奇怪地冻结了.


Important note:

When working with .NET from Delp it is important calling Set8087CW($133F); at the beginning of your program (i.e. before Application.Initialize;). Delphi has enabled floating point exceptions by default (see this) and the CLR doesn’t like them. When I had them enabled, my program weirdly freezed.

推荐答案

该类必须是可可视的.如果您为整个程序集设置 ComVisible(false),则情况可能并非如此.

The class has to be comvisible. Which might not be the case if you have ComVisible(false) for the whole assembly.

.Net 类在默认情况下将与 IDispatch 兼容,因此您的示例应该可以正常工作,如果该类确实是可兼容的..

.Net classes will be IDispatch compatible by default, so your sample should work just fine, if the class really is comvisible..

但首先将其精简到最低限度.将您的 exe 与您的 .Net 程序集放在同一文件夹中,并跳过配置文件和应用程序库.

But strip it down to the bare minimum first. Put your exe in the same folder as your .Net assembly and skip the config file and application base.

在某些事情混淆之前,这里会发生例外,对吗?

Before something gets mixed up, the exception happesn here, right?

 ov := obj.Unwrap;

这篇关于使用/不使用 JCL 在 Delphi 中托管 CLR - 示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆